• Codeforces Round #283 (Div. 2) D. 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.

    Examples
    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
    【分析】进行若干场比赛,每次比赛两人对决,赢的人得到1分,输的人不得分,先得到t分的人获胜,开始下场比赛,某个人率先赢下s场比赛时,游戏结束。
    现在给出n次对决的记录,问可能的s和t有多少种,并按s递增的方式输出。
    可以想到枚举t,然后挨个找每一场结束的位置。但是从头遍历肯定会超时,所以需要预处理一下。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #define inf 0x3f3f3f3f
    #define met(a,b) memset(a,b,sizeof a)
    #define pb push_back
    typedef long long ll;
    using namespace std;
    const int N = 1e6+10;
    const int M = 1e6+10;
    int n,m,k,cnt[3];
    int p[3][N],a[N],pp[3][N];
    struct man{
        int s,t;
    }ans[N];
    bool cmp(man d,man f){
        if(d.s==f.s)return d.t<f.t;
        return d.s<f.s;
    }
    int main() {
        int x,y,tot=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&x);
            cnt[x]++;
            p[x][cnt[x]]=i;
            pp[1][i]=cnt[1];
            pp[2][i]=cnt[2];
        }
        bool ok=false;
        int s1=0,s2=0;
        for(int i=1;i<=n;i++){
            x=y=0;
            int g1=0,g2=0;
            int u,v;
            while(1){
                x+=i;y+=i;
                u=p[1][x];
                v=p[2][y];
                if(u==v)break;
                else if(u==0){
                    g2++;
                    x=pp[1][v];
                    y=pp[2][v];
                    if(v==n)break;
                }
                else if(v==0){
                    g1++;
                    x=pp[1][u];
                    y=pp[2][u];
                    if(u==n)break;
                }
                else if(u<v){
                    g1++;
                    x=pp[1][u];
                    y=pp[2][u];
                }
                else if(v<u){
                    g2++;
                    x=pp[1][v];
                    y=pp[2][v];
                }
            }
            if(u!=n&&v!=n)continue;
            if(g1!=g2){
                ok=true;
                if(g1>g2&&u==n){
                    ans[++tot].s=max(g1,g2);
                    ans[tot].t=i;
                }
                else if(g2>g1&&v==n){
                    ans[++tot].s=max(g1,g2);
                    ans[tot].t=i;
                }
            }
        }
        if(!ok)puts("0");
        else {
            sort(ans+1,ans+tot+1,cmp);
            printf("%d
    ",tot);
            for(int i=1;i<=tot;i++){
                printf("%d %d
    ",ans[i].s,ans[i].t);
            }
        }
        return 0;
    }
  • 相关阅读:
    javascript的字段值,私有变量,静态方法声明
    取得序列中某个点的范围边界
    使用wubi安装ubuntu11.04后无线网卡被禁用无法打开解决办法
    WEB前端性能优化
    jQuery的arrayLike
    在手机上安装BT5系统,没错就是破解无线密码的那个
    PHP在子类方法B调用父类的方法A时,不传参数时仍能得到方法B的名称
    讨厌的文本选区
    PHP遍历解析XML为一个数组
    “软工厂代码生成工具”的学习笔记
  • 原文地址:https://www.cnblogs.com/jianrenfang/p/6484941.html
Copyright © 2020-2023  润新知