Codeforces 496D Tennis Game【连续二分+枚举】

来源:互联网 发布:从程序员到架构师 pdf 编辑:程序博客网 时间:2024/06/15 11:11

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.

Examples
Input
51 2 1 2 1
Output
21 33 1
Input
41 1 1 1
Output
31 42 24 1
Input
41 2 1 2
Output
0
Input
82 1 2 1 1 1 1 1
Output
31 62 36 1

题目大意:

已知N轮比赛的比赛结果,1表示A胜利,2表示B胜利。

一个赛季一共有S场比赛,每场比赛赛点为t轮。表示一场比赛如果有一个人先获胜了t轮那么这一场比赛就结束了,并且先达到t轮胜利的那个人为这场比赛的获胜者。

已知,如果一个人先获得了S场比赛的胜利,那么就结束了所有轮次的比赛,也就是说在N轮前结束的比赛是不合法的。

现在让你找到合法的s,t,并且按照s递增输出。


思路:


1、如果暴力去搞这个问题的话,就是O(n)枚举t.然后O(n)判断是否满足题意,使得一个队胜利了S场,且在第N轮结束的时候获得的最终胜利,那么对应此时的t,s是维护出来的。

那么总时间复杂度O(n^2);显然超时的一个时间复杂度。

其实对于一场比赛来讲,已知这场比赛的起点轮次的情况下,我们可以二分终点的轮次。

对于每一场游戏进行维护,那么我们需要连续二分,将时间复杂度降低至O(n*logn*longn);


2、在维护的过程中,需要保证正好游戏进行了N轮,并且需要保证最后一轮结束的同时,获胜的那只队伍必须是最终的获胜者。注意平局也是不可行的方案。


Ac代码:


#include<stdio.h>#include<iostream>#include<string.h>#include<algorithm>using namespace std;struct node{    int x,y;}e[150000];int a[150000];int sum[105000];int sum2[105000];int n;int cmp(node a,node b){    return a.x<b.x;}int Slove(int tt){    int aa=0;    int bb=0;    int now=0;    while(now<=n)    {        if(now==n)break;        int ans=-1;        int l=tt+now;        int r=tt*2-1+now;        r=min(r,n);        while(r-l>=0)        {            int mid=(l+r)/2;            if(sum[mid]-sum[now]>=tt||sum2[mid]-sum2[now]>=tt)            {                if(sum[mid]-sum[now]==tt)ans=mid;                if(sum2[mid]-sum2[now]==tt)ans=mid;                r=mid-1;            }            else l=mid+1;        }        if(ans==-1)return -1;        else        {            if(sum[ans]-sum[now]==tt)            {                aa++;                if(ans==n)                {                    if(aa<=bb)return -1;                }            }            if(sum2[ans]-sum2[now]==tt)            {                bb++;                if(ans==n)                {                    if(bb<=aa)return -1;                }            }            now=ans;        }    }    int tmp=max(aa,bb);    if(aa==bb)return -1;    return tmp;}int main(){    while(~scanf("%d",&n))    {        memset(sum,0,sizeof(sum));        memset(sum2,0,sizeof(sum2));        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            int tmp=0;            if(a[i]==1)tmp++;            sum[i]=sum[i-1]+tmp;            tmp=0;            if(a[i]==2)tmp++;            sum2[i]=sum2[i-1]+tmp;        }        int tot=0;        for(int t=1;t<=n;t++)        {            int ans=Slove(t);            if(ans!=-1)            {                e[tot].x=ans;                e[tot++].y=t;            }        }        sort(e,e+tot,cmp);        printf("%d\n",tot);        for(int i=0;i<tot;i++)        {            printf("%d %d\n",e[i].x,e[i].y);        }    }}











0 0