MG loves gold

 Accepts: 451
 Submissions: 1382
 Time Limit: 3000/1500 MS (Java/Others)
 Memory Limit: 262144/262144 K (Java/Others)
Problem 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 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
  1. 2
  2. 5
  3. 1 1 2 3 -1
  4. 5
  5. 1 1 2 2 3
Sample Output
  1. 2
  2. 3
  3.  
  4. 题意:MG要挖矿,矿物的排列为从左到右的一串数列,数列中不同数值的元素代表不同种类的矿,他从左到右开始挖,每使用一把铲子挖的矿必须是不同种类的,否则需要换一把铲子再继续挖,直到所有矿都挖完为止,问MG总共使用了多少把铲子。
    思路:可以用一个set集合来记录当前所使用的铲子已经挖的矿,从左到右扫描每一个矿,若当前的矿在集合set中找到,说明这块矿用旧铲子挖到过,得用新的铲子继续挖,此时清空集合,再用来记录新铲子已经挖到的矿。记录使用了几把铲子即可;
    AC代码:
  1. #define _CRT_SECURE_NO_DEPRECATE
  2. #include<iostream>
  3. #include<algorithm>
  4. #include<vector>
  5. #include<string>
  6. #include<set>
  7. using namespace std;
  8. typedef long long ll;
  9. const int N_MAX = +;
  10. int N;
  11. ll A[N_MAX];
  12. set<ll>s;
  13. int main() {
  14. int T;
  15. scanf("%d", &T);
  16. while (T--) {
  17. scanf("%d", &N);
  18. for (int i = ; i < N; i++) {
  19. scanf("%lld", &A[i]);
  20. }
  21. int res = , i = ;
  22.  
  23. while (i < N) {
  24. bool what = ;
  25. s.insert(A[i]);
  26. for (int j = i + ; j < N; j++) {
  27. set<ll>::iterator it = s.find(A[j]);
  28. if (it != s.end()) {//说明该元素出现过
  29. i = j;//下次用新铲子的挖掘从当前j开始
  30. what = ;
  31. break;
  32. }
  33. else
  34. s.insert(A[j]);
  35. }
  36. s.clear();
  37. res++;
  38. if (!what)break;//中途没被打断过,顺利挖到尾部
  39. }
  40. printf("%d\n",res);
  41. }
  42. return ;
  43. }

best corder MG loves gold的更多相关文章

  1. (set)MG loves gold hdu6019

    MG loves gold Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  2. 【BestCoder Round #93 1001】MG loves gold

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=6019 [题意] 每次选择一段连续的段,使得这一段里面没有重复的元素; 问你最少选多少次; [题解] ...

  3. HDU 6019:MG loves gold(暴力set)

    http://acm.hdu.edu.cn/showproblem.php?pid=6019 题意:给出n个颜色的物品,你每次取只能取连续的不同颜色的物品,问最少要取多少次. 思路:从头往后扫,用se ...

  4. hdu 6020 MG loves apple 恶心模拟

    题目链接:点击传送 MG loves apple Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Ja ...

  5. 【HDU 6021】 MG loves string (枚举+容斥原理)

    MG loves string  Accepts: 30  Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: ...

  6. 【HDU 6020】 MG loves apple (乱搞?)

    MG loves apple  Accepts: 20  Submissions: 693  Time Limit: 3000/1500 MS (Java/Others)  Memory Limit: ...

  7. hdu 6021 MG loves string

    MG loves string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others ...

  8. MG loves string

    MG loves string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others ...

  9. hdu 6021 MG loves string (一道容斥原理神题)(转)

    MG loves string    Accepts: 30    Submissions: 67  Time Limit: 2000/1000 MS (Java/Others)    Memory ...

随机推荐

  1. ajax的序列化表单提交

    通过传统的 form 表单提交的方式上传文件 ? 1 2 3 4 <form id="uploadForm" action="" method=" ...

  2. github:Commit failed - exit code 1 received

    问题 使用github desktop 将项目提交到github,但提示Commit failed - exit code 1 received 开始以为名称过程,把名称改短,但还是失败. 原因 因为 ...

  3. 01_13_Struts_默认Action

    01_13_Struts_默认Action 1. 配置struts默认Action <package name="default" namespace="/&quo ...

  4. 01_2_Namespace命名空间

    01_2_Namespace命名空间 1. Namespace_命名空间 namespace决定了action的访问路径,默认为””,可以接收所有路径的action namespace可以写为/,或者 ...

  5. TabControl重写,添加关闭按钮

    class userTabControl : TabControl { const int CLOSE_SIZE = 15; protected override void OnInvalidated ...

  6. 53. Maximum Subarray@python

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

  7. 【转】嵌入式操作系统VxWorks中TFFS文件系统的构建

    时间:2005-02-20 来源:21IC中国电子网 作者:771所加固机工程部 蔡本华 高文炜 关键字:VxWorks   TFFS   嵌入式操作系统   文件系统       摘要:目前的嵌入式 ...

  8. w3resource_MySQL练习: Aggregate_functions

    w3resource_MySQL练习题:Aggregate_functions   1. Write a query to list the number of jobs available in t ...

  9. excel日期格式取年份

    具体思路:先将日期格式更改为常规格式,再取常规格式的前4位数字 例如:A1==1981/12/22 第一步B1=TEXT(A1,"emd") 第二步C1=LEFT(B1,4) 结束

  10. 关于host,nslookup,dig 的安装

    host,nslookup,dig依赖bind包,所以先看一下系统有没有bind包 命令如下:rpm -qa |grep bind 如果没有或者版本太低请升级安装 命令是:yum install bi ...