Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a machine that captures snowflakes as they fall, and serializes them into a stream of snowflakes that flow, one by one, into a package. Once the package is full, it is closed and shipped to be sold. The marketing motto for the company is “bags of uniqueness.” To live up to the motto, every snowflake in a package must be different from the others. Unfortunately, this is easier said than done, because in reality, many of the snowflakes flowing through the machine are identical. Emily would like to know the size of the largest possible package of unique snowflakes that can be created. The machine can start filling the package at any time, but once it starts, all snowflakes flowing from the machine must go into the package until the package is completed and sealed. The package can be completed and sealed before all of the snowflakes have flowed out of the machine.
Input
The first line of input contains one integer specifying the number of test cases to follow. Each test case begins with a line containing an integer n, the number of snowflakes processed by the machine. The following n lines each contain an integer (in the range 0 to 109, inclusive) uniquely identifying a snowflake. Two snowflakes are identified by the same integer if and only if they are identical. The input will contain no more than one million total snowflakes.
Output
For each test case output a line containing single integer, the maximum number of unique snowflakes that can be in a package.
Sample Input
1 5 1 2 3 2 1
Sample Output
3

分析:

从一个有n个数的序列中,找到一个最大的子段(连续的),使得这个最大子段中没有重复元素,输出最大子段的长度。

注意:序列是不连续的,子段必须是连续的

做法:

对于该类段查找问题可以采用经典的滑动窗口方法,即维护一个窗口,窗口的左右边界用两个变量L,R代表,先增加R直到出现重复数字,再增加L,再增加R,直到R达到n

贴张图形象得一批~

set好用!

code:

#include<stdio.h>
#include <iostream>
#include <math.h>
#include <queue>
#include<set>
using namespace std;
#define max_v 1000005
int a[max_v];
int main()
{
set<int> s;
int t,n;
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
s.clear();
int left=,right=,ans=;
while(right<n)
{
while(right<n&&!s.count(a[right]))
{
s.insert(a[right++]);
}
ans=max(ans,right-left);
s.erase(a[left++]);
}
cout<<ans<<endl;
}
return ;
}

11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)的更多相关文章

  1. (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)

    题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #inclu ...

  2. UVa 11572 Unique snowflakes【滑动窗口】

    题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...

  3. UVA - 11572 Unique Snowflakes 滑动扫描

    题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...

  4. UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)

    题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素. 分析: 法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r] ...

  5. 【uva 11572】Unique Snowflakes(算法效率--滑动窗口,3种实现方法)

    题意:求长度为N的序列中,最长的一个无重复元素的连续子序列. 解法:[L,R]每次R++或L++延伸就可以得到答案. 实现:(1)next[],last[]--O(n): 1 #include< ...

  6. uva 11572 unique snowflakes——yhx

    Emily the entrepreneur has a cool business idea: packaging and selling snowakes. She has devised ama ...

  7. UVA 11572 Unique snowflakes (滑窗)

    用set,保存当前区间出现过的数字,如果下一个数字没有出现过,加入,否则删掉左端点,直到没有重复为止 #include<bits/stdc++.h> using namespace std ...

  8. UVA - 11572 Unique Snowflakes

    /* STLsort离散化==T 手工sort离散化==T map在线==T map离线处理c==A 240ms */ #include<cstdio> #include<map&g ...

  9. uva 11572 - Unique Snowflakes(和书略有不同)

    本书是关于使用刘汝佳set, 通过收集找到.count()和删除.erase().这种方法比我好.用较短的时间. 我想map这个任务可以完成.但是,这是不容易删除,必须先找到find()标.然后删除索 ...

随机推荐

  1. HTML语言中img标签的alt属性和title属性的作用与区别

    alt属性是在你的图片因为某种原因不能加载时在页面显示的提示信息,它会直接输出在原本加载图片的地方,而title属性是在你鼠标悬停在该图片上时显示一个小提示,鼠标离开就没有了,有点类似jQuery的h ...

  2. mysql if函数使用例子

    1.场景一 有时查询数量a 同时查询过滤后的数量b 2. 代码 SELECT count(id) as total_count, count( IF ( date(order_time) = DATE ...

  3. java unsupported major.minor version 51.0 解决

    1.概述 出现如题所述异常 是因为jdk高版本 编译后的class文件 运行在低版本的jre环境下(如jdk7编译 运行在jdk6环境下) 2. 解决方案 在eclipse等ide中重新编译 指定编译 ...

  4. linux sheel script demo

    demo1 (输入/输出) 1.1. target : 输入姓.名, 输出姓名 1.2. create directory mkdir ~/bin 1.3. create & edit she ...

  5. Office - InfoPath

    1. 移除隐藏空间后剩余的空白: http://social.technet.microsoft.com/Forums/sharepoint/zh-TW/3dea3014-f808-428b-b283 ...

  6. 【转】ArcGIS Server10.1安装常见问题及解决方案

    转载自:http://www.higis.cn/Tech/tech/tId/85/ 最近因为更换系统的原因,重新安装了ArcGISServer 10.1.过程中遇到了几个小问题,虽然都一一解决了,但也 ...

  7. redis 在linux安装

    转自:http://futeng.iteye.com/blog/2071867 下载 官网下载 安装 tar zxvf redis-2.8.9.tar.gz cd redis-2.8.9 #直接mak ...

  8. 爬虫入门之Scrapy框架实战(新浪百科豆瓣)(十二)

    一 新浪新闻爬取 1 爬取新浪新闻(全站爬取) 项目搭建与开启 scrapy startproject sina cd sina scrapy genspider mysina http://roll ...

  9. 事件循环进阶:macrotask与microtask

    这段参考了参考来源中的第2篇文章(英文版的),(加了下自己的理解重新描述了下), 这里没法给大家演示代码,我就简单说下我的理解吧. promise和settimeout 在一起的时候执行顺序是个有意思 ...

  10. angular2 文件上传

    ng2-file-upload文件上传 1.安装ng2-file-upload模块 npm install ng2-file-upload --save 2.如果使用systemjs打包,需要在配置s ...