HDU1029 - Ignatius and the Princess IV【水题】
给你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【水题】的更多相关文章
- HDU1029 Ignatius and the Princess IV (水题)
<题目链接> 题目大意:给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个. 解题分析: 本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙. ...
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- kuangbin专题十二 HDU1029 Ignatius and the Princess IV (水题)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [HDU1029]Ignatius and the Princess IV<桶 水题>
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029 题目大意: 多组数据,每组数据先给一个n,然后给n各数字,找出n各数字中出现了至少(n+1)/2 ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- 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 ...
- (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 ...
- HDOJ.1029 Ignatius and the Princess IV(map)
Ignatius and the Princess IV 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出一个奇数n,下面有n个数,找出下面数字中出现次数大于(n+1)/2的数字,并输出. ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
随机推荐
- springmvc 解析xml数据
springmvc 解析xml数据 http://blog.csdn.net/zhi_jun/article/details/37925475
- 小米红米1S 电信/联通版 专用TWRP2.8.1.1中文版 (全屏触摸/支持MTP挂载内外置存储)
转载请注明出处和链接: http://blog.csdn.net/syhost/article/details/39340477 说说中文TWRP的简史: 中文TWRP是本人在201 ...
- javascript 打印函数名称和被引用的函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- bzoj4397【Usaco2015 Dec】Breed Counting
4397: [Usaco2015 dec]Breed Counting Time Limit: 10 Sec Memory Limit: 128 MB Submit: 29 Solved: 25 ...
- 使用playonlinux安装windows软件
转载 http://qspy.is-programmer.com/posts/40913.html Wine提供了一个用来运行Windows程序的平台.PlayOnLinux 是使用 Python 写 ...
- linux中两个缓冲区
不同于Windows,Linux系统里存在两个剪切板:一个叫做选择缓冲区(X11 selection buffer),另一个才是剪切板(clipboard). 01)选择缓冲区(缓冲内容在其他位置可用 ...
- B3403 [Usaco2009 Open]Cow Line 直线上的牛 deque
deque真的秀,queue和stack...没啥用了啊.操作差不多,就是在前面加一个front||back_就行了. 题干: 题目描述 题目描述 约翰的N只奶牛(编为1到N号)正在直线上排队 ...
- heap堆&&priority_queue优先队列
堆(heap)不是stl中的东西...它分为 max heap 和min heap. 但我不想用这些,而是采用了priority_queue,优先队列,定义在queue中.顾名思义,它的作用就是无论怎 ...
- 杂项-Java:JDBC
ylbtech-杂项-Java:JDBC JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访 ...
- selenium3 + python - select定位
一.Select模块(index) 1.导入Select模块.直接根据属性或索引定位 2.先要导入select方法:from selenium.webdriver.support.se ...