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
思路 模拟+stl 维护
代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define LL long long
#define M 100000+100
#define inf 0x3f3f3f3f
#define mod 100009
using namespace std;
set < LL > gold;
map< LL ,bool > vis;
void solve1() //用set来维护 ,某个数据是不是出现过 499ms
{
int n;
scanf("%d",&n);
gold.clear();
int i,j;
int ans=1;
for(i=0;i<n;i++)
{
LL a;
scanf("%lld",&a);
if(gold.count(a)) // 如果存在这个a值
{
ans++;
gold.clear();
gold.insert(a);
}
else gold.insert(a);
}
printf("%d\n",ans);
}
void solve2() //用map来维护 ,判定是不是存在过某数据a 717ms
{
int n;
scanf("%d",&n);
vis.clear();
int i,j;int ans=1;
for(i=0;i<n;i++)
{
LL a;
scanf("%lld",&a);
if(vis[a])
{
ans++;
vis.clear();
vis[a]=true;
}
else vis[a]=true;
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
solve1();
//solve2();
}
return 0;
}