[Codeforces496D] Tennis Game

描述 Description

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?

输入格式 InputFormat

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.

输出格式 OutputFormat

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.

样例输入 SampleInput

8
2 1 2 1 1 1 1 1

样例输出 SampleOutput

3
1 6
2 3
6 1


Codeforces 496D


代码 Code

枚举 t,寻找是否存在符合的 s,二分优化时间复杂度。

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
#define pa pair<int,int>
const int maxn=100005;
int i,j,t,n,m,l,r,k,z,y,x;
int a[maxn],sum1[maxn],sum2[maxn];
vector <pa> ans;
vector <pa>::iterator ai;
inline int calc(int s)
{
    int i,j,t,p1,p2,s1,s2,c1,c2;
    s1=s2=c1=c2=0;
    p1=lower_bound(sum1+1,sum1+n+1,s)-sum1;
    p2=lower_bound(sum2+1,sum2+n+1,s)-sum2;
    while (p1<=n && p2<=n)
    {
        if (p1<p2)
        {
            c1++;
            s1=sum1[p1];s2=sum2[p1];
        }
        else
        {
            c2++;
            s1=sum1[p2];s2=sum2[p2];
        }
        p1=lower_bound(sum1+1,sum1+n+1,s1+s)-sum1;
        p2=lower_bound(sum2+1,sum2+n+1,s2+s)-sum2;
    }
    if (p1>n && p2>n) return 0;
    if (p1>n)
    {
        if ((sum2[n]-s2)%s!=0 || a[n]==1) return 0;
        c2+=(sum2[n]-s2)/s;
        return (c2>c1)?c2:0;
    }
    else
    {
        if ((sum1[n]-s1)%s!=0 || a[n]==2) return 0;
        c1+=(sum1[n]-s1)/s;
        return (c1>c2)?c1:0;
    }
}
int main()
{
    scanf("%d",&n);
    sum1[0]=sum2[0]=0;
    for (i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        sum1[i]=sum1[i-1];sum2[i]=sum2[i-1];
        if (a[i]==1) sum1[i]++;
        else sum2[i]++;
    }
    m=max(sum1[n],sum2[n]);
    ans.clear();
    for (i=1;i<=n;i++)
    {
        t=calc(i);
        if (t) ans.push_back(make_pair(t,i));
    }
    sort(ans.begin(),ans.end());
    printf("%d\n",(int)ans.size());
    for (ai=ans.begin();ai!=ans.end();ai++) printf("%d %d\n",(*ai).first,(*ai).second);
    return 0;
}