HDU 1029 Ignatius and the Princess IV

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1029


题意:给出n个数,找出出现次数(n+1)/2以上的数


思路:说是dp的题,不过感觉和dp没什么关系,数据比较水似乎直接暴力或者用map容器都可以通过,因为要找的数必定占该序列一半以上,所以将2个不一样的数消掉,最后剩下的数必定是要找的数



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define maxn 10000030
using namespace std;

int s[maxn];

int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        for (int i=0;i<n;i++)
          scanf("%d",&s[i]);

        int res=s[0],cnt=1;

        for (int i=1;i<n;i++)
        {
            if (res==s[i]) cnt++;
            else cnt--;

            if (cnt==0)
            {
                res=s[i];
                cnt=1;
            }
        }
        printf("%d\n",res);

    }
    return 0;
}




你可能感兴趣的:(HDU 1029 Ignatius and the Princess IV)