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.
枚举t,在O(lgn)的复杂度判断这个t是否可行,如果可行的话计算出s。a[i]代表第一个人赢的第i把在整个序列中的位置,b[i]代表第二个人赢的第i把在整个序列中的位置,同时用win1[i]表示前i次第一个人赢了几次,win2[i]表示前i次第二个人赢了几次。
然后进行模拟,pos1表示第一个人在a数组中当前的位置,pos2表示第二个人在数组b中当前的位置,cur表示当前已经玩了几次,根据a[pos1+t]和b[pos2+t]的大小来判断下轮谁赢,然后根据pos和win更新下一轮结束后cur和pos1,pos2的位置。最后如果cur能刚好在N的位置并且赢的次数多的那个人是整个序列最后出现的那个人,说明t是满足的。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
typedef pair<int,int> pii;
const int MAXN=100010;
int N,a[MAXN],b[MAXN],win1[MAXN],win2[MAXN];
vector<pii> V;
int main(){
while(scanf("%d",&N)!=EOF){
int t,cnt1=0,cnt2=0,ans=0,last;
V.clear();
for(int i=1;i<=N;i++){
scanf("%d",&t);
last=t;
if(t==1) a[++cnt1]=i;
else b[++cnt2]=i;
win1[i]=cnt1;
win2[i]=cnt2;
}
for(t=1;t<=N;t++){
int cur=0,pos1=0,pos2=0,ans1=0,ans2=0,flag=1;
while(cur<N){
if(pos1+t>cnt1&&pos2+t>cnt2){
flag=0;
break;
}
if(pos1+t<=cnt1&&pos2+t<=cnt2){
if(a[pos1+t]<b[pos2+t]){
pos1+=t;
cur=win1[a[pos1]]+win2[a[pos1]];
pos2=win2[a[pos1]];
ans1++;
}
else{
pos2+=t;
cur=win1[b[pos2]]+win2[b[pos2]];
pos1=win1[b[pos2]];
ans2++;
}
}
else if(pos1+t<=cnt1){
int n=(cnt1-pos1)/t;
pos1+=n*t;
cur=win1[a[pos1]]+win2[a[pos1]];
ans1+=n;
}
else if(pos2+t<=cnt2){
int n=(cnt2-pos2)/t;
pos2+=n*t;
cur=win1[b[pos2]]+win2[b[pos2]];
ans2+=n;
}
}
if(flag&&(last==1&&ans1>ans2||last==2&&ans1<ans2)){
ans++;
V.push_back(make_pair(max(ans1,ans2),t));
}
}
int len=V.size();
sort(V.begin(),V.end());
printf("%d\n",ans);
for(int i=0;i<len;i++) printf("%d %d\n",V[i].first,V[i].second);
}
return 0;
}