• best corder MG loves gold


                                                                    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|<=2000000000C<=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

    题意: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;
    }
    
  • 相关阅读:
    C#面向对象
    CSS样式表---------第三章:样式属性
    CSS样式表-------第二章:选择器
    CSS样式表------第一章:样式表的基本概念
    解决Web部署 svg/woff/woff2字体 404错误
    sql server 2012 如何收缩事务日志
    input file类型,文件类型的限制
    C#对XML、JSON等格式的解析
    SQL实现表名更改,列名更改,约束更改
    sql 坐标距离排序计算距离(转)
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/6690499.html
Copyright © 2020-2023  润新知