大意: n个人, 两个党派, 轮流投票, 两种操作(1)ban掉一个人 (2)投票, 每轮一个未被ban的人可以进行一次操作(1)或操作(2), 求最终哪个党派得票最多.
显然先ban人会更优, 所以维护两个set模拟. 不过好像可以有O(n)的做法?
#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl ' ' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;} //head const int N = 1e6+10; int n, a[N]; char s[N], b[2][2]{"R","D"}; set<int> q, g[2]; int main() { scanf("%d%s", &n, s); REP(i,0,n-1) { a[i]=s[i]=='D'; q.insert(i); g[a[i]].insert(i); } REP(i,0,1) if (g[!i].empty()) return puts(b[i]),0; while (q.size()) { int x = *q.begin(); q.erase(x); int id = a[x%n], now = x/n; auto t = g[!id].lower_bound(x%n); if (t==g[!id].end()) t=g[!id].begin(),++now; q.erase(now*n+*t); g[!id].erase(t); if (g[!id].empty()) return puts(b[id]),0; q.insert(x+n); } }