HDU1029 简单DP
"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?
InputThe 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.
OutputFor 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
题意是给一个项数为奇数的数列,里面有一个元素出现次数大于(n+1)/2次,找出这个数。
因为要求的这个数一定大于这个数列个数的一半,所以不妨假设拿每次都取这个数和其他的数一换一,最终剩下的还是这个数。最坏情况是这个出现次数最多的数出现了(n+1)/2次,经过了(n-1)/2次相消后剩下一个这个数,其他情况例如拿其他的不同的数相消,最后剩下的数也一定是出现次数最多的数且剩下的数量大于1。代码也就呼之欲出了。
#include <bits/stdc++.h>
using namespace std;
int n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i;
int num=-;//存储出现次数最多的数
int cnt=;//num出现的次数,
int pre;//之前的数
for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
if(i==)
{
num=temp;
pre=temp;
cnt++;
continue;
}
if(temp==pre)cnt++;//输入的数与pre相同时 累计次数++
else
{
if(pre==-)//重置
{
num=temp;
pre=temp;
cnt=;
continue;
}
cnt--;//没有两两抵消掉且当前输入数与pre不同,累计次数--
if(cnt==)pre=-;//如果之前所有数都两两抵消掉了,即累计次数为0,就重置
}
}
cout<<num<<endl;
}
}
HDU1029 简单DP的更多相关文章
- HDU 1087 简单dp,求递增子序列使和最大
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- codeforces Gym 100500H A. Potion of Immortality 简单DP
Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...
- 简单dp --- HDU1248寒冰王座
题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...
- poj2385 简单DP
J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu1087 简单DP
I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB ...
- poj 1157 LITTLE SHOP_简单dp
题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...
- hdu 2471 简单DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2571 简单dp, dp[n][m] +=( dp[n-1][m],dp[n][m-1],d[i][k ...
- Codeforces 41D Pawn 简单dp
题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...
随机推荐
- jvm字节码助记符
反编译指令 javap -c xxxx.class JVM参数设置 -xx:+<option> 开启option -xx: -<option> ...
- 哈希 Perl第六章
哈希元素赋值: $hash{$some_key} = ‘something' 访问整个哈希: %some_hash = (’a' , '0' , 'b' , '1' , 'c' , '3') @a ...
- Azure IoT Hub 十分钟入门系列 (3)- 使用消息路由将原始设备数据记录存档
本文主要分享一个案例: 10分钟使用消息路由将原始设备数据记录存档 B站视频讲解:https://www.bilibili.com/video/av90223893/ 本文主要有如下内容: 1.理解什 ...
- 专题-集合-ConcurrentHashMap
本文介绍ConcurrentHashMap是线程安全的,但为什么却不用加锁的原因 一.ConcurrentHashMap简介 在jdk1.7中是采用Segment + HashEntry + Reen ...
- Ubuntu 国内安装 kubernetes
由于墙的原因,国内要安装 kubernetes 非常的麻烦,因此只要解决这个问题,就可以顺利安装 kubernetes 的 三个官法工具 kubelet.kubeadm.kubectl. 安装环境: ...
- Bugku-CTF加密篇之贝斯家族(@iH<,{bdR2H;i6*Tm,Wx2izpx2!)
贝斯家族 @iH<,{bdR2H;i6*Tm,Wx2izpx2!
- Python--比较两个字典部分value是否相等(可以用于接口自动化)
eg:例如你调用了一个新增的接口,以往功能测试的话,你再web端新增一个店铺之后,你肯定要去数据库中查看,这些数据插入的对不对,是否正确的插入了每个字段 # 比较两个字典部分是否相等 def comp ...
- Linux 服务器作为Nginx web服务器常见优化参数
内核参数调整cat /etc/sysctl.conf# sysctl settings are defined through files in # /usr/lib/sysctl.d/, /run/ ...
- C语言:找出一个大于给定整数m且紧随m的素数,-求出能整除x且不是偶数的数的个数,
//函数fun功能:找出一个大于给定整数m且紧随m的素数,并作为函数值返回. #include <stdlib.h> #include <conio.h> #include & ...
- How2j学习java-3下载 ECLIPSE
1.下载并解压Eclipse 下载并解压到e:/eclipse,目录情况如图所示.注: 这个Eclipse是64位的,应该使用本站提供的JDK(64)位,下载地址:JDK. 如果JDK位数和 Ecli ...