题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029

就是给你n(n是奇数)个数找出个数大于(n+1)/ 2 的那个数;

n的取值范围是 n(1<=n<=999999)

这是很久之前做的一道题了,当时就是用sort排序,输出a[(n+1)/2]就行了,在这道题上很容易就过了,但是如果说n很大的大到无法开数组呢,所以有搜了一下别人的题解;

我们可以知道结果的那个数的个数一定大于总个数的一半(关键),所以我们可以线性的做,当cnt=0的时候更新ans;否则当当前数为ans的时候让cnt++,不相等时就让cnt--;

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std; #define N 1000000 int main()
{
int n, num, cnt, ans;
while(scanf("%d", &n)!=EOF)
{
cnt = ;
for(int i=; i<n; i++)
{
scanf("%d", &num);
if(cnt==)
ans = num, cnt++;
else
{
if(num==ans)
cnt++;
else
cnt--;
}
}
printf("%d\n", ans);
}
return ;
}

排序的(n*log(n)):

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
using namespace std; #define N 1000000 int a[N]; int main()
{
int n;
while(scanf("%d", &n)!=EOF)
{
for(int i=; i<n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
printf("%d\n", a[(n+)/]);
}
return ;
}

Ignatius and the Princess IV---hdu1029(动态规划或者sort)的更多相关文章

  1. (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 ...

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

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

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

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

  6. hdu 1029 Ignatius ans the Princess IV

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

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

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

  8. Ignatius and the Princess IV

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

  9. Ignatius and the Princess IV(乱搞一发竟然过了)

    B - Ignatius and the Princess IV Time Limit:1000MS     Memory Limit:32767KB     64bit IO Format:%I64 ...

随机推荐

  1. Vue 状态管理

    类flux状态管理的官方实现 由于多个状态分散的跨越在许多组件和交互间的各个角落,大型应用复杂度也经常逐渐增长. 为了解决这个问题,vue提供了vuex:我们有收到elm启发的状态管理库,vuex甚至 ...

  2. 在sql结果中显示行号

    1.准备 create table newtable ( name ), ) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ); ); ); ); ); 2.实现 ) a ...

  3. ldap 使用 问题参考

    Q2.ldapsearch查询一个有30000多条记录时出现:Size limit exceeded 4 A2:服务器端配置文件有sizelimit 1000的限制!用管理员身份查询-D"c ...

  4. 一款纯html5实现的时钟

    今天给大家分享一款非常漂亮的纯html5实现的时钟.整个界面都由html5绘制而成.一起看下效果图: 在线预览   源码下载 实现的代码. html代码: <div class="co ...

  5. The 14th UESTC Programming Contest Final B - Banana Watch 预处理、前缀和

    B - Banana Watch Time Limit: 1000/1000MS (Java/Others)     Memory Limit: 262144/262144KB (Java/Other ...

  6. C语言 · 数组查找及替换

    算法训练 数组查找及替换   时间限制:1.0s   内存限制:512.0MB      问题描述 给定某整数数组和某一整数b.要求删除数组中可以被b整除的所有元素,同时将该数组各元素按从小到大排序. ...

  7. #!/bin/sh与#!/bin/bash的区别

    Linux 中的 shell 有很多类型,其中最常用的几种是: Bourne shell (sh).C shell (csh) 和 Korn shell (ksh), 各有优缺点.Bourne she ...

  8. linux -- Ubuntu network-manager

    [Server版本] 在UbuntuServer版本中,因为只存有命令行模式,所以要想进行网络参数设置,只能通过修改/etc/network/interfaces.具体设置方法如下: (1) Ubun ...

  9. 【BZOJ】1176: [Balkan2007]Mokia(cdq分治)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1176 在写这题的时候思维非常逗啊........2333................... 最后 ...

  10. 纪念伟大的sb错-noip滚粗

    人弱就是弱,被sb错虐翻. 手一抖一生就毁了 开此博文纪念这个伟大的sb错! noip2014 d2t2逆bfs后删点手残没考虑后效性,完美爆80 愿省选rp++,求进noi,orz