kuangbin专题十二 HDU1029 Ignatius and the Princess IV

本文提供了一种解决HDU 1029问题的有效方法,通过排序和动态规划来找到出现次数最多的数,并给出了完整的AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目: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]);
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值