题目链接:
http://codeforces.com/contest/749/problem/C
题解:
给你一个字符串,里面只含有D和R两种字母,分别表示的是支持D和支持R,每一个人可以有两种选择,要么什么都不做,要么否定一个人的支持,就是让一个的支持无效,一直循环(从头到尾,再从头到尾,但是被否定的人就不算里面了),直到分出结果。
自己用贪心的方法,每一个人直接将离自己最近的那一个跟自己支持不同的人给否定掉就行了 。(不知道怎么证明。。。。。。)但是模拟的时候比较麻烦,自己错了好几次,最后看到了网上大神的代码,恍然大悟,看来自己还是太弱了,得更加努力才行。
代码:
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define met(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
const int maxn = 200000+10;
char s[maxn];
int main()
{
int n;
scanf("%d",&n);
scanf("%s",s);
queue<int>qd,qr;
for(int i=0;i<n;i++)
{
if(s[i]=='D')
qd.push(i);
if(s[i]=='R')
qr.push(i);
}
char last;
while(true)
{
if(qd.size()==0)
{
last='R';
break;
}
if(qr.size()==0)
{
last='D';
break;
}
if(qd.front()<qr.front())
{
qr.pop();
qd.push(qd.front()+n);
qd.pop();
}
else
{
qd.pop();
qr.push(qr.front()+ n);
qr.pop();
}
}
printf("%c\n",last);
}