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",& ...
随机推荐
- mybatis 缓存的使用, 看这篇就够了
目录 1 一级缓存 1.1 同一个 SqlSession 1.2 不同的 SqlSession 1.3 刷新缓存 1.4 总结 2 二级缓存 2.1 配置二级缓存 2.2 使用二级缓存 2.3 配置详 ...
- Jmeter(二十八)_Docker+Jmeter+Gitlab+Jenkins+Ant(容器化的接口自动化持续集成平台)
这套接口自动化持续集成环境已经部署差不多了,现在说说我的设计思路 1:利用Docker容器化Gitlab,Jenkins,Jmeter,Ant,链接如下 Docker_容器化gitlab Docker ...
- [朴孝敏][Ooh La La]
歌词来源:http://music.163.com/#/song?id=484058960 作曲 : Damon Sharpe/Jimmy Burney/Adam Kapit [作曲 : Damon ...
- Command Analyze failed with a nonzero exit code
在运行RN项目的时候,报 Command Analyze failed with a nonzero exit code ,试着将build System 修改下
- combox的基本应用
easyui-combox:控件的初始化: 可以在其中进行文字的筛选功能(过滤), 动态加载数据的方法. <!DOCTYPE html><html lang="en&quo ...
- PairProject 总结
结对编程人员:张迎春,赵梓皓.下面是我们一起编程的照片. 结对编程的优点: 首先,结对编程的目的是为了减少编程的错误,在编程的时候,大家一起检查错误,一起分析有没有更加合理的编写方法,所以这是结对编程 ...
- Practice2 结对子之“小学四则运算”
开发环境:Eclipse,js,css,html 程序完成的方向: 1.可以出表达式里含有负整数(负整数最小不小于-100)的题目,且负数需要带括号,用户输入的结果不用带括号.如: 2*(-4) = ...
- #Leetcode# 788. Rotated Digits
https://leetcode.com/problems/rotated-digits/ X is a good number if after rotating each digit indivi ...
- [读书笔记]Linux命令行与shell编程读书笔记04 安装软件,编辑器注意事项
1. debian以及redhat两种主流的linux发行版用的包管理工具 debian的包管理工具是 dpkg 再现安装的是 apt apt的工具主要有 apt-get apt-cache apti ...
- Linux 重启网络提示找不到eth0(no device found for “System eth0”)
一.背景 使用VMWare创建了一个虚拟机(VM1),然后通过拷贝的方式创建了另一台虚拟机(VM2).在第二台虚拟机上设置网卡为固定IP,使用service network restart重启网络的时 ...