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 you can never pass the next test." feng5166 says.
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
Input
The input contains several test cases. Each test case contains two lines. The first line consists of an odd integer N(1<=N<=999999) which indicate the number of the integers feng5166 will tell our hero. The second line contains the N integers. The input is terminated by the end of file.
Output
For each test case, you have to output only one line which contains the special number you have found.
Sample Input
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
Sample Output
3
5
1
Http
https://vjudge.net/problem/HDU-1029
Source
思维题,排序?
题目大意
给一个保证个数n为奇数个的数列,求里面那个出现了至少n/2+1次的数
解决思路
因为我们要输出的数出现了至少n/2+1次,所以我们用一个cnt记录当前某个数的出现次数(看不懂?没关系,往下看)。每读入一个数,我们看一下当前的cnt是否是0,如果是0,则把Ans更新为当前读入的数并把cnt置为1,否则,如果Ans与当前读入的数相同,则cnt+1,否则-1。
这个算法的正确性在于因为我们要输出的Ans的次数是大于等于n/2+1的,也就是说它出现的次数是大于其他所有数出现次数之和的。所以我们利用上面的方法,可以保证最后cnt记录的就是Ans。
另:强烈建议本题扩大数据范围,因为这题还可以用sort水过。因为要输出的数出现了至少n/2+1次,所以只要把所有的数排一边序,可以保证第n/2+1个数就是答案。
2017.8.26 Update
原代码在BZOJ上会MLE,原因是使用了std。请看最简化版
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
int n;
while (cin>>n)
{
int cnt=0,Ans=0;
for (int i=1;i<=n;i++)
{
int number;
scanf("%d",&number);
if (cnt==0)
{
cnt=1;
Ans=number;
continue;
}
if (number==Ans)
cnt++;
else
cnt--;
}
printf("%d\n",Ans);
}
return 0;
}
最简化版
#include<cstdio>
int main()
{
int n;
scanf("%d",&n);
int cnt=0,Ans=0;
for (int i=1;i<=n;i++)
{
int number;
scanf("%d",&number);
if (cnt==0)
{
cnt=1;
Ans=number;
continue;
}
if (number==Ans)
cnt++;
else
cnt--;
}
printf("%d\n",Ans);
return 0;
}
sort暴力方法:
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxN=1000000;
const int inf=2147483647;
int n;
int Arr[maxN];
int main()
{
while (cin>>n)
{
for (int i=1;i<=n;i++)
scanf("%d",&Arr[i]);
sort(&Arr[1],&Arr[n+1]);
printf("%d\n",Arr[n/2+1]);
}
return 0;
}
为什么这题会出现在[kuangbin带你飞]的专题十二 基础DP1里?很迷……

HDU 1029 Ignatius and the Princess IV / HYSBZ(BZOJ) 2456 mode(思维题,~~排序?~~)的更多相关文章
- HDU 1029 Ignatius and the Princess IV --- 水题
HDU 1029 题目大意:给定数字n(n <= 999999 且n为奇数 )以及n个数,找出至少出现(n+1)/2次的数 解题思路:n个数遍历过去,可以用一个map(也可以用数组)记录每个数出 ...
- HDU 1029 Ignatius and the Princess IV (map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1029 Ignatius and the Princess IV Time Limit: 2000/10 ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- [ACM] hdu 1029 Ignatius and the Princess IV (动归或hash)
Ignatius and the Princess IV Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32767K (Ja ...
- HDU 1029 Ignatius and the Princess IV (动态规划、思维)
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)
此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...
- HDU 1029 Ignatius and the Princess IV
解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...
- HDU 1029 Ignatius and the Princess IV DP
kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...
- HDU 1029 Ignatius and the Princess IV(数论)
#include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...
随机推荐
- 【JVM.11】Java内存模型与线程
鲁迅曾经说过“并发处理的广泛应用是使得Amdahl定律代替摩尔定律成为计算机性能发展源动力的根本原因,也是人类‘压榨‘ 计算机运行能力的最有力武器.” 一.概述 多任务处理在现代计算机操作系统中几乎已 ...
- mac系统下修复第三方Python包bug
发现问题 今天在github上fork了CI 3.x的中文手册,按照README文档一步步进行Sphinx和相关工具的安装,最终build生成html版手册.操作到第6步执行`make html`的时 ...
- android——error opening trace file: No such file or directory (2)
1.疑惑: 程序运行起来的时候日志总是显示下面这个错误,但是不影响程序的正常进行,我是用真机来测试的,android4.4.4(API17). 02-11 14:55:03.629 15525-155 ...
- <构建之法>第11、12章
第11章软件设计与实现 主要讲了典型的开发流程和开发阶段的一些管理方法 问题: 从spec道实现是代码的实现吗? 第12章 用户体验 主要讲了用户体验的各种角度和认识阻力登 问题: 用户的体验是设计前 ...
- scipy的一些函数名
rvs:随机变量pdf:概率密度函数cdf:累计分布函数sf:残存函数(1-CDF)ppf:分位点函数(CDF的逆)isf:逆残存函数(sf的逆)stats:返回均值,方差,(费舍尔)偏态,(费舍尔) ...
- TCP系列11—重传—1、TCP重传概述
在最开始介绍TCP的时候,我们就介绍了TCP的三个特点,分别是面向连接.可靠.字节流式.前面内容我们已经介绍过了TCP的连接管理,接下来的这部分内容将会介绍与TCP可靠性强关联的TCP重传. 很多网络 ...
- Tools (StExBar vs Cmder)which can switch to command line window on context menu in windows OS
https://tools.stefankueng.com/StExBar.html https://github.com/cmderdev/cmder
- 转《js闭包与内存泄漏》
首先,能导致内存泄漏的一定是引用类型的变量,比如函数和其他自定义对象.而值类型的变量是不存在内存泄漏的,比如字符串.数字.布尔值等.因为值类型是靠复制来传递的,而引用类型是靠类似c语言中的指针来传递的 ...
- MidoNet 安装(Kilo RDO)(最老版)
一.初始化(Kilo RDO)版(最老版) 环境介绍 系统 CentOS 7.3 主机ip 10.211.55.5 主机名 midonet 1.配置yum源 ###OpenStack源 yum ins ...
- list1与list2求交集的方法总结!
一.有序集合求交集的方法有 a)二重for循环法,时间复杂度O(n*n) b)拉链法,时间复杂度O(n) c)水平分桶,多线程并行 d)bitmap,大大提高运算并行度,时间复杂度O(n) e)跳表, ...