【Jason's_ACM_解题报告】Unique Snowflakes
来源:互联网 发布:大同seo大牛 编辑:程序博客网 时间:2024/05/16 17:30
Unique Snowflakes
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 10^9 , 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
一道比较简单的题目,但是有一个思想是我一直没有将他抽象出来的,叫“滑动窗口”,这道题很好的体现了这种思想。在求解的过程中,每次并非重新枚举序列的上界,因为序列越长越好,所以每次只需将下界直接移动到重复数字之后,并将上界继续向后移动即可。使用map或者set实现定位重复数字的工作,定位的时间复杂度为O(logN),所以总时间复杂度为O(NlogN)。
附代码如下:
MAP实现:
#include<cstdio>#include<map>using namespace std;#define MAXN (1000000+5)int n,A[MAXN],last[MAXN];map< int , int > current;int main(){int T;scanf("%d",&T);while(T--){current.clear();scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d",&A[i]);if(!current.count(A[i]))last[i]=-1;else last[i]=current[A[i]];current[A[i]]=i;}int L=0,R=0,ans=0;while(R<n){while(R<n&&last[R]<L)R++;ans=max(ans,R-L);L=last[R]+1;}printf("%d\n",ans);}return 0;}
SET实现:
#include<cstdio> #include<set>using namespace std;#define MAXN (1000000+5)int n;int A[MAXN];set< int > SNOW;int main(){int T;scanf("%d",&T);while(T--){SNOW.clear();scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&A[i]); int L=0,R=0,ans=0;while(R<n){while(!SNOW.count(A[R])&&R<n){SNOW.insert(A[R]);R++;}ans=max(ans,R-L);SNOW.erase(A[L++]);//while(A[L]!=A[R]){SNOW.erase(A[L]);L++;}//if(R!=L){SNOW.erase(A[L]);L++;}}printf("%d\n",ans);}return 0;}
0 0
- 【Jason's_ACM_解题报告】Unique Snowflakes
- 【Jason's_ACM_解题报告】Graveyard
- 【Jason's_ACM_解题报告】Assemble
- 【Jason's_ACM_解题报告】Pie
- 【Jason's_ACM_解题报告】Network
- 【Jason's_ACM_解题报告】Meteor
- 【Jason's_ACM_解题报告】Subsequence
- 【Jason's_ACM_解题报告】Ananagrams
- 【Jason's_ACM_解题报告】Rails
- 【Jason's_ACM_解题报告】Tree
- 【Jason's_ACM_解题报告】Quadtrees
- 【Jason's_ACM_解题报告】Division
- 【Jason's_ACM_解题报告】Bandwidth
- 【Jason's_ACM_解题报告】Fill
- 【Jason's_ACM_解题报告】Average
- 【Jason's_ACM_解题报告】Tour
- 【Jason's_ACM_解题报告】Spreading the Wealth
- 【Jason's_ACM_解题报告】Commando War
- 使用Row_Number的分页存储过程
- Git初级入门
- C语言重学的一些笔记。
- Python运算符重载
- 薇薇动听(一)
- 【Jason's_ACM_解题报告】Unique Snowflakes
- 蓝桥杯练习--硬币方案
- POJ -- 3259 Wormholes
- 【node.js学习】--(3)--读写文件
- 什么是stoket?
- 人类登月不可或缺 大型机半个世纪发展史
- 水下滑翔机可视化系统使用手册
- A - Friendship
- POJ -- 2253 Frogger