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",& ...
随机推荐
- HAProxy 日志输出及配置
正所谓,没有软件敢说没有bug,人无完人,software is not perfect software.是软件就可能存在bug,那么如果出现bug,我们就要分析对我们业务的影响及可能如何避免bu ...
- Terraform:简介
在 DevOps 实践中,基础设施即代码如何落地是一个绕不开的话题.像 Chef,Puppet 等成熟的配置管理工具,都能够满足一定程度的需求,但有没有更友好的工具能够满足我们绝大多数的需求?笔者认为 ...
- libmysqlclient.so.16: cannot open shared object file: No such file or directory
编译安装的mysql5.6.39,安装目录是/usr/local/mysql,启用程序时报错:libmysqlclient.so.16: cannot open shared object file: ...
- open-falcon ---客户机agent操作
open-falcon的agent用于采集机器负载监控指标,比如cpu.idle.load.1min.disk.io.util等等,每隔60秒push给Transfer.agent与Transfer建 ...
- 第三个sprint冲刺第一阶段
- Java的常用命令javac与java
javac 可以使用javac -h来查看常用的命令: -> ~ # javac -help 用法: javac <options> <source files> 其中, ...
- #Leetcode# 977. Squares of a Sorted Array
https://leetcode.com/problems/squares-of-a-sorted-array/ Given an array of integers A sorted in non- ...
- HDU 2096 小明A+B
http://acm.hdu.edu.cn/showproblem.php?pid=2096 Problem Description 小明今年3岁了, 现在他已经能够认识100以内的非负整数, 并且能 ...
- hadoop集群故障排除
故障一:某个datanode节点无法启动 我是以用户名centos安装和搭建了一个测试用的hadoop集群环境,也配置好了有关的权限,所有者.所属组都配成centos:centos [故障现象] 名称 ...
- Performance testing test scenarios
1 check if page load time is within acceptable range2 check page load on slow connections 3 check re ...