MG is a lucky boy. He is always be able to find gold from underground.
It is known that the gold is a sequence with nn elements, which has its own color CC.
MG can dig out a continuous area of sequence every time by using one shovel, but he's unwilling to dig the golds of the same color with one shovel.
As a greedy person, he wish to take all the n golds away with least shovel. The rules also require MG not to dig twice at the same position.
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
Input
The first line is an integer TT which indicates the case number.(1<=T<=101<=T<=10)
And as for each case, there are 11 integer nn in the first line which indicate gold-number(1<=n<=1000001<=n<=100000).
Then there are nn integers CC in the next line, the x-th integer means the x-th gold’s color(|C|<=2000000000∣C∣<=2000000000).
Output
As for each case, you need to output a single line.
there should be one integer in the line which represents the least possible number of shovels after taking away all nn golds.
Sample Input
2
5
1 1 2 3 -1
5
1 1 2 2 3
Sample Output
2
3
这题意思就是求一个序列有多少个连续的中间没有重复元素的子序列。
#include<stdio.h>
#include<set>
using namespace std;
int main()
{
set<int> s;
int t;
scanf("%d",&t);
while(t--)
{
int n,ans=1;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int c;
scanf("%d",&c);
if(s.find(c)==s.end())
s.insert(c);
else
{
s.clear();
s.insert(c);
ans++;
}
}
printf("%d\n",ans);
s.clear();
}
return 0;
}
MG loves gold Accepts: 451 Submissions: 1382 Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Problem DescriptionMG is a lucky boy.