2018 “百度之星”程序设计大赛 - 初赛(B)1001模拟 1004 二分 1006 模拟

1001
Code:

#include <bits/stdc++.h>
using namespace std;
const int AX = 2e5+66;
std::vector<int> v[AX];
int main(){
    int n , m , k ;
    int T;
    scanf("%d",&T);
    while(T--){
        int x , y;
        scanf("%d%d%d",&n,&m,&k);
        for( int i = 0 ; i < n ; i++ ){
            v[i].clear();
        }
        for(int i = 0 ; i < m ; i++ ){
            scanf("%d%d",&x,&y);
            v[x].push_back(y);
            v[y].push_back(x);
        }
        int ans = 0  ;
        for( int i = 0 ; i < n ; i ++ ){
            if( v[i].size() > ans ) {
                ans = v[i].size();
                x = i ;
            }
        }
        int res = m-ans;
        if( res >= k ){
            printf("%d\n",n - ( res - k ) - 1 );
        }else{
            printf("%d\n", n - 1 );
        }
    }
    return 0;
}

1004

Code:

#include <bits/stdc++.h>
#define LL long long
#define INF 0x3f3f3f3f 
using namespace std;
const int AX = 3e5+66;
LL a[AX];
int n ;

int f( LL x ){
    int pos = upper_bound( a , a + n , x ) - a ;
    if( !(a[pos] > x) ) return 2;
    LL ans1 = 0 ;
    int tmp = 0 ;
    for( int i = pos ; i < n ; i++ ){
        if( ( a[i] - x ) % 2 == 0 ){
            ans1 += ( a[i] - x ) / 2 ; 
            tmp = 1 ;
        }else ans1 += ( a[i] - x - 1 ) / 2 ;
    }
    LL ans2 = 0 ;
    for( int i = 0 ; i < pos ; i++ ){
        if( a[i] == x ) break;
        ans2 += ( x - a[i] );
    }
    if( ans2 == ans1 || ( ans1 >= ans2 && ans1 < ans2 + pos ) ){
        return 1 ;
    }else if( ans1 < ans2 ){
        return 2 ;
    }else{
        if( ans1 >= ans2 + pos && tmp ) return 1 ;
        else return 3 ;
    }
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        int x ; 
        LL maxn = 0 ;
        for( int i = 0 ; i < n ; i++ ){
            scanf("%I64d",&a[i]);
        }
        if( n == 1 ) { printf("%d\n",a[0]); continue;}
        //if( n == 2 && a[0] == a[1] ) { printf("%d\n",a[0]); continue;}
        sort( a , a + n );
        LL l = a[0] , r = a[n-1] ;
        //LL res = -1;
        while( l + 1 < r ){    
            LL mid = ( l + r ) >> 1 ;
            int flag = f(mid);
            if( flag == 1 ){
                //res = max( res , mid );
                l = mid  ;
            }else if( flag == 2 ){
                r = mid  ;
            }else{
                l = mid  ;
            }
        }
        printf("%I64d\n",l);
    }
    return 0;
}

1006

Code:

#include <bits/stdc++.h>
#pragma comment(linker, “/STACK:1024000000,1024000000”)
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int AX = 2e4+66;

int main(){
    int T;
    scanf("%d",&T);
    int MX , MY , n;
    int x , y ;
    while( T-- ){
        scanf("%d%d%d",&MX,&MY,&n);
        LL res = 0 ;
        for( int i = 0 ; i < n ; i++ ){
            cin >> x >> y ;
            res += min( x ,min( y ,min(MX-x,MY-y)));
        }
        cout << res << endl;
    }
    return 0 ;
}

猜你喜欢

转载自blog.csdn.net/FrankAx/article/details/81610959