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(思维题,~~排序?~~)的更多相关文章

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

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

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

  3. hdu 1029 Ignatius ans the Princess IV

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

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

  5. HDU 1029 Ignatius and the Princess IV (动态规划、思维)

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

  6. HDOJ/HDU 1029 Ignatius and the Princess IV(简单DP,排序)

    此题无法用JavaAC,不相信的可以去HD1029题试下! Problem Description "OK, you are not too bad, em- But you can nev ...

  7. HDU 1029 Ignatius and the Princess IV

    解题报告: 题目大意:就是要求输入的N个数里面出现的次数最多的数是哪一个,水题.暴力可过,定义一个一位数组,先用memset函数初始化,然后每次输入一个数就将下标对应的上标对应的那个数加一,最后将整个 ...

  8. HDU 1029 Ignatius and the Princess IV DP

    kuangbin 专题 这题,有很多种解法. 第一种: 直接比较每个数出现次数. #include<iostream> #include<string> #include< ...

  9. HDU 1029 Ignatius and the Princess IV(数论)

    #include <bits/stdc++.h> using namespace std; int main(){ int n; while(~scanf("%d",& ...

随机推荐

  1. 系统、决策、控制研究系列(SSDC)

    本类目主要介绍的书籍来自springer的系列书籍中的一本,对于该系列书籍介绍如下: “系统.决策及控制研究”(SSDC)系列涵盖了在广泛认知的系统.决策及控制的各个领域的快速.最新和高质量的最新发展 ...

  2. LB层到Real Server之间访问请求的响应时间及HTTP状态码监控及报警设置

    为了监控到各业务的访问质量,基于LB层的Nginx日志,实现LB层到Real Server之间访问请求的响应时间(即upstream_response_time)及HTTP状态码(即upstream_ ...

  3. Redis常见问题和解决办法梳理

    =============Redis主从复制问题和解决办法 ================= 一.Redis主从复制读写分离问题 1)数据复制的延迟读写分离时,master会异步的将数据复制到sla ...

  4. Net-SNMP V3协议 安装配置笔记(CentOS 6.3/5.6)

    注意:snmp V3,需要需要关闭selinux和防火墙: 关闭selinux方法: #vi /etc/selinux/config 将文件中的SELINUX="" 为 disab ...

  5. BZOJ3782 上学路线

    设障碍个数为,\(obs\)则一般的容斥复杂度为\(O(2^{obs})\).但因为这个题是网格图,我们可以用DP解.设\(f[i]\)表示不经过任何障碍到达第\(i\)个障碍的方案数,转移时枚举可以 ...

  6. 2016.3.30 OneZero站立会议

    会议时间:2016年3月30日  13:00~13:20 会议成员:冉华,张敏,王巍,夏一鸣. 会议目的:汇报前一天工作,全体成员评论,确定会议内容或分配下一步任务. 会议内容: 1.汇报头一天工作情 ...

  7. Zookeeper 3.4.8分布式安装

    1.机器信息 五台centos 64位机器 2.集群规划 Server Name Hadoop Cluster Zookeeper   Ensemble HBase Cluster Hadoop01 ...

  8. Github上传更新

    通过2天的时间,不停的网上找各种资料,今天下午终于可以登录上github for Windows 客户端了,,, 然后通过一整晚的摸索,也把项目上传到github里. github地址:https:/ ...

  9. 08-java学习-数组-增强for循环-数组与方法-main函数参数

    数组定义和使用 数组与方法的结合使用 main函数传参

  10. java collections - keyset() vs entrySet() in map

    https://stackoverflow.com/questions/8962459/java-collections-keyset-vs-entryset-in-map http://blog.c ...