Emily the entrepreneur has a cool business idea: packaging and selling snow
akes. She has devised a
machine that captures snow
akes as they fall, and serializes them into a stream of snow
akes that
ow,
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
snow
ake in a package must be different from the others. Unfortunately, this is easier said than done,
because in reality, many of the snow
akes
owing through the machine are identical. Emily would like
to know the size of the largest possible package of unique snow
akes that can be created. The machine
can start lling the package at any time, but once it starts, all snow
akes
owing 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 snow
akes have
owed out of the machine.
Input
The rst 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 snow
akes processed by the machine.
The following n lines each contain an integer (in the range 0 to 109, inclusive) uniquely identifying a
snow
ake. Two snow
akes are identied by the same integer if and only if they are identical.
The input will contain no more than one million total snow
akes.
Output
For each test case output a line containing single integer, the maximum number of unique snow
akes
that can be in a package.

整体思路是用滑动窗口,每次对一段区间[i,j]操作后,左指针+1,再把右指针尽量后移,得到尽量长的区间。关键是如何判断右指针什么时候该停止,即判断一个区间是否可行。

 #include<cstdio>
#include<map>
using namespace std;
int a[],last[];
map<int,int> cur;
int main()
{
int i,j,k,n,p,q,x,y,z,t,ans;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
cur.clear();
for (i=;i<=n;i++)
scanf("%d",&a[i]);
for (i=;i<=n;i++)
{
if (cur.count(a[i]))
last[i]=cur[a[i]];
else
last[i]=-;
cur[a[i]]=i;
}
ans=;
for (i=,j=;j<=n;i++)
{
while (j<=n&&i>last[j]) j++;
if (j-i>ans) ans=j-i;
}
printf("%d\n",ans);
}
}

以上为做法一。用nlogn时间求出上一个与之相同的元素的坐标last[i],然后右指针右移直到它的last超过左指针。

求last的过程中,用map存储元素上一次出现的位置,边扫描边更新。

 #include<cstdio>
#include<set>
using namespace std;
int a[];
int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int i,j,k,m,n,p,q,x,y,z,ans,T;
scanf("%d",&T);
while (T--)
{
set<int> s;
scanf("%d",&n);
for (i=;i<=n;i++)
scanf("%d",&a[i]);
ans=-;
i=j=;
while (j<=n)
{
while (j<=n&&!s.count(a[j])) s.insert(a[j++]);
ans=max(ans,j-i);
s.erase(a[i++]);
}
printf("%d\n",ans);
}
}

以上为做法二。用set存储一个元素当前是否存在。左指针左移时删除元素,右指针右移时加入元素。

uva 11572 unique snowflakes——yhx的更多相关文章

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

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

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

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

  3. UVA - 11572 Unique Snowflakes

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

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

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

  5. UVA 11572 Unique snowflakes (滑窗)

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

  6. UVA - 11572 Unique Snowflakes 滑动扫描

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

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

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

  8. 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)

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

  9. UVA 11527 Unique Snowflakes

    用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...

随机推荐

  1. DataList分页访问FooterTemplate模板里的控件

    今天做DataList分页的时候,突然想把分页控件写在FooterTemplate模板里面,弄了很久都访问不到控件,终于发现问题所在,以下是访问FooterTemplate里控件的方法: <Fo ...

  2. MySQL架构

    一.MySQL逻辑架构         第一层,即最上一层,所包含的服务并不是MySQL所独有的技术.它们都是服务于C/S程序或者是这些程序所需要的 :连接处理,身份验证,安全性等等.         ...

  3. js generator数据类型

    1. 概述 generator 是ES6引入的新的数据类型, 看上去像一个函数,除了使用return返回, yield可以返回多次. generator 由function* 定义, (注意*号), ...

  4. java ftp

    FTPUtil import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import jav ...

  5. NoSuchMethodException <init>()

    1. Question Description: SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/n ...

  6. DataTable 转换为ArrayList 再转换成 json 格式

    //JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();        //javaScriptSeriali ...

  7. Arcengine实现创建网络数据集札记(三)

    后记 下面给出项目中用到的自定义的封装类. AE许可初始化封装类: public class AELicenseChecker { private static volatile AELicenseC ...

  8. C语言可变参数函数实现原理

    一.可变参数函数实现原理 C函数调用的栈结构: 可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 本 ...

  9. iOS开发工程师面试知识点汇总

    1.KVO实现原理 2.内存管理 3.Runtime 4.GCD 5.Block 6.响应者链 7.@peoperty属性特性 8.单元格cell加载图片处理

  10. Web应用程序系统的多用户权限控制设计及实现-数据库设计【2】

    搭建一个Web权限配置的系统,需要以下五张数据表:人员表,分组表,页面表,目录表,操作权限表.各张数据表中用到的id均为自增1的标识,每张数据表的定义如下: 1.1人员表(operatorinfo)