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. MyBatis_动态代理

    一.项目结构 二.代码实现 import java.util.List; import java.util.Map; import com.jmu.bean.Student; public inter ...

  2. Android-多线程Handler

    http://www.cnblogs.com/plokmju/p/android_Handler.html android不允许在主线程里做耗时操作,如网络操作,以此来避免ANR ANR(Applic ...

  3. lintcode 刷题记录··

    单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The ...

  4. PHP支持多线程吗?

    https://zhidao.baidu.com/question/2053529640037778107.html

  5. python中循环删除list和dict类型注意事项

    列表和字典在循环操作(增删)时,其长度会改变 # 删除 li = [11, 22, 33, 44, 'rock']中索引为单数的元素 # 方法一 del li[1::2] print(li) # [1 ...

  6. layui-学习03-页面元素

    布局: 相关样式: 包裹层 : layui-container ( 响应式 宽度 ) layui-fluid ( 100% 宽度 )行        : layui-row列        : lay ...

  7. CentOS 7运维管理笔记(3)----Linux路由器配置

    当正在配置的Linux主机需要作为路由器使用时,通过以下步骤配置后,子网上的计算机就可以访问外网了: 1. 编辑 /etc/sysctl.conf 文件,添加 net.ipv4_ip_forward ...

  8. Java基础之Java中的泛型

    1.为什么要使用泛型 这里我们俩看一段代码; List list = new ArrayList(); list.add("CSDN_SEU_Cavin"); list.add(1 ...

  9. Linux文件系统的实现 ZZ

    作者:Vamei 出处:http://www.cnblogs.com/vamei Linux文件管理从用户的层面介绍了Linux管理文件的方式.Linux有一个树状结构来组织文件.树的顶端为根目录(/ ...

  10. Linux中redis的安装与配置

    redis官网地址:http://www.redis.io/ redis安装 在Linux下安装Redis非常简单,这里以2.8.3版本为例,具体步骤如下: 1.下载源码,解压缩后编译源码. $ mk ...