Best Coder 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 nnn elements, which has its own color CCC.

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 TTT which indicates the case number.(1<=T<=101<=T<=101<=T<=10)

And as for each case, there are 111 integer nnn in the first line which indicate gold-number(1<=n<=1000001<=n<=1000001<=n<=100000).

Then there are nnn integers CCC in the next line, the x-th integer means the x-th gold’s color(∣C∣<=2000000000|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 nnn golds.
Sample Input

2
5
1 1 2 3 -1
5
1 1 2 2 3

Sample Output

2

3



题意:用一把铲子去挖宝石,每个宝石都有自己的颜色(宝石上面的数字代表颜色),可以连续着挖一段,但是同一把铲子不能挖到同一种颜色的宝石,问最少用多少铲子可以把宝石挖完。


刚看到这道题的时候我想的是直接用set来进行去重,然后我们找到有几个数是重复的,那么就需要加上几把铲子,但是直接wa,后来想了想,发现如果是这样的话,有数据是不行的,比如:

1

4

1 2 1 2

这组数据答案应该是2,但是按照上面的想法是3,所以pass。


接下来我想到我可以判断下一个要挖的宝石的颜色是否在前面挖过了,如果有,那么铲子加一,没有,继续往下挖。

同样是用set,用到set中的count(x)函数,用来确定x在set中是否出现过。

就比如说数据:

5

1 1 2 2 3

首先我把1插入set中,往下挖,判断下一个数是否已经在set中出现,很明显出现过,那么铲子数我要加一,然后把set清空(这个时必须要的,因为每次我要挖的都是连续的且数字没有的相同的序列),再把下一个要挖的宝石插入set中,再继续往下挖,重复上面的步骤。就可以得到最后的铲子数。


AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
#define MAXN 20000
using namespace std;
int color;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        set<int>M;
        int n;
        scanf("%d",&n);
        int sum = 1;
        for(int i = 0 ; i < n; i++)
        {
            scanf("%d",&color);
            if(M.count(color))
            {
                M.clear();
                M.insert(color);
                sum++;
            }
            else
            {
                M.insert(color);
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值