Educational Codeforces Round 49 (Rated for Div. 2) A B模拟C数学 D搜索

版权声明:本文为博主原创文章,未经博主允许也可以转载。 https://blog.csdn.net/FrankAx/article/details/82316710

A
题意:一个字母可以替换成前面的或者后面的,每个字母必须替换一次,问能不能替换成回文。
Code:

#include <bits/stdc++.h>
#define LL long long 
using namespace std;
const int AX = 1e2+6;
char s[AX];
int a[AX];
int main(){
    int T ;
    int n ;
    scanf("%d",&T);
    while( T-- ){
        scanf("%d",&n);
        scanf("%s",s+1);
        for( int i = 1 ; i <= n ; i++ ){
            a[i] = s[i] - 'a' ;
        } 
        int i ; 
        for( i = 1 ; i <= n / 2 ; i++ ){
            int r = n - i + 1 ;
            int f = 0 ; 
            if( a[i] == a[r] )  continue;
            if( a[i] == 0 ){
                if( a[r] != 25 ){
                    if( a[i] + 1 == a[r] + 1 || a[i] + 1 == a[r] - 1 ){
                        f = 1 ;
                    }
                }else{
                    if( a[i] + 1 == a[r] - 1 ){
                        f = 1 ;
                    }
                }
            }else if( a[i] == 25 ){
                if( a[r] != 0 ){
                    if( a[i] - 1 == a[r] + 1 || a[i] - 1 == a[r] - 1 ){
                        f = 1 ; 
                    }
                }else{
                    if( a[i] - 1 == a[r] + 1 ) f = 1;  
                }
            }else{
                if( a[i] - 1 == a[r] + 1 || a[i] - 1 == a[r] - 1 ) f = 1;  
                if( a[i] + 1 == a[r] + 1 || a[i] + 1 == a[r] - 1 ) f = 1; 
            }
            if( !f ) { printf("NO\n");  break; }
        }
        if( i == n / 2 + 1 ) printf("YES\n");
    }
    return 0 ; 
}

B
题意:n*n的方格,前n*n/2(向上取整)个数填到(x,y)其中x,y奇偶性同,剩下的数填到剩下的格子,按照顺序。
思路:分情况讨论下。
Code:

#include <bits/stdc++.h>
#define LL long long 
using namespace std;
int main(){
    ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0);
    LL n ; 
    int q ; 
    cin >> n >> q ; 
    LL m = n * n ; 
    LL x , y ; 
    if( m % 2 ){
        LL lim = m / 2 + 1 ;
        LL t1 = n / 2 + 1 ;
        LL t2 = n - t1 ;
        for( int i = 0 ; i < q ; i++ ){
            cin >> x >> y ;
            LL tmp ;
            if( ( x & 1 ) == ( y & 1 ) ){
                int a1, a2;
                if( x % 2 ) { a1 = ( x / 2 + 1 ); a2 = x / 2; }
                else a1 = x / 2 , a2 = x / 2 ; 
                tmp = a1 * t1 + a2 * t2 ;
                x %= 2 ;
                if( x ) tmp -= t1 ;
                else tmp -= t2 ;
                if( x == 0 ){
                    tmp += y / 2 ; 
                }else{
                    tmp += ( ( y + 1 ) / 2 );
                }
            }else{
                int a1, a2;
                if( x % 2 ) { a1 = ( x / 2 ); a2 = x / 2 + 1 ; }
                else a1 = x / 2 , a2 = x / 2 ;  
                tmp = a1 * t1 + a2 * t2 ;
                x %= 2 ;
                if( x ) tmp -= t2 ;
                else tmp -= t1 ;
                if( x == 0 ){
                    tmp += ( y + 1 ) / 2 ;
                }else{
                    tmp += y / 2 ; 
                }
                tmp += lim ;
            } 
            cout << tmp << endl;
        }
    }else{
        LL t = n / 2 ;
        LL lim = m / 2 ;
        for( int i = 0 ; i < q ; i++ ){
            cin >> x >> y ; 
            LL tmp ;
            if( ( x & 1 ) == ( y & 1 ) ){
                tmp = (LL)x * t ; 
                x %= 2 ;
                tmp -= t ;
                if( x == 0 ){
                    tmp += ( y / 2 ) ;
                }else{
                    tmp += ( ( y + 1 ) / 2 );
                }
            }else{
                tmp = (LL)x * t ; 
                x %= 2 ;
                tmp -= t ;
                if( x == 0 ){
                    tmp += ( y + 1 ) / 2 ;
                }else{
                    tmp += ( y / 2 ) ;
                }
                tmp += lim ;
            }
            cout << tmp << endl;
        }
    }
    return 0 ;
}

C
题意:从给定的长度选取4条边,使得构成的矩形周长的平方/ 面积最小。
思路:均值不等式可得a=b时最小。
Code:

#include <bits/stdc++.h>
#define LL long long 
using namespace std;
const int AX = 1e6+66;
int a[AX];
int b[AX];
map<int,int>mp;
int main(){
    int T , n ;
    scanf("%d",&T);
    while( T-- ){
        int tot = 0 ;
        scanf("%d",&n);
        mp.clear();
        for( int i = 0 ; i < n ; i++ ){
            scanf("%d",&a[i]);
            mp[a[i]]++;
            if( mp[a[i]] == 2 ){
                b[tot++] = a[i];
                mp[a[i]] = 0 ;
            }
        }
        double minus = 1000000000.0;
        int x , y ;
        sort( b , b + tot ) ;
        for( int i = 0; i < tot - 1 ; i++ ){
            int xx = b[i+1];
            int yy = b[i];
            double tmp = (double)( (double)xx / (double)yy );
            if( tmp < minus ){
                minus = tmp ; 
                x = b[i+1];
                y = b[i];
            }
        }
        printf("%d %d %d %d\n",x,x,y,y);
    }
    return 0 ; 
}

D
题意:每个点都有下一个能够到达的点,要求求出不论老鼠在哪个点出发,均能被捕捉的投放捕鼠器的所需要的花费。
思路:因为每个点只有一个能够直接达到的点,最后的情况只有一条链的最后一个点,或者一个环的最小值。

Code:

#include <bits/stdc++.h>
#define LL long long 
#define INF 0x3f3f3f3f 
using namespace std;
const int AX = 2e5+66;
int c[AX];
int nxt[AX];
int vis[AX];
int res ;
int ans ; 
int main(){
    int n ;
    scanf("%d",&n);
    for( int i = 1 ; i <= n ; i++ ){
        scanf("%d",&c[i]);
    }
    for( int i = 1 ; i <= n ; i++ ){
        scanf("%d",&nxt[i]);
    }
    for( int i = 1 ; i <= n ; i++ ){
        if( vis[i] ) continue; 
        int tmp = i ;
        while( 1 ){
            if( vis[tmp] && vis[tmp] != i ){
                ans = 0 ; 
                break;
            }
            if( vis[tmp] == i ){
                int x = tmp , k = nxt[x] ;
                ans = c[x]; 
                while( x != k ){
                    ans = min( ans , c[k] ) ;
                    k = nxt[k] ; 
                }break;
            } else if( nxt[tmp] == tmp ){
                vis[tmp] = i ;
                ans = c[tmp] ; break;
            }
            vis[tmp] = i ;
            tmp = nxt[tmp];
        }
        res += ans ; 
        //cout << res << endl;
    }
    printf("%d\n",res);
    return 0 ; 
}

猜你喜欢

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