MG loves gold
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?
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).
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.
2 5 1 1 2 3 -1 5 1 1 2 2 3
2 3
题意:MG要挖矿,矿物的排列为从左到右的一串数列,数列中不同数值的元素代表不同种类的矿,他从左到右开始挖,每使用一把铲子挖的矿必须是不同种类的,否则需要换一把铲子再继续挖,直到所有矿都挖完为止,问MG总共使用了多少把铲子。
思路:可以用一个set集合来记录当前所使用的铲子已经挖的矿,从左到右扫描每一个矿,若当前的矿在集合set中找到,说明这块矿用旧铲子挖到过,得用新的铲子继续挖,此时清空集合,再用来记录新铲子已经挖到的矿。记录使用了几把铲子即可;
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> #include<string> #include<set> using namespace std; typedef long long ll; const int N_MAX = 100000+4; int N; ll A[N_MAX]; set<ll>s; int main() { int T; scanf("%d", &T); while (T--) { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%lld", &A[i]); } int res = 0, i = 0; while (i < N) { bool what = 0; s.insert(A[i]); for (int j = i + 1; j < N; j++) { set<ll>::iterator it = s.find(A[j]); if (it != s.end()) {//说明该元素出现过 i = j;//下次用新铲子的挖掘从当前j开始 what = 1; break; } else s.insert(A[j]); } s.clear(); res++; if (!what)break;//中途没被打断过,顺利挖到尾部 } printf("%d ",res); } return 0; }