HDU——1029 Ignatius and the Princess IV (STL/dynamic programming)

Link to the original question: http://acm.hdu.edu.cn/showproblem.php?pid=1029

Insert picture description here
Test sample

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

Question: Give you an odd NNN length integer sequence, you need to find out one of the elements, it appears at least(N + 1) / 2 (N+1)/2(N+1 ) / 2 times.

Problem-solving ideas: here we have two ways to solve it. Let me talk about the popular solution first,That is to use the map container to solve, we record each input and judge it to meet the requirements. Store the values ​​that meet the requirements,The final output can be. It's easy to understand. Then another method is dynamic programming. This method is very clever. We must first determine the fact that only one element that meets the requirements can appear in this integer sequence. Because it appears at least (N + 1) / 2 (N+1)/2(N+1 ) / 2 times. So we can set this element asresultr e s u l t , then the number of times it appears iscnt cntc n t . Think about it again, in the first2k 2k2 k numbers, if none of the numbers appear more thankkk , then this number must be in the remainingN − 2 k N-2kNAppears in 2 k numbers. Therefore, we can judge that when the input value is not equal to the element we assumed at the time, then the number of occurrences is reduced, and if it is, then it is increased. The purpose of this is to judge whether there is an element that can appear more than half during the culling process. So we havecnt=0 cnt=0cnt=At 0 we need to change our assumed value because it is no longer possible. OK, look at the code specifically.

STL AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n,temp;
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>n){
    
    
		int result;
		map<int,int> p;
		rep(i,0,n-1){
    
    
			cin>>temp;
			if(++p[temp]>=(n+1)/2){
    
    
				result=temp;
			}
		}
		cout<<result<<endl;
	}
	return 0;
}

Dynamic programming AC code

/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int n,temp,result,cnt;
int main(){
    
    
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>n){
    
    
		cnt=0;
		rep(i,1,n){
    
    
			cin>>temp;
			if(cnt==0){
    
    
				result=temp;
				cnt++;
			}
			else{
    
    
				if(temp==result){
    
    
					cnt++;
				}
				else{
    
    
					cnt--;
				}
			}
		}
		cout<<result<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/hzf0701/article/details/108986171