[kuangbin带你飞]专题十二 基础DP1 B - Ignatius and the Princess IV HDU - 1029

题目描述

"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.

"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.

"But what is the characteristic of the special integer?" Ignatius asks.

"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.

Can you find the special integer for Ignatius?

Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input

5
1 3 2 3 3
11
1 1 1 1 1 5 5 5 5 5 5
7
1 1 1 1 1 1 1

Sample Output

3
5
1

Sponsor
题目的意思很简单,给定n个数,n是奇数,这n个数中,有一个数字出现的次数是大于(n+1)/2的。我们的目标就是找到这个数。

思路讲解

这道题有两种思路。
解法一
第一种思路容易想到。我们先给这组数进行排序,由于要找的这个数字出现的次数大于(n+1)/2。那么n/2位置的数字肯定是待求的数字。
这种方法的时间复杂度是O(nlog(n))。
解法二
第二种思路就比较难想到。但这种思路似乎和动态规划也没什么关系,所以这道题为什么会出现在动态规划的习题里就很让人疑惑。第二种思路是一种消除的思路。我们使用一个变量times来记录连续出现相同数字的次数。比如前一个数字是1,现在的数字又是1,那么times就加1,如果前一个数字和当前数字不一样,那么就times减1,当times减为1的时候,说明前面连续出现相同数字的累积次数减完了,这个时候就换用当前数字作为连续出现数字。因为要求的数字的个数是超过一半的,所以,不管数字怎么排列,最后肯定会得到代求数字的。
这种方法时间复杂度为O(n)。

AC代码

解法一

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int s[1000005];
int d[1000005];
int pre_d[1000005];
int main()
{
	int i,j,k,max_pre,max_now;
	while(scanf("%d%d",&m,&n)!=EOF)
	{
		memset(d,0,sizeof(d));
		memset(pre_d,0,sizeof(pre_d));
//		cout<<m<<"**"<<endl;
//		cout<<n<<"***"<<endl;
		for(i =1;i<=n;i++)
		{
			scanf("%d",&s[i]);
		}
//		pre_d[1] = a[1];
		for(i = 1;i<=m;i++)
		{
			
//			max_pre = 0;
			max_now = 0;
			for(k = 1;k<=i;k++) max_now +=s[k];
			max_pre = pre_d[i];
			d[i] = max_now;
			for(j = i+1;j<=n;j++)
			{
				d[j] = max(d[j-1],max_pre)+s[j];
				max_pre = max(max_pre,pre_d[j]);
				max_now = max(max_now,d[j]);
				pre_d[j] = max_now;
				
			}
		}
		cout<<max_now<<endl;
		
	}

 } 

解法二

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[999999];
int main()
{
	int n;
	int i,j;
	while(scanf("%d",&n)!=EOF)
	{
		int times = 0;
		int cmp_num;
		for(i = 0;i<n;i++) scanf("%d",&a[i]);
		cmp_num = a[0];
		times = 1;
		for(i = 1;i<n;i++)
		{
			if(cmp_num!=a[i]) 
			{
				if(times==1) cmp_num = a[i];
				else times--;
			}
			else times++; 
		}
		cout<<cmp_num<<endl;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值