MGloves gold
Time Limit: 3000/1500 MS (Java/Others) MemoryLimit: 262144/262144 K (Java/Others)
Total Submission(s): 969 Accepted Submission(s): 416
Problem Description
MGis a lucky boy. He is always be able to find gold from underground.
It is known that the gold is a sequence with n elements,which has its own color C.
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 abystander, could you please help settle the problem and calculate the answer?
Input
Thefirst line is an integer T whichindicates the case number.(1<=T<=10)
And as for each case, there are 1 integer n in the first line which indicategold-number(1<=n<=100000).
Then there are n integers C inthe next line, the x-th integer means the x-th gold’s color(|C|<=2000000000).
Output
Asfor each case, you need to output a single line.
there should be one integer in the line which represents the least possiblenumber of shovels after taking away all n golds.
Sample Input
2
5
1 1 2 3 -1
5
1 1 2 2 3
Sample Output
2
3
Source
考察:对map的使用。水题。
Clear()函数可以删除。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
using namespace std;
int main()
{
int n,T;
int i,sum;
int a[100007];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
map<int,int>flag;
//memset(flag,0,sizeof(flag));
sum=1;
flag[a[0]]=1;
for(i=1;i<n;i++)
{
if(flag[a[i]]==1)
{
sum++;
flag.clear();
flag[a[i]]=1;
}
else
{
flag[a[i]]=1;
}
}
printf("%d\n",sum);
}
return 0;
}