Codeforces Round #283 (Div. 2) D. Tennis Game

来源:互联网 发布:海南大学网络教学中心 编辑:程序博客网 时间:2024/04/29 08:25

D. Tennis Game
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ofs sets, he wins the match and the match is over. Heres and t are some positive integer numbers.

To spice it up, Petya and Gena choose new numbers s andt 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 winss 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 numberss and t for the given match are also lost. The players now wonder what values ofs 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 integersai. Ifai = 1, then thei-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 numberss and t corresponds to the given record.

Output

In the first line print a single number k — the number of options for numberss and t.

In each of the following k lines print two integerssi andti — the option for numberss and t. Print the options in the order of increasingsi, and for equalsi — in the order of increasingti.

  2个人打网球,谁先赢t次这一把就算赢了,谁先赢s把算赢,游戏结束。输出可能的s和t。

  枚举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;}



0 0
原创粉丝点击