题目链接: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. matlab_legend_使用

    matlab legend 使用 用Matlab画图时,有时候需要对各种图标进行标注,例如,用“+”代表A的运动情况,“*”代表B的 运动情况.legend函数的基本用法是LEGEND(string1 ...

  2. springJDBC实现查询

    其实在Spring这个框架中,提供了一些对JDBC访问数据库的封装,其中JdbcTemplate就是一个很好用的类,下面来 演示一下这个类的一些用法.首先需要导入commons-logging.jar ...

  3. nodejs具体解释

    文件夹 javascript与node.js     javascript与你     因为javascript真正意义上有两种,甚至能够说是三种形态(从最早的作为DHTML进行增强的小工具,到像jQ ...

  4. C++ operator关键字

    operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...

  5. php扩展安装

    [root@129-2-10-2 src]# cat kuozhan.sh #!/bin/bash###install redis extend #########cd /usr/local/srct ...

  6. Entity Framework(三):使用特性(数据注解)创建表结构

    一.理解Code First及其约定和配置 传统设计应用的方式都是由下而上的,即我们习惯优先考虑数据库,然后使用这个以数据为中心的方法在数据之上构建应用程序.这种方法非常适合于数据密集的应用或者数据库 ...

  7. Mongodb的性能优化问题

    摘要 数据库性能对软件整体性能有着至关重要的影响,对于Mongodb数据库常用的性能优化方法主要有: 范式化与反范式化: 填充因子的使用: 索引的使用: 一. 范式化与反范式化 范式是为了消除重复数据 ...

  8. springcloud微服务架构搭建

    SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...

  9. ThinkPHP整合cropper剪裁图片上传功能

    1.先下载核心文件:https://github.com/fengyuanchen/cropper 2. 3.对于index.html文件 4.对于main.js文件 5.对于crop.php文件 & ...

  10. Apache里的httpd-vhosts.conf详解

    首先看下面的配置: <VirtualHost *:80> ServerAdmin webmaster@dummy-host.example.com DocumentRoot "D ...