(UVA
来源:互联网 发布:腾讯视频评论源码 编辑:程序博客网 时间:2024/06/16 09:07
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
题意:给定长为的序列,求最长的连续序列(序列中不能出现相同的数字)
分析:1 2 3 2 1 令l=0,r=0, 不断让r++,用set判断是否出现相同的数,当出现相同的数时,l++, 在这个过程ans=max(ans,r-l)
循环一次过后ans即为所求,时间复杂度O(nlogn)
1.set
#include<bits/stdc++.h>using namespace std;#define mem(a,n) memset(a,n,sizeof(a))const int N=1e6+5;const int INF=0x3f3f3f3f;int a[N];int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); set<int>st; int l=0,r=0,ans=0; while(r<n) { if(st.count(a[r])==0) st.insert(a[r++]); else st.erase(a[l++]); //printf("l=%d r=%d\n",l,r); ans=max(ans,r-l); } printf("%d\n",ans); } return 0;}
2.map将数的下标记录
判断a[i]是否出现过,未出现过,就令记录下标的数组a[i]=-1,反之记录其下标
时间复杂度O(nlogn)
#include<bits/stdc++.h>using namespace std;#define mem(a,n) memset(a,n,sizeof(a))const int N=1e6+5;const int INF=0x3f3f3f3f;int a[N],xb[N];int main(){ int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%d",&a[i]); map<int,int>mp; int i=0,l=0,r=0,ans=0; while(i<n) { if(!mp.count(a[i])) xb[i]=-1; else xb[i]=mp[a[i]]; mp[a[i]]=i; i++; } while(r<n) { if(xb[r]<l) r++; else l++; ans=max(ans,r-l); } printf("%d\n",ans); } return 0;}
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- MFC工具栏停靠到左边栏的BUG
- Linux指令笔记(更新...)
- 流程图基础绘制方法和流程图制作软件功能详解
- String、StringBuffer与StringBuilder之间区别
- Unity2d引入新功能SpriteAtlas,Sprite新的图集方式
- (UVA
- android 5.0后对于apk 跑32 64 的逻辑
- Professional JS(20-JSON Syntax/Serialization/Parse/XMLHttpRequest Object)
- HashTable哈希表改进
- 小白学分布式程序开发9-三阶段提交
- phpredis connect 与 pconnect
- 并查集 判环
- 如何在你的应用中调用其它已知包名的应用?
- Kafka到底会不会丢数据