cf496D Tennis Game
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?
Input
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.
Output
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.
Example
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
倍增预处理下每个数字往后2^k是哪,然后就可以logn的知道往后走n步是哪。
枚举每一个可能的“小分”,然后直接模拟下往后走。可以logn的时间知道1和2那个先到。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n;
int a[];
int s1[],s2[];
int go1[][],go2[][];
int bin[];
int lst1,lst2,anst;
struct aaa{int x,y;}ans[];
bool operator <(aaa a,aaa b){return a.x<b.x;}
inline int lowbit(int x){return x&(-x);}
inline int calc(int s,int k,int op)
{
if (s==-)return s;
while (k)
{
if (op==)s=go1[s][bin[lowbit(k)]];
else s=go2[s][bin[lowbit(k)]];
if (!s)break;
k-=lowbit(k);
}
return s==?-:s;
}
int main()
{
for (int i=;i<;i++)bin[<<i]=i;
n=read();
for (int i=;i<=n;i++)a[i]=read();
if (a[n]==)for (int i=;i<=n;i++)a[i]=-a[i];
for (int i=;i<=n;i++)
{
s1[i]=s1[i-]+(a[i]==);
s2[i]=s2[i-]+(a[i]==);
}
for (int i=n;i>=;i--)
{
go1[i][]=lst1;
go2[i][]=lst2;
if (a[i]==)lst1=i;
else lst2=i;
}
for (int i=;i<=;i++)
{
if(i>n)break;
for (int j=;j<=n;j++)
{
if (go1[j][i-])go1[j][i]=go1[go1[j][i-]][i-];
if (go2[j][i-])go2[j][i]=go2[go2[j][i-]][i-];
}
}
go1[][]=lst1;
go2[][]=lst2;
for (int i=;i<=;i++)
{
if (i>n)break;
if (go1[][i-]!=)go1[][i]=go1[go1[][i-]][i-];
if (go2[][i-]!=)go2[][i]=go2[go2[][i-]][i-];
}
for (int i=;i<=n;i++)
{
int cnt1=,cnt2=,now=,nx1,nx2,mrk=;
while (now!=-&&now<n)
{
nx1=calc(now,i,);
nx2=calc(now,i,);
if (nx1==-&&nx2==-){mrk=;break;}
if (nx1==-)cnt2++,now=nx2;
else if (nx2==-)cnt1++,now=nx1;
else
{
if (nx1<nx2)
{
cnt1++;now=nx1;
if (now==n)break;
}else
{
cnt2++;now=nx2;
}
}
}
if (mrk)continue;
if (!cnt1&&!cnt2)break;
if (cnt1>cnt2)ans[++anst].y=i,ans[anst].x=cnt1;
}
sort(ans+,ans+anst+);
printf("%d\n",anst);
for (int i=;i<=anst;i++)printf("%d %d\n",ans[i].x,ans[i].y);
}
cf496D
cf496D Tennis Game的更多相关文章
- Codeforces CF#628 Education 8 A. Tennis Tournament
A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...
- CF 628A --- Tennis Tournament --- 水题
CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...
- 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 ...
- PAT 1026. Table Tennis
A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For ...
- Tennis Championship
Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 1026. Table Tennis (30)
题目如下: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. ...
- codeforces 735C Tennis Championship(贪心+递推)
Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...
- Tennis Game CodeForces - 496D(唯一分解定理,费马大定理)
Tennis Game CodeForces - 496D 通过排列组合解决问题. 首先两组不同素数的乘积,是互不相同的.这应该算是唯一分解定理的逆运用了. 然后是,输入中的素数,任意组合,就是n的因 ...
随机推荐
- TDB文件介绍
samba在运行时,Samba 存储许多信息,从本地密码到希望从中收到信息的一系列客户端.这类数据其中一些是暂时的,在 Samba 重启时可能会被丢弃,但是另一些却是永久的,不会被丢弃.这类数据可能是 ...
- mysql中的空值问题
MySQL的查询如果需要用到空值的情况下,where后面的条件就需要注意了 MySQL中的表示空值的方法:is null 和 is not null 比如:select * from user whe ...
- Python SciPy Sparse模块学习笔记
1. sparse模块的官方document地址:http://docs.scipy.org/doc/scipy/reference/sparse.html 2. sparse matrix的存储 ...
- javaweb基础(8)_HttpServletResponse生成验证码
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- ubuntu 16.04 + 中文输入法
在桌面右上角设置图标中找到"System Setting",双击打开. 在打开的窗口里找到"Language Support",双击打开. 可能打开会说没有安装 ...
- NOIP模拟赛 混合图
[题目描述] Hzwer神犇最近又征服了一个国家,然后接下来却也遇见了一个难题. Hzwer的国家有n个点,m条边,而作为国王,他十分喜欢游览自己的国家.他一般会从任意一个点出发,随便找边走,沿途欣赏 ...
- STA basic
- poj 2236 网络连接问题 并查集
题意:n台电脑,当两台之间的距离小于d的时候可以连接. 题目会进行操作“修复”还有“查询是否已经连接”.只要在查询的时候输出YES或者ON 思路: 把可以相互连接的 即两者之间的距离小于 d q[i ...
- debian7不能apt安装emacs24
维护者在主页上 http://emacs.naquadah.org/ 提到: These packages are not maintained anymore I don't use these p ...
- UVa 10110 Light, more light
开始所有的灯是灭的,不过我们只关心最后一个灯. 在第i次走动时,只有编号为i的倍数的灯的状态才会改变. 也就是说n有偶数个约数的时候,最后一个灯的状态不会改变,也就是灭的. n有奇数个约数的时候也就是 ...