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

Input
5
1 2 1 2 1
Output
2
1 3
3 1
Input
4
1 1 1 1
Output
3
1 4
2 2
4 1
Input
4
1 2 1 2
Output
0
Input
8
2 1 2 1 1 1 1 1
Output
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的更多相关文章

  1. 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 ...

  2. CF 628A --- Tennis Tournament --- 水题

    CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m & ...

  3. Codeforces Round #382 (Div. 2)C. Tennis Championship 动态规划

    C. Tennis Championship 题目链接 http://codeforces.com/contest/735/problem/C 题面 Famous Brazil city Rio de ...

  4. 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 ...

  5. PAT 1026. Table Tennis

    A table tennis club has N tables available to the public.  The tables are numbered from 1 to N.  For ...

  6. Tennis Championship

    Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. 1026. Table Tennis (30)

    题目如下: A table tennis club has N tables available to the public. The tables are numbered from 1 to N. ...

  8. codeforces 735C Tennis Championship(贪心+递推)

    Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...

  9. Tennis Game CodeForces - 496D(唯一分解定理,费马大定理)

    Tennis Game CodeForces - 496D 通过排列组合解决问题. 首先两组不同素数的乘积,是互不相同的.这应该算是唯一分解定理的逆运用了. 然后是,输入中的素数,任意组合,就是n的因 ...

随机推荐

  1. 在DataGridView控件中启用换行

    实现效果: 知识运用: DataGridView控件公共属性DefaultCellStyle的WrapMode属性 public DataGridViewTriState WrapMode {  ge ...

  2. python之函数默认参数的坑

    坑 当你的默认参数如果是可变的数据类型,你要小心了 例题 # 正常没毛病的操作 def func(a,b=False): print(a) print(b) func(1,True) # 在实参角度, ...

  3. JavaScript——图片懒加载

    前言 有一个朋友问我这个问题,刚好有时间,现在就简单的写个Demo~ github | https://github.com/wangyang0210/bky/tree/picLoadLazy 内容 ...

  4. java基础—线程(一)

    一.线程的基本概念

  5. cocos2dx for iOS fmod的音效引擎接入

    上一个博客我写了一篇fmod的android接入过程,这一次介绍一下ios接入fmod的方法. 首先下载fmod的api包,解压后,在FMOD Programmers API/api文件夹下有lowl ...

  6. CentOS7安装配置VSFTP

    #是否开启匿名用户,匿名都不安全,不要开 anonymous_enable=NO #允许本机账号登录FTP local_enable=YES #允许账号都有写操作 write_enable=YES # ...

  7. 13Shell脚本—编写简单脚本

    1. 概述 Shell脚本命令的工作方式有两种:交互式和批处理. 交互式(Interrctive): 用户每输入一条命令就立即执行. 批处理(Batch): 由用户事先编写好一个完整的 Shell 脚 ...

  8. java 的多态(2013-10-11-163 写的日志迁移

    java 的多态性:(所谓多态--就是指一个引用(类型)在不同情况下的多种状态)   1.方法的多态:    重载(overload)   重写(覆盖 override)   2.对象的多态性:(本人 ...

  9. 【全面】Linux基础知识和基本操作语句大全(一)

    接触Linux已经有一段时间了,由于实际需要,三三两两地掌握了一些基本语法和实用语句,主要都是在日常开发中用得比较多的,条理不是特别清晰,请见谅!下面开始上硬货!! 基本操作: 关闭Linux系统的命 ...

  10. matplotlib学习记录 一

    from matplotlib import pyplot as plt # 先实例一个图片,传入图片参数,10宽,5高,分辨率为80 image = plt.figure(figsize=(10,5 ...