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. Winform窗体实现简单的二维码生成和保存

    二维码的生成需要用到二维码生成的类库,ThoughtWorks.QRCode.dll 步骤: 第一步:下载二维码生成类库,ThoughtWorks.QRCode.dll 第二步:新建winform项目 ...

  2. ajax onblur 用法

    value为当前框中的值 <input  name="num"type="text"  onblur="changeorder(id,this. ...

  3. 调用另一个Activity

    <转>调用另一个Activity Intent对象的使用 范例说明 前一个范例介绍了如何运用切换Layout的方式,进行手机页面间的转换.如果要转换的页面并不单只是背景.颜色或文字内容的不 ...

  4. SQL数据库基础(八)

    连接查询:通过连接运算符可以实现多个表查询.连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志. 常用的两个链接运算符: 1.join   on 2.union     在关 ...

  5. JSP--JavaBean

    JSP 最强有力的一个方面就是能够使用 JavaBean 组件. 按照 Sun 公司的定义, JavaBean是一个可重复使用的软件组件.实际上 JavaBean 是一种 Java 类,通过封装属性和 ...

  6. JSP内置对象--request对象

    本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttri ...

  7. 【读书笔记】iOS-GCD-block-后台运行

    当一个app按home键退出的时候,只有最多5秒的时间做一些保存或清理资源的工作.但是调用beginBackgroundTaskWithExpirationHandler方法,可以最多有10分时间在后 ...

  8. iOS网络-04-大文件下载

    大文件下载注意事项 若不对下载的文件进行转存,会造成内存消耗急剧升高,甚至耗尽内存资源,造成程序终止. 在文件下载过程中通常会出现中途停止的状况,若不做处理,就要重新开始下载,浪费流量. 大文件下载的 ...

  9. Win7下:编译器错误信息: CS0016: 未能写入输出文件

    错误如下: "/"应用程序中的服务器错误. 编译错误 说明: 在编译向该请求提供服务所需资源的过程中出现错误.请检查下列特定错误详细信息并适当地修改源代码. 编译器错误消息: CS ...

  10. Volley源码分析(1)----Volley 队列

    Android网络框架很多,但是基于Google自己的volley,无疑是优秀的一款. 网络框架,无外乎解决一下几个问题,队列,缓存,图片异步加载,统一的网络请求和处理等. 一.Volley 队列 启 ...