[思维题] Ignatius and the Princess IV HDU

来源:互联网 发布:网络在线咨询 编辑:程序博客网 时间:2024/06/11 14:29
 [思维题] 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;}