给你n个数字,请你找出出现至少(n+1)/2次的数字。

输入

本题包含多组数据,请处理到EOF:
每组数据包含两行。
第一行一个数字N(1<=N<=999999) ,保证N为奇数。
第二行为N个用空格隔开的整数。

输出

对于每组数据,输出一行,表示要求找到的那个数

样例输入

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

样例输出

3
5
1

思路:第一种思路是将数组排序,由于其出现(n+1)/2   次,故中位数一定是要找的那个数。时间复杂度O(nlogn)

#include<cstdio>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int maxn=1000005;
int a[maxn],b[maxn];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
//int t;
for(int i=0;i<n;++i)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
printf("%d\n",a[(n-1)/2]);
}
return 0;
}

另一种就是网上题解中比较常见的了,不得不说思路确实很棒,复杂度O(n)

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std; const int N = 1000005;
int a[N]; int main()
{
int n;
while(~scanf("%d", &n))
{
int num = 0;
int ans = -1;
for(int i=0; i<n; i++)
{
scanf("%d", &a[i]);
if(num == 0)
{
++num;
ans = a[i];
}
else
{
if(ans != a[i])
num--;
else
num++;
}
}
printf("%d\n", ans);
}
return 0;
}

HDU1029 - Ignatius and the Princess IV【水题】的更多相关文章

  1. HDU1029 Ignatius and the Princess IV (水题)

    <题目链接> 题目大意:给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个. 解题分析: 本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙. ...

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

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

  3. kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)

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

  4. [HDU1029]Ignatius and the Princess IV<桶 水题>

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...

  5. 【HDU - 1029】Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV  先搬中文 Descriptions:   给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...

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

  7. (Arrays.sort() 或 map) Ignatius and the Princess IV hdu1029

    Ignatius and the Princess IV 链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 借鉴链接:https://blog.csd ...

  8. HDOJ.1029 Ignatius and the Princess IV(map)

    Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...

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

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

随机推荐

  1. Eclipse ADT 导入别的电脑开发的项目

    用Eclipse开发的时候常常要导入别的电脑开发的项目,常常会出错,甚至导入不了. 方法一: 把你正在使用的Eclipse开发的随便一个项目.打开,把下图这三个文件复制过去你要导入的项目.覆盖.然后再 ...

  2. 向Java枚举类型中加入新方法

    除了不能继承enum之外,可将其看做一个常规类.甚至能够有main方法. 注意:必须先定义enum实例.实例的最后有一个分号. 以下是一个样例:返回对实例自身的描写叙述,而非默认的toString返回 ...

  3. 一个基于cocos2d-x 3.0和Box2d的demo小程序

    p图demo小应用.想怎么p就怎么p 本文參考于http://blog.csdn.net/xiaominghimi/article/details/6776096和http://www.cnblogs ...

  4. Swift基础(类,结构体,函数)

    import Foundation // 创建一个类 class Student { // 属性(类的属性必须赋初值,如果不赋值,需要写自定义方法) var studentName: String v ...

  5. 怎样使用ListView实现一个带有网络请求,解析,分页,缓存的公共的List页面来大大的提高工作效率

    在寻常的开发中常常会有非常多列表页面.每做一个列表页就须要创建这个布局文件那个Adapter适配器文件等等一大堆与之相关的附属的不必要的冗余文件. 假设版本号更新迭代比較频繁,如此以往,就会使项目pr ...

  6. VBS 控制语句

    1.if...then...end if if [条件] then [执行语句] end if 可以嵌套 多个if if [条件] then [执行语句] else if [条件] then [执行语 ...

  7. 使用Genymotion无法连接网络设置代理

    A.) Genymotion 的 Proxy 设置 , 在Android的设置 -> 无线网络 -> Wi-Fi 之中 1.) 在 设置 -> 无线网络 -> Wi-Fi 里面 ...

  8. linux驱动由浅入深系列:tinyalsa(tinymix/tinycap/tinyplay/tinypcminfo)音频子系统之一【转】

    本文转载自:http://blog.csdn.net/radianceblau/article/details/64125411 目前linux中主流的音频体系结构是ALSA(Advanced Lin ...

  9. PHPExcel 使用心得

    最近有个项目需要将数据导出成excel,以前都没接触过.便google一下知道了PHPExcel 这个据说很强大的类库. 写这个博文主要目的等同于做笔记 1首先看了下官方的文档.http://phpe ...

  10. 比较两个Json对象是否相等

    一个前端同事遇到的面试题,抽空写了写,也算是个积累 1.先准备三个工具方法,用于判断是否是对象类型,是否是数组,获取对象长度 function isObj(object) { return objec ...