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+10; const int M = 1e6+10; int n,m,k,cnt[3]; int p[3][N],a[N],pp[3][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=0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&x); cnt[x]++; p[x][cnt[x]]=i; pp[1][i]=cnt[1]; pp[2][i]=cnt[2]; } bool ok=false; int s1=0,s2=0; for(int i=1;i<=n;i++){ x=y=0; int g1=0,g2=0; int u,v; while(1){ x+=i;y+=i; u=p[1][x]; v=p[2][y]; if(u==v)break; else if(u==0){ g2++; x=pp[1][v]; y=pp[2][v]; if(v==n)break; } else if(v==0){ g1++; x=pp[1][u]; y=pp[2][u]; if(u==n)break; } else if(u<v){ g1++; x=pp[1][u]; y=pp[2][u]; } else if(v<u){ g2++; x=pp[1][v]; y=pp[2][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("0"); else { sort(ans+1,ans+tot+1,cmp); printf("%d ",tot); for(int i=1;i<=tot;i++){ printf("%d %d ",ans[i].s,ans[i].t); } } return 0; }