您的位置:首页 > 编程语言 > Go语言

【BestCoder Round #93】HDU6019 MG loves gold

2017-04-01 21:36 344 查看
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.

贪心地扫描一遍,用set维护当前集合。

#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
using namespace std;
set<int> s;
int a[100010],n;
void solve()
{
int ans=1;
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
s.clear();
for (int i=1;i<=n;i++)
if (s.count(a[i]))
{
s.clear();
ans++;
s.insert(a[i]);
}
else s.insert(a[i]);
printf("%d\n",ans);
}
int main()
{
int T;
scanf("%d",&T);
while (T--) solve();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心