496D Tennis Game (快速查找)

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 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?

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 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.

Output

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.

Sample test(s)
input
5
1 2 1 2 1
output
2
1 3
3 1
input
4
1 1 1 1
output
3
1 4
2 2
4 1
input
4
1 2 1 2
output
0
input
8
2 1 2 1 1 1 1 1
output
3
1 6
2 3
6 1
 
题意:每一局赢一分,一场有多局,得t分赢一场,s场赢得比赛,每场赢得人名会被按次序记录,输入n个赢得局人的名字1或2,问你有多少种s,t.
思路:枚举s, t就会被确定,但一一遍历的话会到O(n^2)的复杂度,对于10^5是超时的,所以遍历就利用了一个小技巧,以t为间隔记录对应序号跳跃遍历(自己瞎扯的).这样就能在规定时间完成。
代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int N = 200005;
int a[N],win[N][3],xu[N][3],s[3];//xu[i][k]代表k人胜利i局在比赛进行的局数
vector<pair<int,int> > ans;
int main()
{
    int n;
    cin >> n;
    for(int i = 1; i < N; i++)//这里都先初始最大化,便于tmp1,tmp2比较
        xu[i][1] = xu[i][2]= N;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        win[i][1] = win[i-1][1];
        win[i][2] = win[i-1][2];
        win[i][a[i]]++;
        xu[win[i][a[i]]][a[i]] = i;
    }
    for(int t=1; t<=n; t++)
    {
        bool flag=false;s[1]=s[2]=0;
        for(int k=0; k<=n;)
        {
            if(k==n){flag=true;break;}
            int tmp1=xu[win[k][1]+t][1];
            int tmp2=xu[win[k][2]+t][2];
            k = min(tmp1,tmp2);
            if(tmp1<tmp2)
                s[1]++;
            else
                s[2]++;
        }
        if(flag&&s[a[n]]>s[a[n]%2+1])
            ans.push_back(make_pair(s[a[n]],t));
    }
    sort(ans.begin(),ans.end());
    cout << ans.size()<<endl;
    for(int i=0;i<ans.size();i++)
        cout<<ans[i].first<<" "<<ans[i].second<<endl;
    return 0;
}
View Code
 
 
posted @ 2015-02-11 20:35  Doli  阅读(152)  评论(0)    收藏  举报