HDU_6019(set)

Description

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 nelements, 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 a bystander, could you please help settle the problem and calculate the answer?
 
Input
 
The first line is an integer Twhich indicates the case number.(1<T<=10.)
And as for each case, there are Tinteger n in the first line which indicate gold-number.(1<=n<=1e5)
Then there are n integers Cthe next line, the x-th integer means the x-th gold’s color.(|C|<2e9)
 
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 ngolds.
 
Sample Input
2
5
1 1 2 3 -1
5
1 1 2 2 3

Sample Output
2
3


学长讲了题解后,明白了一点,在数据自身的范围好大时应该用离散的思想,就是把这个数映射成一个较小的数 从而遍历那个范围。
但一直也不会实现,碰到了一个用了set的题目后,想起这道题似乎也可以这样做。
开始wa了一发,需要注意的是铲子换过一次,就要对set中的元素清空,不然会浪费铲子。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100000;
int a[maxn];
set<int> s;

int main(){
	int T,n,ans;
	scanf("%d",&T);
	while(T--){
		s.clear();		
		ans = 1;
		scanf("%d",&n);
		for(int i=0;i<n;i++){
			scanf("%d",&a[i]);
		}
		for(int i=0;i<n;i++){
			if(s.count(a[i])){
				ans++;
				s.clear();
			}
			s.insert(a[i]);
		}
		printf("%d
",ans);
	}
	return 0;	
}





原文地址:https://www.cnblogs.com/gjy963478650/p/7297542.html