Ignatius and the Princess IV
Time limit:1 Sec Memory limit:256 MB
Topic Connection http://acm.hdu.edu.cn/showproblem.php?pid=1029
Description "OK, you aren't too bad, em ... But can never pass the next test. "Feng5166 says.
"I'll tell you a odd number N, and then N integers. There'll be a special integer among them and you've to tell me which an integer is the Special one after I tell you all the integers. "feng5166 says.
"What's the characteristic of the special integer?" Ignatius asks.
"The integer would appear at least (n+1)/2 times. If you can ' t find the right integer, I'll kill the princess, and you'll 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 the lines. The first line consists of an odd integer N (1<=n<=999999) which indicate the number of the integers feng5166 would te ll our hero. The second line contains the N integers. The input is terminated by the end of file. Outputfor each of the test case and you had to output only one line which contains the special number you had found. Sample INPUT5 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 OUTPUT3 5 1
HINT
Test Instructions
Give you n number, let you find repeat more than half of the number
Exercises
Direct violence statistics is good, because more than half of the repetition, it means that this number is the most, so encountered this number on + +, did not meet-
Code:
//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>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;} while (x>0) ch[++num]=x%10,x/=10; while (Num) Putchar (ch[num--]+48); Puts ("");}*///**************************************************************************************inline ll read () {intx=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;} while(x>0) ch[++num]=x%Ten, x/=Ten; while(Num) Putchar (ch[num--]+ -); Puts ("");}intMain () {intCnt=0; intans=0, A,n; while(SCANF ("%d", &n)! =EOF) {CNT=0; Ans=0; for(intI=0; i<n;i++) {scanf ("%d",&a); if(cnt==0) {ans=A; CNT++; } Else { if(a==ans) CNT++; ElseCNT--; }} printf ("%d\n", ans); }}
HDU 1029 Ignatius and the Princess IV DP