首页 > 代码库 > B - Ignatius and the Princess IV DP

B - Ignatius and the Princess IV DP

#include<iostream>
#include<vector>
using namespace std;
int cnt[999999];
int main()
{
    int time,n,limit;
    vector<int> been;
    while(cin>>time)
    {
    int a = 1;
    memset(cnt,0,sizeof(cnt));
    limit = (time+1)/2;
    while(time--)
    {
        cin>>n;
        cnt[n]++;
        if(cnt[n]>=limit&&a)
        {
            cout<<n<<endl;
            a=0;
        }
    }
    }
}

普通cnt数组

DP版本:

include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    _int64 n,a;
    while(scanf("%I64d",&n)!=EOF)
    {
        _int64 b=0,c;
        while(n--)
        {
            scanf("%I64d",&a);
            if(b==0)
            {
                c=a;
                b++;
            }
            else
            {
                if(c==a)
                    b++;
                else
                    b--;
            }
        }
        printf("%I64d\n",c);
    }
    return 0;

 

B - Ignatius and the Princess IV DP