uva 11572 unique snowflakes——yhx
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的更多相关文章
- (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
题目地址:UVa 11572 这样的方法曾经接触过,定义两个指针,不断从左向右滑动,推断指针内的是否符合要求. 这个题为了能高速推断是否有这个数,能够用STL中的set. 代码例如以下: #inclu ...
- UVa 11572 Unique snowflakes【滑动窗口】
题意:给出 n个数,找到尽量长的一个序列,使得该序列中没有重复的元素 看的紫书,滑动窗口来做的 当右端碰到有相同的数的时候,左端向前滑动一个数 模拟一个样例好理解些 #include<iostr ...
- 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 ...
- UVA - 11572 Unique Snowflakes 滑动扫描
题目:点击打开题目链接 思路:从左往右扫描,定义扫描左端点L,右端点R,保证每次往几何中添加的都是符合要求的连续的数列中的元素,L和R从0扫到n,复杂度为O(n),使用set维护子数列,set查找删除 ...
- UVA - 11572 Unique Snowflakes(唯一的雪花)(滑动窗口)
题意:输入一个长度为n(n <= 10^6)的序列A,找到一个尽量长的连续子序列AL~AR,使得该序列中没有相同的元素. 分析: 法一:从r=0开始不断增加r,当a[r+1]在子序列a[l~r] ...
- 11572 - Unique Snowflakes(贪心,两指针滑动保存子段最大长度)
Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...
- UVA 11527 Unique Snowflakes
用STL做会很方便 SET: /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring ...
随机推荐
- .NET中Debug模式与Release模式
Visual Studio 项目对程序的发布和调试版本分别有单独的配置.顾名思义,生成调试版本的目的是用于调试,而生成发布版本的目的是用于版本的最终分发. 如果在 Visual Studio 中创建程 ...
- GWYAlertSelectView 选择收货地址和选择联系人
iOS 开发在app内(特别是在类似购物和旅游类)经常会用到选择,添加联系人和收货地址,而且经常都是在跳转界面,很是麻烦,今天我就自己封装了一个类似弹出视图的代码,方便了很多!demo地址: http ...
- ElasticSearch文档-简单介绍
ElasticSearch是一个基于Lucene构建的开源,分布式,RESTful搜索引擎.设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便.支持通过HTTP使用JSON进行数据索引 ...
- ubuntu定时执行脚本(crond)
如果发现您的系统里没有这个命令,请安装下面两个软件包. vixie-cron crontabs crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表.-u ...
- 2013学习总结----JavaScript
javascript面向对象,实现的几种方式 1:直接使用JSON对象 var o1={ "a":1, "b":2, "c":functio ...
- js得到屏幕宽高、页面宽高 (window.screen.availHeight)等--笔记
window.screen.availWidth 返回当前屏幕宽度(空白空间) window.screen.availHeight 返回当前屏幕高度(空白空间) window.screen.width ...
- windows下mongodb安装与使用
首先安装mongodb 1.下载地址:http://www.mongodb.org/downloads 2.解压缩到自己想要安装的目录,比如d:\mongodb 3.创建文件夹d:\mongodb\d ...
- SharePoint 2010 文档管理之过期归档工具
前言:使用过SharePoint的人都知道,SharePoint对于操作是便捷的,但是对于数据量承载却是不令人满意的,这样,就要求我们需要更加合理的使用,规范大家的使用规则和习惯,所以,定期清理不必要 ...
- android项目中gen目录不能自动生成R.java的原因
1.调用的资源文件不存在:xml文件中有些控件没有关联引用:把项目缺少的文件加上,包括资源文件,如 values中的strings.xml或者图片等资源. 2.项目中缺少必须的系统文件(比如:defa ...
- 基于Retrofit+RxJava的Android分层网络请求框架
目前已经有不少Android客户端在使用Retrofit+RxJava实现网络请求了,相比于xUtils,Volley等网络访问框架,其具有网络访问效率高(基于OkHttp).内存占用少.代码量小以及 ...