Description

\(N\) 个人排成一排, 每个人都事先决定出剪刀、石头、布。

每次可以任意选两个相邻的人进行决斗。 规则和游戏一样。

但是如果平局, 则掷硬币来决定胜负。 输的人下场。

现要求出有几个人 有获胜的可能(即可以任意决定决斗的顺序 和 掷出的硬币)

Solution

一个很显然的结论: 一个人要想获胜, 两边都要满足其中一个条件, 以左边为例:

左边没有能赢他的人, 或者 左边存在一个他能赢的人即可。

根据这个结论, 我们分别计算出剪刀 、石头、 布的人有多少人能赢。

以计算出剪刀有多少人能赢为例, 先找出最先出布的人和最后出布的人, 这两个人中间的人都可以赢, 记入贡献

然后再剩余出剪刀的人 左边没有人出石头 和 右边没有人出石头的人的个数。

用\(Bit\)和\(Set\) 可以\(O(logN)\)计算。

Code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#define up(a, b) (a = a > b ? a : b)
#define down(a, b) (a = a > b ? b : a)
#define cmax(a, b) (a > b ? a : b)
#define cmin(a, b) (a > b ? b : a)
#define Abs(a) ((a) > 0 ? (a) : -(a))
#define lowbit(x) ((x) & -(x))
#define rd read()
#define db double
#define LL long long
using namespace std;
typedef pair<int, int> P; /*
inline char nc(){
static char buf[1<<14],*p1=buf,*p2=buf;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,1<<14,stdin),p1==p2)?EOF:*p1++;
}
inline LL read(){
char c=nc();LL x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
return x*f;
}
*/ int read() {
int X = 0, p = 1; char c = getchar();
for (; c > '9' || c < '0'; c = getchar())
if (c == '-') p = -1;
for (; c >= '0' && c <= '9'; c = getchar())
X = X * 10 + c - '0';
return X * p;
} const int N = 2e5 + 5; int n, m, sum[4][N];
char s[N];
set<int> S[4]; int ch(char c) {
if (c == 'R')
return 0;
if (c == 'P')
return 1;
if (c == 'S')
return 2;
} void add(int x, int d, int *p) {
for (; x <= n; x += lowbit(x))
p[x] += d;
} int query(int x, int *p) {
if (x < 0) return 0;
int res = 0;
for (; x; x -= lowbit(x))
res += p[x];
return res;
} int work(int x) {
int y = (x + 2) % 3; //x > y
int z = (x + 1) % 3; //z > x
int res = 0;
if (!S[y].size()) {
if (S[z].size())
return 0;
else return n;
}
int l = *(S[y].begin()), r = *(--S[y].end());
if (S[y].size() > 1)
res += query(r, sum[x]) - query(l - 1, sum[x]);
int tmp = S[z].size() ? *(S[z].begin()) : n + 1;
down(tmp, l);
res += query(tmp, sum[x]); tmp = S[z].size() ? *(--S[z].end()) : 0;
up(tmp, r);
res += query(n, sum[x]) - query(tmp - 1, sum[x]);
return res;
} int main()
{
n = rd; m = rd;
scanf("%s", s + 1);
for (int i = 1; i <= n; ++i) {
add(i, 1, sum[ch(s[i])]);
S[ch(s[i])].insert(i);
}
printf("%d\n", work(0) + work(1) + work(2));
for (int i = 1; i <= m; ++i) {
int x = rd; char c = getchar();
for (; !(c == 'R' || c == 'P' || c == 'S');)
c = getchar();
S[ch(s[x])].erase(x); add(x, -1, sum[ch(s[x])]);
s[x] = c;
S[ch(s[x])].insert(x); add(x, 1, sum[ch(s[x])]);
printf("%d\n", work(0) + work(1) + work(2));
}
}

