BZOJ-1271: [BeiJingWc2008]秦腾与教学评估(二分查找)

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1271

由于最多只有一个奇数,那么就二分前缀和即可。

代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
 
using namespace std ;
 
#define rep( i , x ) for ( int i = 0 ; i ++ < x ; )
 
typedef unsigned int uint ;
 
const int maxn = 200100 ;
 
int S[ maxn ] , E[ maxn ] , D[ maxn ] , n ;
 
int count( int x ) {
    int cnt = 0 ;
    rep( i , n ) if ( S[ i ] <= x ) {
        if ( E[ i ] <= x ) cnt += ( E[ i ] - S[ i ] ) / D[ i ] + 1 ;
        else cnt += ( x - S[ i ] ) / D[ i ] + 1 ; 
    }
    return cnt ;
}
 
int main(  ) {
    int tot ; scanf( "%d" , &tot ) ;
    while ( tot -- ) {
        scanf( "%d" , &n ) ;
        rep( i , n ) scanf( "%d%d%d" , S + i , E + i , D + i ) ;
        uint l = 0 , r = 0 , mid , maxe ;
        rep( i , n ) r = max( r , uint( E[ i ] ) + 1 ) ;
        maxe = r ; 
        while ( r - l > 1 ) {
            mid = ( l + r ) >> 1 ; 
            if ( ! ( count( mid ) & 1 ) ) l = mid ; else r = mid ;
        }
        if ( r == maxe ) printf( "Poor QIN Teng:(\n" ) ; 
        else printf( "%d %d\n" , r , count( r ) - count( r - 1 ) ) ;
    }
    return 0 ; 
}

    原文作者:AmadeusChan
    原文地址: https://www.jianshu.com/p/f1c16514e7e6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