Codeforces Round #388 749C Voting

第一要明白它的规则,第二要清楚如何才能得到对自己最有利的方法,第三还有找到简单的执行方法执行。
1.规则是按排队顺序选择一个没有作废的人,他可以选择一个人让他的票作废。如果不选则他的票作废。直到剩下一个人。
2.如果当时两派都在,如果不选下一个能投票的对立派,他就能多一次投票投出自己派别的。所以要投出下一个对立派。
3.如果是一个数组循环判断那么就有可能超过时间限制,就要不能多次对已经没有投票权的人投票。
最初没有想到怎么做。看了大神的代码后很受启发。

#include<algorithm>
#include <iostream>
#include  <ctype.h>
#include  <cstring>
#include  <fstream>
#include   <cstdio>
#include   <vector>
#include   <string>
#include    <cmath>
#include    <stack>
#include    <queue>
#include      <set>
#include      <map>
#define INF (1<<30)
#define PI acos(-1.0)
typedef long long ll;
using namespace std;
const int N=2000010;
int n,i,h,t,x,q[N],f[2],c[2];
char a[N];
int main()
{
    scanf("%d%s",&n,a+1);
    for(i=1; i<=n; i++)c[q[i]=a[i]=='R']++;
    h=1,t=n;
    while(c[0]&&c[1])
    {
        x=q[h++];
        if(f[x])
        {
            f[x]--;
            c[x]--;
            continue;
        }
        f[x^1]++;
        q[++t]=x;
    }
    puts(q[h]?"R":"D");
}

猜你喜欢

转载自blog.csdn.net/lxworld123/article/details/54409713