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 ...
随机推荐
- Thinkphp整合阿里云OSS图片上传实例
Thinkphp3.2整合阿里云OSS图片上传实例,图片上传至OSS可减少服务器压力,节省宽带,安全又稳定,阿里云OSS对于做负载均衡非常方便,不用传到各个服务器了 首先引入阿里云OSS类库 < ...
- Spark启动报错|java.io.FileNotFoundException: File does not exist: hdfs://hadoop101:9000/directory
at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:) at org.a ...
- Kafka(1)--kafka基础知识
Kafka 的简介: Kafka 是一款分布式消息发布和订阅系统,具有高性能.高吞吐量的特点而被广泛应用与大数据传输场景.它是由 LinkedIn 公司开发,使用 Scala 语言编写,之后成为 Ap ...
- 将String类型的json字符串转换成java对象
1,import com.fasterxml.jackson.databind.ObjectMapper; ObjectMapper mapper = new ObjectMapper(); Mycl ...
- C++_数字时钟
利用C++语言基础,制作了一个模拟电子时钟的程序. #include<iostream> #include<windows.h> //延时与清屏头文件 using namesp ...
- JIRA日期格式设置
https://blog.csdn.net/zj911008/article/details/48312927?utm_source=blogxgwz3 https://blog.csdn.net/z ...
- react-native-table-component, react-native 表格
使用 react-native-table-component, 加上 FlatList 组件,实现可以下拉刷新,上拉加载的demo import React, { Component } from ...
- 线程之Callable、Future 和FutureTask使用及源码分析
一.Callable 我们知道启动线程有以下两种方式(jdk源码注释中官方定义只有两种启动方式,callable不算线程启动方式) 原文链接:http://www.studyshare.cn/blog ...
- @PostConstruct注解小结
1.在具体Bean的实例化过程中,@PostConstruct注解的方法,会在构造方法之后,init方法之前进行调用2.在项目中@PostConstruct主要应用场景是在初始化Servlet时加载一 ...
- qt学习001之运行对话框
使用QT实现Window下运行对话框 1.摆放控件 首先设置并摆放相应的对话框控件,并更改相应名称: 2.实现功能 1)在文本框中输入信息后,点击确定或回车可以运行系统中相应的程序: 点 ...