Codeforces 1086D Rock-Paper-Scissors Champion
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的更多相关文章
- 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)
2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...
- SDUT 3568 Rock Paper Scissors 状压统计
就是改成把一个字符串改成三进制状压,然后分成前5位,后5位统计, 然后直接统计 f[i][j][k]代表,后5局状压为k的,前5局比和j状态比输了5局的有多少个人 复杂度是O(T*30000*25*m ...
- FFT(Rock Paper Scissors Gym - 101667H)
题目链接:https://vjudge.net/problem/Gym-101667H 题目大意:首先给你两个字符串,R代表石头,P代表布,S代表剪刀,第一个字符串代表第一个人每一次出的类型,第二个字 ...
- Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数
Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...
- Gym101667 H. Rock Paper Scissors
将第二个字符串改成能赢对方时对方的字符并倒序后,字符串匹配就是卷积的过程. 那么就枚举字符做三次卷积即可. #include <bits/stdc++.h> struct Complex ...
- 【题解】CF1426E Rock, Paper, Scissors
题目戳我 \(\text{Solution:}\) 考虑第二问,赢的局数最小,即输和平的局数最多. 考虑网络流,\(1,2,3\)表示\(Alice\)选择的三种可能性,\(4,5,6\)同理. 它们 ...
- 题解 CF1426E - Rock, Paper, Scissors
一眼题. 第一问很简单吧,就是每个 \(\tt Alice\) 能赢的都尽量让他赢. 第二问很简单吧,就是让 \(\tt Alice\) 输的或平局的尽量多,于是跑个网络最大流.\(1 - 3\) 的 ...
- HDOJ(HDU) 2164 Rock, Paper, or Scissors?
Problem Description Rock, Paper, Scissors is a two player game, where each player simultaneously cho ...
- HDU 2164 Rock, Paper, or Scissors?
http://acm.hdu.edu.cn/showproblem.php?pid=2164 Problem Description Rock, Paper, Scissors is a two pl ...
- 1090-Rock, Paper, Scissors
描述 Rock, Paper, Scissors is a classic hand game for two people. Each participant holds out either a ...
随机推荐
- python_装饰器
越来越觉得写一点技术博客是有多么重要了,明日复明日,现在就开始写吧! 1. 普通装饰器 装饰器的写法是一种语法糖,装饰器也还是一个函数而已,它接收一个函数对象作为参数,并返回一个新函数,主要是拓展原函 ...
- 创建Oracle表空间
*分为四步 */ /*第1步:创建临时表空间 */ create temporarytablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\ ...
- flask sqlchemy 多对多的自引用关系定义
多对多的定义可以使用关联表,或者重新定义一个模型,通过模型定义多对多的自引用在flask web开发书里有讲到,这里主要演示用关联表定义的方法. from flask_sqlalchemy impor ...
- EXCEL文本字符串转日期
=IF(ISERROR(DATEVALUE(A2)), A2, DATEVALUE(A2)) 注意ISERROR函数的使用,如果DATEVALUE解析字符串失败,比如单元格数值本来就是日期,会发挥#V ...
- 这就涉及到ABAQUS历史输出中各能量变量的意义
ABAQUS中,对于很多动态问题,尤其像高速冲击模拟中,对结果评价很重要的一点就是要保证模型能量守恒,这就涉及到ABAQUS历史输出中各能量变量的意义,下面最各简单整理: ALLAE:人工伪应变能,六 ...
- [Flutter] Windows/MacOS Flutter 环境走一遍
Windows Install 1.系统需要:> win7 > 400M磁盘空间 Windows PowerShell(Windows 搜索框中找) Git for Windows 2.x ...
- 吴裕雄 python 数据可视化
import pandas as pd df = pd.read_csv("F:\\python3_pachongAndDatareduce\\data\\pandas data\\taob ...
- C# 多线程传递参数或多个参数
using System;using System.IO;using System.Text;using System.Threading; namespace ConsoleApp7{ class ...
- Linux源码安装JDK1.8
Linux源码安装Java 1.到官网下载 jdk-8u131-linux-x64.tar.gz 官网地址:http://www.oracle.com/technetwork/java/javase/ ...
- 使用netty HashedWheelTimer构建简单延迟队列
背景 最近项目中有个业务,需要对用户新增任务到期后进行业务处理.使用定时任务定时扫描过期时间,浪费资源,且不实时.只能使用延时队列处理. DelayQueue 第一想到的是java自带的延时队列del ...