Unique Snowflakes 详细题解

来源:互联网 发布:遗传算法 实际应用 编辑:程序博客网 时间:2024/06/12 22:45

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 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
1
Sample Output
3

详细题解:
首先我们来说一下这个题的题意是怎样的:我们有一个长度不超过一百万的序列,而题要求我们求出这个序列中连续的不含有重复元素的最长子序列的长度。


可以采用经典的滑动窗口的方法,即维护一个窗口,窗口的左右边界用两个变量left,right表示,先增加right直到出现重复元素,再增加left,再增加right,直到序列末尾。
对于一个序列,要找出连续的不含有重复元素的最长子序列,我们可以从第一个元素开始寻找,这个寻找的序列向后延伸,如果碰到一个序列中已经存在的元素,就缩短一个序列的头,如果这个已经存在的元素还存在于缩短的元素中,那么就继续缩短,直到将重复元素第一次出现的那个元素踢出序列。之后继续延伸知道原序列结束。最终的那个序列就是我们要找的序列,求出其长度即可。下面附上图加深理解吧:(这里我们随便建一个序列:2 3 4 5 6 7 5 8 9 12)

了解了思路,要怎么才能实现呢?这里我们引入一个set容器的知识进行实现。set容器的知识我会在另外一篇博客中阐明。(博客标题为“set容器”)

下面附上AC代码:
#include<stdio.h>#include<iostream>#include<algorithm>#include<set>using namespace std;#define MAX 1000000+5int a[MAX];int main(){set<int> s;int t,n;while(cin>>t){while(t--){cin>>n;for(int i=0;i<n;i++)cin>>a[i];s.clear();int left=0,right=0,ans=0;while(right<n){while(right<n&&!s.count(a[right]))s.insert(a[right++]);ans=max(ans,right-left);s.erase(a[left++]);  }cout<<ans<<endl;}}return 0;}


同样的思路,我们利用数组来进行求解。
创建一个数组pos[]用于记录每个元素所在的位置,这个数组初始化-1;用一个变量start来记录当前序列的起始位置,初始值为0;
然后枚举这个数列,依次记录每个元素的位置。
在记录元素位置之前,要先判断当前元素的位置pos[a[i]]是否大于等于start,如果大于,说明在之前这个元素已经出现过一次即已经在(start,i-1)中出现过;记下这个满足条件的子序列。继续枚举下一个子序列:start=pos[a[i]]+1。最终算出这几个符合条件的子序列中最长的子序列即可。

下面附上AC代码:

#include<stdio.h>#include<string.h>#include<string.h>#include<algorithm>#include<iostream>#define MAX 1000000+10using namespace std;int a[MAX],pos[MAX];int main(){int t,n;while(cin>>t){while(t--){cin>>n;for(int i=0;i<n;i++)cin>>a[i];memset(pos,-1,sizeof(pos));int start=0,ans=0;a[n]=a[n-1];for(int i=0;i<=n;i++){if(pos[a[i]]>=start){int k=i-start;ans=max(ans,k);start=pos[a[i]]+1;pos[a[i]]=i;}elsepos[a[i]]=i;}cout<<ans<<endl;}}return 0;}


原创粉丝点击