• CodeForces Round #516 Div2 题解


    A. Make a triangle!

    暴力...

    就是给你三个数,你每次可以选一个加1,问最少加多少次能构成三角形

    #include <bits/stdc++.h>
    
    #define ll long long
    #define inf 0x3f3f3f3f 
    #define il inline 
    #define in1(a) read(a)
    #define in2(a,b) in1(a),in1(b)
    #define in3(a,b,c) in2(a,b),in1(c)
    #define in4(a,b,c,d) in2(a,b),in2(c,d)
    
    inline void read( int &x ){
        x = 0 ; int f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    inline void readl( ll &x ){
        x = 0 ; ll f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    using namespace std ;
    
    #define N 100010
    
    int a , b , c ;
    
    int main(){
        in3( a ,b , c ) ;
        if( a > b ) swap( a , b ) ;
        if( b > c ) swap( b , c ) ;
        if( a > c ) swap( a , c ) ;
        int ans = 0 ;
        while( a + b <= c ) {
            a ++ ; 
            ans ++ ;
            if( a > b ) swap( a , b ) ;
        }
        printf( "%d
    " , ans ) ;
        return  0;
    }

    B. Equations of Mathematical Magic

    求满足式子$a-(a xor x)-x=0$的$x$值的数量

    $a<=2^{30}-1$

    对于某一位的$a$和$x$

    设$a$这一位上的数字为$a_i$,$x$这一位上的数字为$x_i$

    对于$a_i=0$,只有$x_i=0$才成立

    对于$a_i=1$,$x_i=0$或者$x_i=1$均成立

    所以乘法原理乘一下就好了

    ZincSabian聚聚抬了一手(orz我B题就不会了

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    #define ll long long
    #define inf 0x3f3f3f3f 
    #define il inline 
    #define in1(a) readl(a)
    #define in2(a,b) in1(a),in1(b)
    #define in3(a,b,c) in2(a,b),in1(c)
    #define in4(a,b,c,d) in2(a,b),in2(c,d)
    
    inline void read( int &x ){
        x = 0 ; int f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    inline void readl( ll &x ){
        x = 0 ; ll f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    using namespace std ;
    
    #define N 100010
    
    ll T , a ;
    
    int main() {
        in1( T ) ;
        while( T -- ) {
            in1( a ) ;
            ll cnt = 1 ;
            for( int i = 40 ; i >= 0 ; i -- ) {
                if( a&(1ll<<i) ) cnt *= 2ll ;
            }
            printf( "%lld
    " , cnt ) ;
        }
    }

    C. Oh Those Palindromes

    怎么B,C都是结论题啊

    C题直接把所有一样的数排在一起就好了...

    因为这样确实就能排出最大的回文串了

    可以找几个字符串自己写写画画一下,感性理解

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    #define ll long long
    #define inf 0x3f3f3f3f 
    #define il inline 
    #define in1(a) read(a)
    #define in2(a,b) in1(a),in1(b)
    #define in3(a,b,c) in2(a,b),in1(c)
    #define in4(a,b,c,d) in2(a,b),in2(c,d)
    
    inline void read( int &x ){
        x = 0 ; int f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    inline void readl( ll &x ){
        x = 0 ; ll f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    using namespace std ;
    
    #define N 100010
    
    int n ;
    char ch[ N ] ;
    
    int main(){
        in1( n ) ;
        scanf( "%s" , ch+1 ) ;
        sort( ch+1 , ch+n+1 ) ;
        printf( "%s" , ch+1 ) ;
    }

    D. Labyrinth

    赛时没调出来...

    然后赛后3min调出来

    $system test$ 完后交了就过了

    哭...掉分了

    唔...其实这题就是建两个图,对于第一个图只有向左走才会花费代价,对于第二个图只有向右走才会花费代价

    然后就是码码码了,我写了$4KB$...

    写的$spfa$,网格图居然没卡$spfa$...

    代码可能有点丑...

    将就着看吧

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    #define ll long long
    #define debug printf("233
    ")
    #define inf 0x3f3f3f3f 
    #define il inline 
    #define in1(a) read(a)
    #define in2(a,b) in1(a),in1(b)
    #define in3(a,b,c) in2(a,b),in1(c)
    #define in4(a,b,c,d) in2(a,b),in2(c,d)
    
    inline void read( int &x ){
        x = 0 ; int f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    inline void readl( ll &x ){
        x = 0 ; ll f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    using namespace std ;
    
    #define N 2010
    
    int n , m , r , c , x , y ;
    char ch[ N ][ N ] ;
    struct edge {
        int to , nxt , v ;
    } e[ N * N * 4 ] , E[ N * N * 4 ];
    int head[ N * N ] , cnt , Head[ N * N ] , Cnt ;
    int vis[ N * N ] , d[ N * N ] , Vis[ N * N ] , D[ N * N ] ;
    int q[ 1000100 ] ;
    
    void ins1( int u , int v , int w ) {
        e[ ++ cnt ].to = v ;
        e[ cnt ].nxt = head[ u ] ;
        e[ cnt ].v = w ;
        head[ u ] = cnt ;
    }
    
    void ins2( int u , int v , int w ) {
        E[ ++ Cnt ].to = v ;
        E[ Cnt ].nxt = Head[ u ] ;
        E[ Cnt ].v = w ;
        Head[ u ] = Cnt ;
    }
    
    void spfa() {
        int s = (r-1) * m + c ;
        q[ 1 ] = s ;
        int l = 1 , r = 2 ;
        for( int i = 1 ; i <= n * m ; i ++ ) d[ i ] = inf ;
        d[ s ] = 0 ;
        vis[ s ] = 1 ;
        while( l != r ) {
            int u = q[ l ++ ] ;
            vis[ u ] = 0 ;
            if( l == 1000000 ) l = 1 ;
            for( int i = head[ u ] ; i ;  i = e[ i ].nxt ) {
                int v = e[ i ].to ;
                if( d[ v ] > d[ u ] + e[ i ].v ) {
                    d[ v ] = d[ u ] + e[ i ].v ;
                    if( !vis[ v ] ) {
                        vis[ v ] = 1 ; 
                        q[ r ++ ] = v ;
                        if( r == 1000000 ) r = 1 ;
                    }
                }
            }
        }
    }
    
    void spfa2() {
        int s = (r-1) * m + c ;
        int l = 1 , r = 2 ;
        q[ 1 ] = s ;Vis[ s ] = 1 ;
        for( int i = 1 ; i <= n * m ; i ++ ) D[ i ] = inf ;
        D[ s ] = 0 ;
        while( l != r ) {
            int u = q[ l ++ ] ;
            Vis[ u ] = 0 ;
            if( l == 1000000 ) l = 1 ;
            for( int i = Head[ u ] ; i ;  i = E[ i ].nxt ) {
                int v = E[ i ].to ;
                if( D[ v ] > D[ u ] + E[ i ].v ) {
                    D[ v ] = D[ u ] + E[ i ].v ;
                    if( !Vis[ v ] ) {
                        Vis[ v ] = 1 ; 
                        q[ r ++ ] = v ;
                        if( r == 1000000 ) r = 1 ;
                    }
                }
            }
        }
    }
    
    int main(){
        in2( n , m ) ;
        in2( r , c ) ;
        in2( x , y ) ;
        for( int i = 1 ; i <= n ; i ++ ) {
            scanf( "%s" , ch[ i ] + 1 ) ;
        }
        for( int i = 1 ; i <= n ; i ++ ) {
            for( int j = 1 ; j <= m ; j ++ ) {
                if( ch[ i ][ j ] == '*' ) continue ;
                if(i-1>=1&&ch[i-1][j]=='.') ins1((i-1)*m+j,(i-2)*m+j,0),  ins2((i-1)*m+j,(i-2)*m+j,0);
                if(j-1>=1&&ch[i][j-1]=='.') ins1((i-1)*m+j,(i-1)*m+j-1,1),ins2((i-1)*m+j,(i-1)*m+j-1,0);
                if(i+1<=n&&ch[i+1][j]=='.') ins1((i-1)*m+j,i*m+j,0),      ins2((i-1)*m+j,i*m+j,0);
                if(j+1<=m&&ch[i][j+1]=='.') ins1((i-1)*m+j,(i-1)*m+j+1,0),ins2((i-1)*m+j,(i-1)*m+j+1,1);
            }
        }
        spfa() ;
        spfa2() ;
        int ans = 0 ;
        for( int i = 1 ; i <= n * m ; i ++ ) {
            if( d[ i ] <= x && D[ i ] <= y ) ans ++ ; 
        }
        printf( "%d
    " , ans ) ;
    }

    E. Dwarves, Hats and Extrasensory Abilities

    这题有点思路,晚上补一下


    因为题目保证了白的在一边黑的在一边

    所以我们可以二分这个分界点

    然后输出的时候,纵坐标随便找两个,尽量让他们连线与坐标轴成45度角那样子

    反正不要太歪,我取的纵坐标是$0$和$2$

    就是真的很不习惯交互的形式

    #include <bits/stdc++.h>
    
    #define ll long long
    #define inf 0x3f3f3f3f 
    #define il inline 
    #define in1(a) readl(a)
    
    inline void readl( ll &x ){
        x = 0 ; ll f = 1 ; char c = getchar() ;
        while( c < '0' || c > '9' ) {
            if( c == '-' ) f = -1 ;
            c = getchar() ;
        }
        while( c >= '0' && c <= '9' ) {
            x = (x << 1) + (x << 3) + c - 48 ;
            c = getchar() ;
        }
        x *= f ;
    }
    
    using namespace std ;
    
    #define N 100010
    
    ll n ;
    ll x , y ;
    char s[ N ] ;
    
    int main() {
        in1( n ) ;
        puts( "0 0" ) ;
        fflush( stdout ) ;
        scanf( "%s" , s ) ;
        char tmp = s[ 0 ] ;
        ll l = 1 , r = 1e9 ;
        for( int i = 2 ; i <= n ; i ++ ) {
            ll mid = ( l + r ) >> 1 ;
            printf( "%lld %lld
    " , mid , 1ll ) ;
            fflush( stdout ) ;
            scanf( "%s" , s ) ;
            if( s[ 0 ] == tmp ) l = mid + 1 ;
            else r = mid - 1 ;
        }
        printf( "%lld 0 %lld 2
    " , l , r ) ;
    }

    F. Candies for Children

    以后补...

  • 相关阅读:
    [转] css3变形属性transform
    [转] ReactJS之JSX语法
    [转] 那些在使用webpack时踩过的坑
    [转] jQuery的deferred对象详解
    [转] Webpack-CommonsChunkPlugin
    [转] 用webpack的CommonsChunkPlugin提取公共代码的3种方式
    Refs & DOM
    [转] Webpack的devtool和source maps
    [转] 编译输出文件的区别
    GDB && QString
  • 原文地址:https://www.cnblogs.com/henry-1202/p/CF1064-Solution.html
Copyright © 2020-2023  润新知