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的更多相关文章

  1. HDU 1029 Ignatius and the Princess IV --- 水题

    HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...

  2. 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 ...

  3. 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 ...

  4. hdu 1029 Ignatius ans the Princess IV

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  5. [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 ...

  6. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

    Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32767 K ( ...

  7. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  8. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

  9. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. MVC4多语言IHttpModule实现

    最近项目需要多语言环境了. 由于项目页面较多,逐个Action去读取资源文件不大现实.就想到了使用 IHttpModule配合MVC的路由规则来实现. 首先创建以个mvc4的应用程序,添加资源文件夹( ...

  2. [转载]test后跟je

    今天俺也用OD(OllyDbg)反汇编了个小软件,其中里面有下面两条指令: 没太明白什么意思,google一下,在看雪论坛上发现了一个大虾的解释很详细,记录一下: 1.test a,b 是a与b相与的 ...

  3. 团体程序设计天梯赛-练习集L1-013. 计算阶乘和

    L1-013. 计算阶乘和 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 对于给定的正整数N,需要你计算 S = 1! + 2 ...

  4. IsBadReadPtr|IsBadWritePtr调试崩溃

    遇到一未找到必然出现条件的崩溃,不知道什么时候能触发崩溃,崩溃dump显示,试图访问了非法的内存或者写入了非法的内存 此时如下两个函数就比较有用了: BOOL WINAPI IsBadReadPtr( ...

  5. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  6. jstack(查看线程)、jmap(查看内存)和jstat(性能分析)

    公司内部同事分享的一篇文章 周末看到一个用jstack查看死锁的例子.昨天晚上总结了一下jstack(查看线程).jmap(查看内存)和jstat(性能分析)命令.供大家参考 1.Jstack 1.1 ...

  7. linux下添加PATH环境变量

    添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...

  8. SPRING IN ACTION 第4版笔记-第九章Securing web applications-005-Applying LDAP-backed authentication

    一. 1.This method is the  LDAP analog to  jdbcAuthentication() @Override protected void configure(Aut ...

  9. ArcGIS学习记录—Arcgis中点、线、面的相互转换方法

    本文使用的工具在Arctoolbox.Data Management Tools.Features (一)面--面转线.面转点 面转线  Polygon To Line .Feature To Lin ...

  10. Android Paint中setTextSize

    界面适配的时候发现Paint.setTextSize与TextView.setTextSize传入的单位不一致.Paint.setTextSize传入的单位是px,TextView.setTextSi ...