Codeforces Round #283 (Div. 2) D. Tennis Game(模拟)
2 seconds
256 megabytes
standard input
standard output
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers.
To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets.
Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options?
The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 105).
The second line contains n space-separated integers ai. If ai = 1, then the i-th serve was won by Petya, if ai = 2, then the i-th serve was won by Gena.
It is not guaranteed that at least one option for numbers s and t corresponds to the given record.
In the first line print a single number k — the number of options for numbers s and t.
In each of the following k lines print two integers si and ti — the option for numbers s and t. Print the options in the order of increasing si, and for equal si — in the order of increasing ti.
5
1 2 1 2 1
2
1 3
3 1
4
1 1 1 1
3
1 4
2 2
4 1
4
1 2 1 2
0
8
2 1 2 1 1 1 1 1
3
1 6
2 3
6 1
【分析】进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时,游戏结束。
现在给出n次对决的记录,问可能的s和t有多少种,并按s递增的方式输出。
可以想到枚举t,然后挨个找每一场结束的位置。但是从头遍历肯定会超时,所以需要预处理一下。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 1e6+;
const int M = 1e6+;
int n,m,k,cnt[];
int p[][N],a[N],pp[][N];
struct man{
int s,t;
}ans[N];
bool cmp(man d,man f){
if(d.s==f.s)return d.t<f.t;
return d.s<f.s;
}
int main() {
int x,y,tot=;
scanf("%d",&n);
for(int i=;i<=n;i++){
scanf("%d",&x);
cnt[x]++;
p[x][cnt[x]]=i;
pp[][i]=cnt[];
pp[][i]=cnt[];
}
bool ok=false;
int s1=,s2=;
for(int i=;i<=n;i++){
x=y=;
int g1=,g2=;
int u,v;
while(){
x+=i;y+=i;
u=p[][x];
v=p[][y];
if(u==v)break;
else if(u==){
g2++;
x=pp[][v];
y=pp[][v];
if(v==n)break;
}
else if(v==){
g1++;
x=pp[][u];
y=pp[][u];
if(u==n)break;
}
else if(u<v){
g1++;
x=pp[][u];
y=pp[][u];
}
else if(v<u){
g2++;
x=pp[][v];
y=pp[][v];
}
}
if(u!=n&&v!=n)continue;
if(g1!=g2){
ok=true;
if(g1>g2&&u==n){
ans[++tot].s=max(g1,g2);
ans[tot].t=i;
}
else if(g2>g1&&v==n){
ans[++tot].s=max(g1,g2);
ans[tot].t=i;
}
}
}
if(!ok)puts("");
else {
sort(ans+,ans+tot+,cmp);
printf("%d\n",tot);
for(int i=;i<=tot;i++){
printf("%d %d\n",ans[i].s,ans[i].t);
}
}
return ;
}
Codeforces Round #283 (Div. 2) D. Tennis Game(模拟)的更多相关文章
- Codeforces Round #283 Div.2 D Tennis Game --二分
题意: 两个人比赛,给出比赛序列,如果为1,说明这场1赢,为2则2赢,假如谁先赢 t 盘谁就胜这一轮,谁先赢 s 轮则赢得整个比赛.求有多少种 t 和 s 的分配方案并输出t,s. 解法: 因为要知道 ...
- 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns
题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...
- 构造+暴力 Codeforces Round #283 (Div. 2) B. Secret Combination
题目传送门 /* 构造+暴力:按照题目意思,只要10次加1就变回原来的数字,暴力枚举所有数字,string大法好! */ /************************************** ...
- Codeforces Round #368 (Div. 2) B. Bakery (模拟)
Bakery 题目链接: http://codeforces.com/contest/707/problem/B Description Masha wants to open her own bak ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #285 (Div. 2) A B C 模拟 stl 拓扑排序
A. Contest time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- codeforces 497b// Tennis Game// Codeforces Round #283(Div. 1)
题意:网球有一方赢t球算一场,先赢s场的获胜.数列arr(长度为n)记录了每场的胜利者,问可能的t和s. 首先,合法的场景必须: 1两方赢的场数不一样多. 2赢多的一方最后一场必须赢. 3最后一场必须 ...
- Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划
C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...
- Codeforces Round #382 (Div. 2) C. Tennis Championship 斐波那契
C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
随机推荐
- 【题解】HNOI2016树
大概最近写的这些题目都是仿生的代码……在这里先说明一下.可能比起做题记录来说更加像是学习笔记吧.之所以这样做主要还是因为感受到最近做的很多题目自己会做的都比较简单,不会做的又不敢触及,虽然也有所进步. ...
- BZOJ3295: [Cqoi2011]动态逆序对 莫队
这个用莫队做会被卡,但是我还是...... 收获在于树状数组维护后缀和以及双维排序...... 莫队的时间复杂度比想象中的要好一些.... 然而我还是被卡了...... #include<ios ...
- 强大的JQuery数组封装使用
JQuery对数组的处理非常便捷并且功能强大齐全,一步到位的封装了很多原生js数组不能企及的功能.下面来看看JQuery数组的强大之处在哪. $.each(array, [callback]) 遍历 ...
- [hdu 3068] Manacher算法O(n)最长回文子串
一个不错的讲解:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/01.05.md # ...
- hdu 3473 (划分树)2
Minimum Sum Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- weblogic的安装和注意的问题以及在idea怎么用weblogic启动一个web服务
第一步下载weblogic: 大家可以通过我的网盘下载weblogic,地址如下 https://pan.baidu.com/s/1NkZ_Gd-xfim5YGcdtjYoUw 第二步安装weblog ...
- java中枚举类到高级使用
参考博文: http://blog.csdn.net/qq_31655965/article/details/55049192 http://www.cnblogs.com/zhaoyanjun/p/ ...
- bzoj 2425 [HAOI2010]计数 dp+组合计数
[HAOI2010]计数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 451 Solved: 289[Submit][Status][Discus ...
- Bash 实例,第一部分
您可能要问:为什么要学习 Bash 编程?好,以下是几条令人信服的理由: 已经在运行它 如果查看一下,可能会发现:您现在正在运行 bash.因为 bash 是标准 Linux shell,并用于各种目 ...
- python函数超时,用装饰器解决 func_timeout
https://zhuanlan.zhihu.com/p/39743129 https://www.jianshu.com/p/a7fc98c7af4d https://ixyzero.com/blo ...