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
先构成一个窗口及1,然后一个一个增大右边的那条边 变为 1 2,然后在为 1 2 3,在增加时发现前面已经出现过一次 2 ,所以这个窗口不能在延伸了,但数据还没有完,所以现将最左边那个去除,现在窗口为2 3,接着发现还不能增加,在去除最左边,窗口为 3,此时再延长右边,为3 2,再延长为 3 2 1,思路就是这样。实现方法有很多,如SET,MAP,或者队列,dequeue,或数组(自己用两个指针维护)
参考:
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int num[1000010];
int main()
{
int kase;
cin >> kase;
while (kase--)
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> num[i];
set<int> s;
int l = 0, r = 0, max = 0, ml=0, mr=0;
while(r<n)
{
if (s.find(num[r]) == s.end())
{
s.insert(num[r]);
r++;
if (r - l>max)
{
max = r - l;
ml = l;
mr = r;
}
}
else
{
s.erase(num[l]);
l++;
}
}
cout << mr - ml << endl;
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<ctype.h>
#include<stack>
#include<math.h>
#include <string>
#include<algorithm>
#include<iomanip>
#include<sstream>
#define _for(i,a,b) for(int i = (a);i < (b);i++)
#define mset(x) memset(x,0,sizeof(x));
// const int inf = 0x3f3f3f3f;
using namespace std;
const int maxn = 1000000 + 5;
int A[maxn],last[maxn];//last 表示上一个相同元素的下标 利用MAP
map<int,int> cur;
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cur.clear();
for(int i = 0; i< n;i++)
{
scanf("%d",&A[i]);
if(!cur.count(A[i])) last[i] = -1;
else last[i] = cur[A[i]];
cur[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++;
}
printf("%d\n", ans);
}
return 0;
}