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 ...
随机推荐
- English-Names
English-Names 1. 西方姓名的组成 2. 职业姓氏 3. 更多相关链接 中国的姓名,姓氏在前,名子在后.传统也有中间字(世代字).名子非常多.所谓百家姓,姓氏数量有限,约500个左右. ...
- java学习-初级入门-面向对象①-面向对象概述-结构化程序设计
为了学习面向对象程序设计,今天我们先利用面向对象以前的知识,设计一个学生类. 要求进行结构化程序设计. 学生类: Student 要求:存储学生的基本信息(姓名.性别.学历层次和年级),实现学生信息的 ...
- Http服务和JSP
需要先安装tomocat8.0,并且使用的IDEA 一个web项目 新建项目 写代码 // 新建一个class @WebServlet("/test") public class ...
- jsp el表达式判空
https://www.cnblogs.com/sxdcgaq8080/p/8119186.html
- 201705 Ruby基础拾遗
Mixin override 异常处理 super 与super() 使用%()处理需要string interpolation但同时也需要" "(double quote)的状况 ...
- uboot源码分析2-启动第二阶段
一.背景知识 1.uboot第二阶段应该做什么? 概括来讲uboot第一阶段主要就是初始化了SoC内部的一些部件(譬如看门狗.时钟),然后初始化DDR并且完成重定位. 由宏观分析来讲,uboot的第二 ...
- 原型与继承与class
对象有属性(专业点叫静态属性)和方法(专业点叫静态方法)和原型属性和原型方法 除了系统自带的那么几百万个对象,我们自己写在js的创建的对象,自定义的对象,都来自对象的构造函数,用来构造对象的函数,叫做 ...
- 017、Java中使用float型
01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...
- delphi关闭和禁用Windows服务
function StopServices(const SvrName: string): Boolean; var SCH, SvcSCH: SC_HANDLE; SS: TServiceStatu ...
- YOKOGAWA ProSafe-RS 通道测试 CENTUMVP
20180927 我并没有调试这个项目 仅仅是听同事讲解了 横河ProSafe-RS通道测试 然后做了笔记 软件安装并不在本记录中 ProSafe-RS版本 CENTUMVP版本 ProSafe-RS ...