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.22 深入理解Java的接口和抽象类

    前言 对于面向对象编程来说,抽象是他的一大特征之一.在Java中,可以通过两种形式来体现oop 的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初雪的时候会以为他们可以随意互 ...

  2. 线程的sleep方法

  3. 《队长说得队》第九次团队作业:Beta冲刺与验收准备

    项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十三 团队作业9:Beta冲刺与团队项目验收 团队名 ...

  4. cocos2dx for lua 截屏功能

    cocos2dx的utils类中包含截图功能,使用方法如下: cc.utils:captureScreen(function(successed,outputFile)--第一个参数是截图成功或者失败 ...

  5. C#基础-判断语句

    switch语句 Console.WriteLine("请输入月份"); string strInput = Console.ReadLine(); switch(strInput ...

  6. SQL前后端分页

    /class Page<T> package com.neusoft.bean; import java.util.List; public class Page<T> { p ...

  7. POJ:2632-Crashing Robots

    Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Description In a modernized warehouse, robot ...

  8. 水题:CF16C-Monitor

    Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...

  9. selenium2通过linkText/partialLinkText定位元素

    通过linkText定位 linkText是根据链接的文本来定位,如下图,导航上全是链接 此时我想找“新闻”这个元素,那么我就可以使用linkText方式定位,语法: By.linkText(“新闻” ...

  10. github FATAL:unable to access 'https://github.com/...: Failed to connect to github.com:443; No error

    今天整理github,初次使用,很多都不懂,所以遇到了克隆失败的问题,研究了大半天,后来..... 打开Git Bash,克隆已有工程到本地: $ git clone https://github.c ...