HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题
这题,有很多种解法。
第一种:
直接比较每个数出现次数。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
const int maxn=;
int a[maxn];
int b[maxn];
int main()
{
int n; while(~scanf("%d",&n))
{
clc(b,);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++;
}
int maxx=-;
int ans;
for(int i=;i<;i++)
{
if(maxx<b[i])
{
maxx=b[i];
ans=i;
}
}
printf("%d\n",ans);
}
return ;
}
第二种:
应为输出至少(n+1)/2次的数,所以,最后result一定是要求的数。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
const int maxn=;
int a[maxn];
int b[maxn];
int main()
{
int n,i;
int t;
int cnt;
int result;
while(scanf("%d",&n)!=EOF)
{
cnt=;
for(i=; i<n; i++)
{
scanf("%d",&t);
if(cnt==)
{
cnt=;
result=t;
}
else
{
if(t==result)cnt++;
else cnt--;
}
}
printf("%d\n",result);
}
return ;
}
HDU 1029 Ignatius and the Princess IV DP的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)
HDU 1029 Ignatius and the Princess IV (思维题,排序?) Description "OK, you are not too bad, em... But ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- 【转载】test和cmp比较
标 题:test和cmp一个很菜很基础的话题! 作 者: FTBirthday 时 间:2003/05/19 01:14am 链 接:http://bbs.pediy.com 看过破解教程,都知道te ...
- The 7th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Wu Xing
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3328 至今未看懂题意,未编译直接提交,然后 A了.莫名AC总感觉怪怪的. ...
- vc2008程序发布指南
vc2008程序发布指南 2008-05-03 17:46 vc2008开发的程序的发布方式可以有5种方式: 1. 采用静态链接到crt和MFC. 只要你拥有组成程序的所有源代码,你就可以采用这种方式 ...
- Maven Source jar
http://blog.csdn.net/symgdwyh/article/details/4407945
- DJANGO的ORM的Q查询作多字段外键的模糊查询样码
工作中用到的,存照一下. from django.db.models import Q if self.kwargs.has_key('search_pk'): search_pk = self.kw ...
- PowerDesigner将name自动添加到Comment注释的方法 VB代码
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl ' the current model ' get t ...
- http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html
http://www.cnblogs.com/amboyna/archive/2008/03/08/1096024.html
- codeforces #310 div1 D
一开始写了个暴力模拟绳子的摆动轨迹 然后在Test 16 T掉了 后来%了一下别人的代码,发现需要对特殊情况进行特殊处理 首先我们考虑绳子的向右摆动,设当前位置为p,绳子当前长度为L 如果其旋转中心位 ...
- SSH应该使用密钥还是密码?
关于SSH,几乎每个人都同意密钥要优于密码,更安全,并且更先进,但我并不同意这个观点. 虽然密钥的确可以更好,但它有着还没被意识到的严重风险,并且我认为比得到妥善管理的密码更不安全. 通常密钥更好的理 ...
- 统计学习方法笔记--EM算法--三硬币例子补充
本文,意在说明<统计学习方法>第九章EM算法的三硬币例子,公式(9.5-9.6如何而来) 下面是(公式9.5-9.8)的说明, 本人水平有限,怀着分享学习的态度发表此文,欢迎大家批评,交流 ...