zoukankan      html  css  js  c++  java
  • Codeforces 497B Tennis Game( 枚举+ 二分)

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

    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 。

    怎么判断s呢,当然就是找s的倍数 lar 。

    然后再进行二分出两个玩家拿到结束当局游戏的分数 , 位置比较前的就是赢家 。

    这题是我打那么多场DIV1最可惜的一题 , 卡在了边缘 。没有判断第n 个位置一定是要赢家得分的 。

    而且注意 , 最后得分的人赢的游戏盘数一定比另外一个人多才符合要求 。 

    复杂度就是 n * log n * log n ( 枚举分数 t , 枚举 t 的倍数 , 二分 分数的位置 ) 。

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 100010;
    const int INF = 1e9+7;
    typedef pair<int,int> pii;
    #define X first
    #define Y second
    int n , a[N], cnt1[N] , cnt2[N] ;
    vector<pii>ANS;
    
    int Find( int sc , int *A ) {
    
        int l = 0 , r = n , pos = INF ;
        if( A[n] < sc ) return pos;
        while( l <= r ){
            int mid = (l+r)>>1;
            if( A[mid] < sc )
                l = mid + 1 ;
            else
                pos = mid , r = mid - 1 ;
        }
        return pos ;
    }
    
    int check( int t ) {
    
        int ans = 0 , sc1 = 0 , sc2 = 0 ;
        int c1 = 0 , c2 = 0 ;
        while( 1 ){
            int pos1 = Find( sc1 + t , cnt1 );
            int pos2 = Find( sc2 + t , cnt2 );
            if( pos1 == INF && pos2 == INF ) return -1 ;
            if( pos1 == INF ) {
                if( ( cnt2[n] - sc2 ) %t || a[n] == 1 ) return -1 ;
                else {
                    c2 += ( cnt2[n] - sc2 ) / t ;
                    if( c1 >= c2 ) return -1 ;
                    return c2 ;
                }
            }
            if( pos2 == INF ) {
                if( ( cnt1[n] - sc1 ) %t || a[n] == 2 ) return -1 ;
                else {
                    c1 += ( cnt1[n] - sc1 ) / t ;
                    if( c2 >= c1 ) return -1 ;
                    return c1 ;
                }
            }
            if( pos1 < pos2 ) c1++ , sc1 = cnt1[pos1] , sc2 = cnt2[pos1];
            else c2++ , sc1 = cnt1[pos2] , sc2 = cnt2[pos2] ;
        }
    }
    
    int main(){
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        while( cin >> n ){
            cnt1[0] = cnt2[0] = 0 ;
            for( int i = 1 ; i <= n ; ++i ) {
                cin >> a[i] ;
                if( a[i] == 1 ) cnt1[i] = cnt1[i-1]+1 , cnt2[i] = cnt2[i-1];
                else cnt1[i] = cnt1[i-1] , cnt2[i] = cnt2[i-1]+1;
            }
            int tot = max( cnt1[n] , cnt2[n] ) , ans = 0 ;
            ANS.clear();
            for( int i = 1 ; i <= tot ; ++i ) {
                int res = check(i);
                if( res != -1 ) {
                    ans++; ANS.push_back( pii( res , i ) );
                }
            }
            sort( ANS.begin() , ANS.end() ) ;
            cout << ans << endl ;
            for( int i = 0 ; i < ans ; ++i ) {
                cout << ANS[i].X <<' ' << ANS[i].Y << endl ;
            }
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    题解[LuoguP7419 「PMOI-2」参天大树]
    UVA11582 巨大的斐波那契数! Colossal Fibonacci Numbers!
    数学专题
    [计蒜客]dp
    [蓝桥杯每日一题]1.3 & 1.4
    【acm】2020icpc南京补题
    [acm]乐师师范学院校赛题解-2020
    西南交通大学峨眉校区第二届"INT"杯程序设计竞赛——决赛
    指针与结构体
    [acm] 动态规划——最长上升子序列
  • 原文地址:https://www.cnblogs.com/hlmark/p/4172458.html
Copyright © 2011-2022 走看看