"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?

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的更多相关文章

  1. HDU 1087 简单dp,求递增子序列使和最大

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  2. Codeforces Round #260 (Div. 1) A. Boredom (简单dp)

    题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...

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

  4. 简单dp --- HDU1248寒冰王座

    题目链接 这道题也是简单dp里面的一种经典类型,递推式就是dp[i] = min(dp[i-150], dp[i-200], dp[i-350]) 代码如下: #include<iostream ...

  5. poj2385 简单DP

    J - 简单dp Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit ...

  6. hdu1087 简单DP

    I - 简单dp 例题扩展 Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

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

  9. Codeforces 41D Pawn 简单dp

    题目链接:点击打开链接 给定n*m 的矩阵 常数k 以下一个n*m的矩阵,每一个位置由 0-9的一个整数表示 问: 从最后一行開始向上走到第一行使得路径上的和 % (k+1) == 0 每一个格子仅仅 ...

随机推荐

  1. 「JSOI2015」地铁线路

    「JSOI2015」地铁线路 传送门 第一问很简单:对于每条线路建一个点,然后所有该条线路覆盖的点向它连边,权值为 \(1\) ,然后它向所有线路上的点连边,权值为 \(0\) . 然后,跑一边最短路 ...

  2. Shiro&Jwt验证

    此篇基于 SpringBoot 整合 Shiro & Jwt 进行鉴权 相关代码编写与解析 首先我们创建 JwtFilter 类 继承自 BasicHttpAuthenticationFilt ...

  3. jQuery+PHP+Ajax动态数字统计展示实例

    jQuery+PHP+Ajax实现的一款动态数字统计展示实例,本例是在页面上动态展示了当前在线用户数,当然了,你可以应用到其他更多场景中. 首先我们在#number放置要统计的数字: <div ...

  4. selenium配合phantomjs实现爬虫功能,并把抓取的数据写入excel

    # -*- coding: UTF-8 -*- ''' Created on 2016年5月13日 @author: csxie ''' import datetime from Base impor ...

  5. 吴裕雄 python 机器学习——模型选择参数优化暴力搜索寻优GridSearchCV模型

    import scipy from sklearn.datasets import load_digits from sklearn.metrics import classification_rep ...

  6. 【MySQL】表的操作

    " 目录 #. 表操作 1. 创建数据库 2. 进入指定库 3. 创建表 4. 添加记录 5. 查询表的数据和结构 /. 查指定表中的存储数据 /. 查指定表的结构 6. 复制表 /. 即复 ...

  7. 传奇GOM引擎授权过期解决方法.

    传奇GOM引擎授权过期解决方法 下载最新的GOM引擎,将里面的Key.Lic文件找出来,替换掉授权过期的版本,如果你本身是免费版最好是找同样的免费版的来覆盖. 如果你本身是免费版,但是却用的是商业版K ...

  8. 排序算法之插入排序的python实现

    插入排序的工作原理如下: (1)从数组第一个元素开始(0下标),从该元素开始进行构建有序序列: (2)取出待排序列中第一个元素,然后从“有序”序列中,从后往前扫描: (3)如果该元素(有序序列)大于待 ...

  9. JS--easyui通用验证

    // JavaScript Document $.extend($.fn.validatebox.defaults.rules, { Composite_validation: { validator ...

  10. python的空格和tab混用报错问题

    python中的空格和tab键不能混用,如果则会报类似如下的错误: IndentationError: unindent does not match any outer indentation le ...