HDU 6019 MG loves gold

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 n elements, which has its own color C 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 <dl><dd> The first line is an integer T T which indicates the case number.( 1<=T<=10 1<=T<=10)

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

Then there are n n integers C C in the next line, the x-th integer means the x-th gold’s color( |C|<=2000000000 |C|<=2000000000). </dd> Output <dd> 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 n golds. </dd> Sample Input <dd>
2
5
1 1 2 3 -1
5
1 1 2 2 3
</dd> Sample Output <dd>
2
3
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<set>

using namespace std;

int road[100005];

void getans(int n)
{
	set<int>set1;
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		if (!set1.empty())
		{
			if (set1.count(road[i]))
			{
				ans++;
				set1.clear();
				set1.insert(road[i]);
				continue;
			}
			else
			{
				set1.insert(road[i]);
			}
		}
		else
		{
			set1.insert(road[i]);
		}
	}
	ans += 1;
	cout << ans << endl;
}

int main()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
		{
			scanf("%d", &road[i]);
		}
		getans(n);
	}
	return 0;
}

// BY T P </dd></dl>
全部评论

相关推荐

08-25 08:20
已编辑
Java
投递诚迈科技等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务