hdu 1029 Ignatius and the Princess IV DP
Ignatius and the Princess IVTime Limit: 1 Sec Memory Limit: 256 MB题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=1029
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
HINT
题意
给你n个数,让你找到重复半数以上的数
题解:
直接暴力统计就好啦,因为重复半数以上,就说明这种数是最多的,所以遇到这个数就++,没遇到就--
代码:
//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH;
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/*
inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH+48);
puts("");
}
*/
//**************************************************************************************
inline ll read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH+48);
puts("");
}
int main()
{
int cnt=0;
int ans=0,a,n;
while(scanf("%d",&n)!=EOF)
{
cnt=0;
ans=0;
for(int i=0;i<n;i++)
{
scanf("%d",&a);
if(cnt==0)
{
ans=a;
cnt++;
}
else
{
if(a==ans)
cnt++;
else
cnt--;
}
}
printf("%d\n",ans);
}
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15303184/3111734
页:
[1]