"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. 洛谷 P3901 数列找不同(莫队)

    题目链接:https://www.luogu.com.cn/problem/P3901 这道题简单莫队模板题,然后$add$和$del$分别处理$vis[]$从$0-->1$和从$1--> ...

  2. STA之RC网

    STA的主要工作是计算电路网络的延时,如今的电路网络还是由CMOS cell和net组成的,所以STA所要计算的延时仍是电容的充放电时间.等量子计算机普及的时候,如今的这一套理论都将随着科技的进步被丢 ...

  3. Hadoop3.1.1源码Client详解 : 写入准备-RPC调用与流的建立

    该系列总览: Hadoop3.1.1架构体系——设计原理阐述与Client源码图文详解 : 总览 关于RPC(Remote Procedure Call),如果没有概念,可以参考一下RMI(Remot ...

  4. Tomcat 和 JVM 性能调优总结

    Tomcat性能调优: 找到Tomcat根目录下的conf目录,修改server.xml文件的内容.对于这部分的调优,我所了解到的就是无非设置一下Tomcat服务器的最大并发数和Tomcat初始化时创 ...

  5. zol验证码抓包

    http://www.zol.com/detail/lcd/samsung/25254662.html?zp_from=pro_price_pricenode 网站’ http://city.zol. ...

  6. LAMP源码编译

    ---恢复内容开始--- 1.LAMP源码编译的基础环境 安装组建包:yum groupinstall "Development Tools" "Development ...

  7. java篇 之 多态

    2018-9-28 多态: 重载也称为静态多态(静态在编译阶段就能确定)(动态是跟运行时挂钩) 尽量去选择关系轻的,降低耦合度(紧密度) 内聚: 减少与外界的联系,降低与其他对象和类的联系 对象与对象 ...

  8. 树莓派下编译并使用miracl密码库

    参考:Linux下编译并使用miracl密码库 MIRACL用户手册:https://wenku.baidu.com/view/d542f2ed0975f46527d3e1dc.html 具体过程. ...

  9. P1428

    AC: #include <stdio.h> #define rep(i, a, b) for(int i = a; i <= b; i++) #define min(a, b) ( ...

  10. 解决STM32工程出现:Undefined symbol TIM_ClearFlag (referred from hcsr04.o).错误。类型问题Undefined symbol TIM_xxx (referred from xxx.o).

    出错原因: 工程FWLIB目录下没有添加stm32f10x_tim.c文件. 添加即可. 一般利用库开发,将ppp.c(ppp.c又调用了库stm32f10x_xx.h)写好之后的调用步骤: 举例使用 ...