给你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. h5 播放器 -2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  2. Java UDP通信简单实现

    1.Java实现方式 1)server端 /** * UDPserver端 * */ public class UdpServer { // 定义一些常量 private final intMAX_L ...

  3. C语言里全局变量管理

    C语言里信息封装比較弱,仅仅有静态变量的文件作用域. 假设不加约束.非常easy造成全局变量满天飞. 假设定义一个全局结构体.把全局变量都放到这个GlobleVariate里,应该好管一些,至少比裸奔 ...

  4. 《Java编程思想》笔记

    第十章 (1)当生成一个内部类的对象时.此对象 与制造他的外围对象之间就有了一种联系,所以它能訪问其外围对象的全部成员,而不须要不论什么特殊条件. 此外,内部类还拥有其它外围类的全部元素的訪问权. ( ...

  5. ambarella H2 添加文件到ext4文件系统

    方法1: ambarella/rootfs目录下有skeleton(骨架)目录,此目录下就是文件系统的各个目录, [root@jz4775dev]# ls skeleton/ bin debug de ...

  6. B3038 上帝造题的七分钟2 线段树

    这就是一道变得比较奇怪的线段树,维护每个区间的最大值和区间和,然后关键在于每次取根号的话数值下降的特别快,不用几次就都是1了,所以每次暴力单点修改,然后直接找区间最大值,假如区间最大值是1的话,就直接 ...

  7. Constructing Roads(spfa)

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2493 #include <stdio.h ...

  8. xml转换成数组array

    直接上代码,成功转换 if($data){ //返回来的是xml格式需要转换成数组再提取值,用来做更新 $startnum = strpos($data,"<xml>" ...

  9. jQuery考试之错题分析

    获取元素范围大小顺序依次为: $(#one).siblings("div")>$("#one~div")>$("#one +div&quo ...

  10. .Net Core(二) 下

    接上面 http://www.cnblogs.com/xcodevs/p/5584218.html 在解决方案浏览器中,右击 Controllers 目录.选择添加>新建项.选择Web API控 ...