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 每一个格子仅仅 ...
随机推荐
- 多表更新:update,join
1.多表更新: 下面我建两个表,并执行一系列sql语句,仔细观察sql执行后表中数据的变化,很容易就能理解多表联合更新的用法. 前期准备工作: update join_teacher_class jo ...
- Python的几种主动结束程序方式
1. sys.exit() 执行该语句会直接退出程序,这也是经常使用的方法,也不需要考虑平台等因素的影响,一般是退出Python程序的首选方法. 该方法中包含一个参数status,默认为0,表示正常退 ...
- 刷题10. Regular Expression Matching
一.题目说明 这个题目是10. Regular Expression Matching,乍一看不是很难. 但我实现提交后,总是报错.不得已查看了答案. 二.我的做法 我的实现,最大的问题在于对.*的处 ...
- [Fiddler学习] - Mock的简单实现原理及方法
最近在研究Fidder抓包并做一点测试工作,下面介绍一下Fiddler的实现原理: 简单来说从clent,server端发出来的请求,都需要通过Fiddler进行代理走一遍.如果有任何请求需要做修改, ...
- Gitee 码云 pages 搭建vue项目记录
首先要在.gitignore文件去掉/dist,这个文件默认是不上传的,但是执行打包bulid的时候会生成dist文件,而线上访问的是打包之后的dist文件: vue.config.js文件,如下图箭 ...
- vscode管理员身份运行
管理员身份运行,如vscode 如何设置呢? vscode图标右键 以管理员身份运行程序打钩就行了 运行“在终端打开”的时候,要以管理员身份运行 刚下载完vscode并运行并不是管理员身份会报错 解 ...
- happen-before原则
单线程happen-before原则: 在同一个线程中,书写在前面的操作happen-before后面的操作. 锁的happen-before原则: 同一个锁的unlock ...
- disconf---分布式配置管理平台的搭建(windows版本)
本人由刚开始接触博客,难免会有不足和错误,写博客只是记录本人在学习和工作的过程中的成长,如有不足,欢迎各位指正,谢谢~ 一.废话不多说,直接进入正题: ①获取github代码 https://gith ...
- utf-8无bom格式编码
BOM——Byte Order Mark,就是字节序标记 在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF.而FFFE在U ...
- centos610无桌面安装libreoffice
Centos610系列配置 #安装文件 yum -y install libreoffice #安装中文包 yum -y install libreoffice-langpack-zh-Han* #安 ...