Codeforces 1086D.Rock-Paper-Scissors Champion

题意

一个长度为 n n n的石头剪刀布序列 S S S代表剪刀, R R R代表石头, P P P代表布,每次选择相邻的两个人进行比赛,直到只剩下一个人,有 m m m次修改,每次把某一个位置的人值的修改。输出 m + 1 m+1 m+1行,分别为这个序列有多少个人有可能会成为最后剩下那个人。


解析

对于某个人(假设为 S S S)如果他不可能是最后的胜者,那么他一定会被左边所有可能的胜者或者右边所有可能的胜者打败,只会有两种情况:

  • 左边只有 S S S R R R
  • 右边只有 S S S R R R

s e t set set和树状数组维护一下就行

#include <bits/stdc++.h>
using namespace std;
char s[200010];
int n,m,into[1000];
set<int> S[3];
int bit[200010][3];
int lowbit(int x){return x&(-x);}
void update(int x,int z,int t){for(x;x<200010;x+=lowbit(x))bit[x][t]+=z;}
int ask(int x,int t){int ans=0;for(x;x;x-=lowbit(x))ans+=bit[x][t];return ans;}
int calc(int x)
{
	int y = (x+1)%3;//bei da
	int z = (x+2)%3;
	int ans = ask(200000,x);
	if(S[y].size() == 2)return ans;
	if(S[z].size() == 2)return 0;

	set<int>::iterator a = S[y].upper_bound(n);a--;
	set<int>::iterator b = S[z].upper_bound(n);b--;
	if(*a>*b)ans-= ask(*a,x)-ask(*b,x);
	a = S[y].lower_bound(1);
	b = S[z].lower_bound(1);
	if(*a<*b)ans-= ask(*b,x)-ask(*a,x);
	return ans;
}
int main(int argc, char const *argv[])
{
	scanf("%d%d",&n,&m);
	into['S'] = 0;into['R'] = 1;into['P'] = 2;
	scanf("%s",s+1);
	for(int i=0;i<3;i++)S[i].insert(0),S[i].insert(n+1);
	for(int i=1;i<=n;i++)
	{
		S[into[s[i]]].insert(i);
		update(i,1,into[s[i]]);
	}
	int op;char q[10];
	for(int i=1;i<=m;i++)
	{
		printf("%d\n",calc(0)+calc(1)+calc(2));
		scanf("%d%s",&op,q);
		char y = q[0];
		S[into[s[op]]].erase(op);
		update(op,-1,into[s[op]]);
		s[op] = y;
		S[into[s[op]]].insert(op);
		update(op,1,into[s[op]]);
	}
	printf("%d\n",calc(0)+calc(1)+calc(2));
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值