hdu 6019 MG loves gold

本文介绍了一道关于挖取不同颜色金子的算法题目。任务目标是最小化使用铲子的数量来挖取所有金子,同时确保不重复挖取相同颜色的金子。文章提供了完整的代码实现,并解释了题目的正确理解方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

MG loves gold

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1527    Accepted Submission(s): 629


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  n  elements, 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  T  which indicates the case number.( 1<=T<=10

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

Then there are  n  integers  C  in the next line, the x-th integer means the x-th gold’s color( |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  n  golds.
 

Sample Input
  
2 5 1 1 2 3 -1 5 1 1 2 2 3
 

Sample Output
  
2 3

刚开始题意理解错了,以为求最少可以改变顺序,但真正的意思是按顺序挖金子,遇到相同的换一把铲子,看代码吧

#include<stdio.h>
#include<set>
using namespace std;
int main()
{
    set<int>gold;
    gold.clear();
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int c,n,sum=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d",&c);
            if(gold.find(c)!=gold.end())
            {
                sum++;
                gold.clear();
                gold.insert(c);
            }
            else
                gold.insert(c);
        }
        printf("%d\n",sum+1);
        gold.clear();
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值