AtCoder Beginner Contest 272

A - Integer Sum

签到题,求和就好

#include<bits/stdc++.h>

using namespace std;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

int32_t main() {
    int n = read() , sum = 0;
    for( int x ; n ; n -- )
        x = read() , sum += x;
    cout << sum << '\n';
    return 0;
}

B - Everyone is Friends

对于每一组,\(O(n^2)\)的枚举两个人,然后标记他们的关系。最后遍历任意两人之间的关系

#include<bits/stdc++.h>

using namespace std;


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 105;
int a[N];
bool v[N][N];

int32_t main() {
    int n = read() , m = read();
    for( int k ; m ; m -- ){
        k = read();
        for( int j = 1 ; j <= k ; j ++ )
            a[j] = read();
        for( int i = 1 ; i <= k ; i ++ )
            for( int j = 1 ; j < i ; j ++ )
                v[ a[i] ][ a[j] ] = v[ a[j] ][ a[i] ] = 1;
    }
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j < i ; j ++ ){
            if( v[i][j] ) continue;
            cout << "No\n";
            return 0;
        }
    cout << "Yes\n";
    return 0;
}

C - Max Even

找到奇数的最大和次大,偶数的最大和次大。然后取max

#include<bits/stdc++.h>
#define int long long
using namespace std;


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 105;
int a = INT_MIN , b = INT_MIN , c = INT_MIN , d = INT_MIN;

int32_t main() {
    int n = read();
    for( int x ; n ; n -- ){
        x = read();
        if( x & 1 ){
            if( x > a ) b = a , a = x;
            else if( x > b ) b = x;
        }
        else{
            if( x > c ) d = c , c = x;
            else if( x > d ) d = x;
        }
    }
    int res = max( a+b , c + d );
    if( res < 0 ) cout << -1;
    else cout << res << "\n";
    return 0;
}

D - Root M Leaper

算出所有移动距离为\(\sqrt m\)的移动向量,然后就是bfs一下啊就好

#include<bits/stdc++.h>
//#define int long long
using namespace std;


int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 405;

int n , m , dis[N][N];
vector< pair<int,int> > e;

int32_t main() {
    n = read() , m = read();
    for( int i = 0 , j ; i <= m ; i ++ ){
        j = (int)sqrt( m - i * i );
        if( i * i + j * j == m ) {
            e.emplace_back( i , j );
            e.emplace_back( i , -j );
            e.emplace_back( -i , j );
            e.emplace_back( -i , -j );
        }
    }
    fill( dis[0] , dis[0] + N * N , INT_MAX );
    dis[1][1] = 0;
    queue< pair<int,int> > q;
    q.push( { 1 , 1 } );
    while( !q.empty() ){
        auto [ x , y ] = q.front() ; q.pop();
        for( auto [ dx , dy ] : e ){

            int fx = x + dx , fy = y + dy;
            if( fx < 1 || fx > n || fy < 1 || fy > n || dis[fx][fy] <= dis[x][y] + 1 ) continue;
            dis[fx][fy] = dis[x][y] + 1 , q.push( { fx , fy });
        }
    }
    for( int i = 1 ; i <= n ; i ++ )
        for( int j = 1 ; j <= n ; j ++ )
            cout << ( dis[i][j] == INT_MAX ? -1 : dis[i][j] ) << ( j == n ? '\n' : ' ' );
    return 0;
}

E - Add and Mex

每一个数只能加到大于等于0时才有贡献,加到大于n时就不会再有贡献,所以二分出最小的可以贡献的值,然后暴力加到没有贡献,再此过从中保存下所有可能产生贡献的值。

对于每次操作,暴力一下循环一下就可以找到mex

#include<bits/stdc++.h>
#define int long long
using namespace std;

int read() {
    int x = 0, f = 1, ch = getchar();
    while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
    if (ch == '-') f = -1, ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
    return x * f;
}

const int N = 2e5+5;
int n , m;
vector<int> p[N];

int search( int i , int x ){
    int l = 1 , r = m , res , mid ;
    while( l <= r ){
        mid = ( l + r ) >> 1;
        if( mid * i + x >= 0 ) res = mid , r = mid - 1;
        else l = mid + 1;
    }
    return res;
}

int32_t main() {
    n = read() , m = read();
    for( int i = 1 , x , t ; i <= n ; i ++ ){
        x = read() ;
        if( m * i + x < 0 ) continue;
        t = search( i , x ) ;
        for( int j = t ; j <= m ; j ++ ){
            if( j * i + x > n ) break;
            p[j].push_back( j * i + x ) ;
        }

    }
    for( int i = 1 , res ; i <= m ; i ++ ){
        res = 0;
        sort( p[i].begin() , p[i].end() );
        for( auto it : p[i] ){
            if( it == res ) res ++;
            else if( it < res ) continue;
            else break;
        }
        cout << res << "\n";
    }
    return 0;
}

AtCoder Beginner Contest 272

posted @ 2022-10-10 11:17  PHarr  阅读(61)  评论(0编辑  收藏  举报