zoukankan      html  css  js  c++  java
  • 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 increasingsi, 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
    暴力的基础上二分优化;
    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int a[N];
    int sum1[N];
    int sum2[N];
    int check(int l,int r,int x)
    {
        if(sum1[r]-sum1[l-1]==x&&sum2[r]-sum2[l-1]<x&&a[r]==1)
        return 1;
        if(sum2[r]-sum2[l-1]==x&&sum1[r]-sum1[l-1]<x&&a[r]==2)
        return 2;
        if(sum1[r]-sum1[l-1]>=x||sum2[r]-sum2[l-1]>=x)
        return 3;
        return 0;
    }
    struct is
    {
        int l,r;
        bool operator <(const is &b)const
        {
            if(l!=b.l)
            return l<b.l;
            return r<b.r;
        }
    }ans[N];
    int out;
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            a[i]=x;
            sum1[i]=sum1[i-1];
            sum2[i]=sum2[i-1];
            if(x==1)
                sum1[i]++;
            if(x==2)
                sum2[i]++;
        }
        sum1[n+1]=sum1[n],sum2[n+1]=sum2[n];
        for(int t=1;t<=n;t++)
        {
            cout<<t<<" ~~~~~"<<endl;
            int s=1,one=0,two=0;
            while(1)
            {
                cout<<s<<endl;
                if(s==n+1&&one!=two)
                {
                    if(two>one&&a[n]==1)
                    break;
                    if(one>two&&a[n]==2)
                    break;
                    ans[out].l=max(one,two);
                    ans[out++].r=t;
                    break;
                }
                if(check(s,n,t)==0)
                break;
                int st=s;
                int en=n;
                while(st<en)
                {
                    int mid=(st+en)>>1;
                    cout<<mid<<" mid"<<endl;
                    int v=check(s,mid,t);
                    if(v==1||v==2)
                    {
                        st=mid;
                        break;
                    }
                    else if(v==3)
                    en=mid;
                    else
                    st=mid+1;
                }
                if(check(s,st,t)==1)
                one++;
                else
                two++;
                s=st+1;
            }
        }
        printf("%d
    ",out);
        sort(ans,ans+out);
        for(int i=0;i<out;i++)
            printf("%d %d
    ",ans[i].l,ans[i].r);
        return 0;
    }
    /*
    5
    1 2 1 2 1
    14
    2 1 2 1 1 1 1 2 1 1 2 1 2 1
    20
    1 1 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 1
    
    */
  • 相关阅读:
    bash:加减乘除(bc、let)
    shell配置文件
    HDU4624 Endless Spin 【最大最小反演】【期望DP】
    SPOJ10707 COT2
    HDU5977 Garden of Eden 【FMT】【树形DP】
    Codeforces1023E Down or Right 【贪心】
    Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
    UOJ272 [清华集训2016] 石家庄的工人阶级队伍比较坚强 【分治乘法】
    LOJ2721 [NOI2018] 屠龙勇士 【扩展中国剩余定理】
    UOJ268 [清华集训2016] 数据交互 【动态DP】【堆】【树链剖分】【线段树】
  • 原文地址:https://www.cnblogs.com/jhz033/p/5926028.html
Copyright © 2011-2022 走看看