kuangbin专题十二 HDU1029 Ignatius and the Princess IV

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1029

被专题的第一题吓到了,以为还是一道难题,就复杂考虑了。排序,然后

dp[i] = (a[i] == a[i-1]) ? dp[i-1] + 1 : 1

后来才发现必定有解,直接输出就可以了

AC代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1000010;
int a[maxn];
int dp[maxn];

int main() {
	freopen("in.txt", "r", stdin);
	int n;
	while(~scanf("%d", &n)) { 
		memset(dp, 0, sizeof(dp));
		for(int i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		if(n == 1) {//特判了一下
			printf("%d\n", a[0]);
			continue;
		}
		sort(a, a+n);
		dp[0] = 1;
		int maxdp = dp[0], pos = -1;
		for(int i = 1; i < n; i++) {
			dp[i] = (a[i] == a[i-1]) ? dp[i-1] + 1 : 1;
			if(dp[i] > maxdp) {
				maxdp = dp[i];//最大值的dp,对应出现最多的数
				pos = i;
			}
		}
		printf("%d\n", a[pos]);
	}
}


猜你喜欢

转载自blog.csdn.net/qq_40932661/article/details/81415109