UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)
题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素。
分析:
法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r]中出现过,只需增大l,并继续延伸r,因为a[l~r]为可行解,则l增大后必然还是可行解。用set判断a[r+1]是否出现过,并进行a[l]的删除操作。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1000000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN];
set<int> s;
int main(){
int T;
scanf("%d", &T);
while(T--){
s.clear();
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
}
int l = 0, r = 0, ans = 0;
while(r < n){
while(r < n && !s.count(a[r])){
s.insert(a[r++]);
}
ans = Max(ans, r - l);
s.erase(a[l++]);
}
printf("%d\n", ans);
}
return 0;
}
法二:mp[cur]记录的是元素cur所对应的下标。只要与a[r+1]相同的上一个元素下标小于l,则可延伸,++r,否则++l。
#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 1000000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int a[MAXN], last[MAXN];
map<int, int> mp;
int main(){
int T;
scanf("%d", &T);
while(T--){
mp.clear();
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
if(!mp.count(a[i])){
last[i] = -1;
}
else{
last[i] = mp[a[i]];//前一个大小为a[i]的元素下标
}
mp[a[i]] = i;//不断更新大小为a[i]的当前下标
}
int l = 0, r = 0, ans = 0;
while(r < n){
while(r < n && last[r] < l) ++r;
ans = Max(ans, r - l);
++l;
}
printf("%d\n", ans);
}
return 0;
}
UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)的更多相关文章
- (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #inclu ...
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- uva 11572 unique snowflakes——yhx
Emily the entrepreneur has a cool business idea: packaging and selling snowakes. She has devised ama ...
- UVA - 11572 Unique Snowflakes 滑动扫描
题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...
- 【uva 11572】Unique Snowflakes(算法效率--滑动窗口,3种实现方法)
题意:求长度为N的序列中,最长的一个无重复元素的连续子序列. 解法:[L,R]每次R++或L++延伸就可以得到答案. 实现:(1)next[],last[]--O(n): 1 #include< ...
- UVA - 11572 Unique Snowflakes
/* STLsort离散化==T 手工sort离散化==T map在线==T map离线处理c==A 240ms */ #include<cstdio> #include<map&g ...
- uva 11572 - Unique Snowflakes(和书略有不同)
本书是关于使用刘汝佳set, 通过收集找到.count()和删除.erase().这种方法比我好.用较短的时间. 我想map这个任务可以完成.但是,这是不容易删除,必须先找到find()标.然后删除索 ...
- UVA 11572 Unique snowflakes (滑窗)
用set,保存当前区间出现过的数字,如果下一个数字没有出现过,加入,否则删掉左端点,直到没有重复为止 #include<bits/stdc++.h> using namespace std ...
- 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
随机推荐
- 隐患写法flag.equals("true")带来的空指针异常
分类:2008-06-04 12:47 467人阅读 评论(0) 收藏 举报 linuxjava测试 昨天,有同事A对同事B写的程序进行测试时,出现错误,看控制台信息,发现抛出了空指针异常. 调查结果 ...
- pgsql 查询jsonb中包含某个键值对的表记录
pgsql 查询jsonb中包含某个键值对的表记录 表名 table_name ,字段 combos 类型为 jsonb 可为空,示例内容如下, $arr_combos = [ ['id' => ...
- C#遍历DataSet
] foreach (DataRow dr in dt.Rows) ///遍历所有的行 foreach (DataColumn dc in dt.Columns) //遍历所有的列 Console.W ...
- webpack 4 x使用详细
1.首先安装node.js 2.打开控制台cmd,输入npm install webpack webpack-cli webpack-dev-server -g 3.在本地磁盘上建一个文件夹,然后通过 ...
- 011.Delphi插件之QPlugins,延时加载服务
这个DEMO是是把DLL插件的相关信息做成了一个配置文件,主程序加载这个配置文件,从而起到延时加载的作用 主程序代码如下 unit Frm_Main; interface uses Winapi.Wi ...
- 012-PHP创建一个多维数组
<?php $Cities = array( //二维数组array() "华北地区"=>array( "北京市", "天津市" ...
- NoSql相关
1 NoSQL, No Problem: An Intro to NoSQL Databases https://www.thoughtworks.com/insights/blog/nosql-n ...
- GAN网络进行图片增强
GAN网络进行图片增强 基于Tensorflow框架 调用ModifyPictureSize.py文件 代码如下: from skimage import io,transform,color imp ...
- JuJu团队12月3号工作汇报
JuJu团队12月3号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 于达 修改batch里给sentence加padding的方法 继续调试 无 婷婷 给crossentro ...
- 如何用hugo搭建个人博客
如何用hugo搭建个人博客 1. 安装 Hugo 点击跳转 Hugo Releases win10 步骤: 下载解压 , 然后添加环境变量 测试: #命令行测试 hugo version 2. 创建站 ...