Codeforces 1086D Rock-Paper-Scissors Champion的更多相关文章

  1. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  2. SDUT 3568 Rock Paper Scissors 状压统计

    就是改成把一个字符串改成三进制状压,然后分成前5位,后5位统计, 然后直接统计 f[i][j][k]代表,后5局状压为k的,前5局比和j状态比输了5局的有多少个人 复杂度是O(T*30000*25*m ...

  3. FFT(Rock Paper Scissors Gym - 101667H)

    题目链接:https://vjudge.net/problem/Gym-101667H 题目大意:首先给你两个字符串,R代表石头,P代表布,S代表剪刀,第一个字符串代表第一个人每一次出的类型,第二个字 ...

  4. Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数

    Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...

  5. Gym101667 H. Rock Paper Scissors

    将第二个字符串改成能赢对方时对方的字符并倒序后,字符串匹配就是卷积的过程. 那么就枚举字符做三次卷积即可. #include <bits/stdc++.h> struct Complex ...

  6. 【题解】CF1426E Rock, Paper, Scissors

    题目戳我 \(\text{Solution:}\) 考虑第二问,赢的局数最小,即输和平的局数最多. 考虑网络流,\(1,2,3\)表示\(Alice\)选择的三种可能性,\(4,5,6\)同理. 它们 ...

  7. 题解 CF1426E - Rock, Paper, Scissors

    一眼题. 第一问很简单吧,就是每个 \(\tt Alice\) 能赢的都尽量让他赢. 第二问很简单吧,就是让 \(\tt Alice\) 输的或平局的尽量多,于是跑个网络最大流.\(1 - 3\) 的 ...

  8. HDOJ(HDU) 2164 Rock, Paper, or Scissors?

    Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...

  9. HDU 2164 Rock, Paper, or Scissors?

    http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...

  10. 1090-Rock, Paper, Scissors

    描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...

随机推荐

  1. mysql 动态行转列

    表结果:create table user( id int , username ), create_time datetime, type int ) insert into user (`id`, ...

  2. 【python】python中的enumerate()函数【笔记】

    结合实例来理解比较好,网上找了一下这个enumerate用法,自己也记录一下加深印象 看一下相关链接: 链接1:http://www.cnblogs.com/danielStudy/p/6576040 ...

  3. centos mysql 默认是区分大小写的,修改成不区分大小写

    修改mysql为不区分大小写设置: [root@test-huanqiu ~]# vim /etc/my.cnf //添加下面一行设置 .... [mysqld] lower_case_table_n ...

  4. mysql5.0手动升级8.0.15,并链接到navicat

    一.卸载老版本的mysql 1.1 在控制面板中删除即可 1.2 将老版本的mysql安装残留文件彻底删除 二.彻底删除mysql-注册表 2.1 开始->运行-> regedit 看看注 ...

  5. if __name__ == '__main__':用法

    这个博主写的很好,已经验证过了.https://blog.csdn.net/yjk13703623757/article/details/77918633

  6. (ZT)算法杂货铺——分类算法之朴素贝叶斯分类(Naive Bayesian classification)

    https://www.cnblogs.com/leoo2sk/archive/2010/09/17/naive-bayesian-classifier.html 0.写在前面的话 我个人一直很喜欢算 ...

  7. JavaScript:我总结的数组API

    栈/队列 数组es3: pop删除最后一项(栈) shift删除第一项(队列) push增加到最后(栈) unshift增加到最前(队列) reverse翻转 join转字符串 slice截取(切片) ...

  8. jQuery入门学习

    一.jQuery的介绍 1.jQuery是一种轻量级的.兼容多浏览器的JavaScript库. 2.jQuery使用户能够方便处理HTML Document Events 实现动画效果.方便的进行Aj ...

  9. 大数据架构工具hadoop

    Hadoop是一个开源框架,它允许在整个集群使用简单编程模型计算机的分布式环境存储并处理大数据.它的目的是从单一的服务器到上千台机器的扩展,每一个台机都可以提供本地计算和存储. “90%的世界数据在过 ...

  10. Dubbo 的配置主要分为三大类

    服务发现.服务治理和性能调优:这三类配置不是独立存在的,而是贯穿在所有配置项中的,比如dubbo:service 标签中的interface 是服务发现类, timeout是性能调优类, mock 是 ...