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. 2018.4.9 Ubuntu install kreogist-mu

    先下载kreogist m文件 然后在下载哪里右键点击打开终端 输入sudo dpkg -i + 文件名 输入密码 下一步会显示 未安装未安装软件包 libmpv1. jiexialai要处理 sud ...

  2. java基础—接口概念

    一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如“金丝猴是一种动物”,金丝猴从动物这个类继承,同时“金丝猴是一种值钱的东西”,金丝猴从“值钱的东西”这个类继承,同时“金丝猴 ...

  3. iOS快速开发框架--Bee Framework

    Bee Framework是一款iOS快速开发框架,允许开发者使用Objective-C和XML/CSS来进行iPhone和iPad开发,由 Gavin Kwoe 和 QFish 开发并维护. 其早期 ...

  4. oracle 将查询结果输出到txt文件里

    在查询语句里先输入spool filepath 中间是需要查询的语句,最后spool off 就会把中间查询的结果都输入到file文件里 spool E:\log.txt; select id,nam ...

  5. ssh整合思想 Spring分模块开发 crud参数传递 解决HTTP Status 500 - Write operations are not allowed in read-only mode (FlushMode.MANUAL): Turn your Session into FlushMode.COMMIT/AUTO or(增加事务)

    在Spring核心配置文件中没有增加事务方法,导致以上问题 Action类UserAction package com.swift.action; import com.opensymphony.xw ...

  6. Spring中c3p0连接池 jar包下载 c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包

    c3p0-0.9.2.1 jar包和mchange-commons-java-0.2.3.4 jar 包 下载地址: https://pan.baidu.com/s/1jHDiR7g 密码 tyek

  7. 66. Plus One@python

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  8. 洛谷 2387/BZOJ 3669 魔法森林

    3669: [Noi2014]魔法森林 Time Limit: 30 Sec  Memory Limit: 512 MBSubmit: 3765  Solved: 2402[Submit][Statu ...

  9. hive sql 学习笔记

    1.coalesce 语法: COALESCE ( expression [ ,...n ] ) 参数: expression 任何类型的表达式. 返回类型: 返回数据类型优先级最高的 express ...

  10. 分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN

    分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN