[思維題] Ignatius and the Princess IV HDU - 1029
題目大意:在給定的N個數字中,有個數字出現至少(N + 1)/ 2次,讓你找出他。
分析:這道題kuangbin大神把他放到了簡單DP裏,我沒看出怎麼DP。。。我是這麼想的,既然這個數字出現了(N + 1)/ 2次,那麼只有它可以和其他數字一一抵消並且最後還有剩餘。所以O(N)模擬下就是答案。
代碼:
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
int cnt,temp,k;
scanf("%d",&temp);
k=temp;
cnt=1;
for(int i=0;i<n-1;i++)
{
scanf("%d",&temp);
if(temp==k)
cnt++;
else
{
cnt--;
}
if(cnt==-1)
{
cnt=1;
k=temp;
}
}
printf("%d\n",k);
}
return 0;
}