AtCoder Beginner Contest 263

A - Full House

#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() {
    map< int , int > st;

    for( int i = 1 , x ; i <= 5 ; i ++){
        x = read() , st[x] ++;
    }
    if( st.size() != 2 ) {
        cout << "No\n";
        return 0;
    }
    vector < int > ve;
    int x = -1;
    for( auto [ k , v ] : st )
        x = max( x , v );
    if( x == 3 ){
        cout << "Yes\n";
    }
    else{
        cout << "No\n";
    }
    return 0;
}

B - Ancestor

因为每个人只有一个父亲,说从n开始向1搜索

#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 = 100;
int n , fa[N] , res;

void dfs( int x , int cnt ){
    if( x == 1 ) {
        cout << cnt << "\n";
        exit(0);
    }
    dfs( fa[x] , cnt + 1 );
}

int32_t main() {
    n = read();
    fa[1] = 1;
    for( int i = 2 ; i <= n ; i ++ )
        fa[i] = read();

    dfs( n , 0 );

    return 0;
}

C - Monotonically Increasing

打印出所有长度为n的递增序列,序列的元素来自[1,m],输出顺序按照字典序升序。

#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 = 100;
int n , a[N] , m;

void dfs( int x , int t ){
    if( t == n + 1 ){
        for( int i = 1 ; i <= n ; i ++ )
            cout << a[i] << " ";
        cout << "\n";
        return;
    }
    for( int i = x + 1 ; i <= m ; i ++ )
        a[t] = i , dfs( i , t + 1 );
    a[t] = 0;
}

int32_t main() {
    n = read() , m = read();
    dfs( 0 , 1 );

    return 0;
}

D - Left Right Operation

给一个序列,可以修改一下序列的前缀和后缀,问修改后的最小值。

首先求一下每一个位置的前缀最小值,把后缀全部改成r,对这个答案求一下最小值

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

int read() {...}

int32_t main() {
    int n = read() , l = read() , r = read();
    int res = n * r , pre = 0;
    for( int i = 1 , x ; i <= n ; i ++ )
        x = read() , pre = min( pre+x , l * i ) , res = min( res , pre + (n-i)*r );
    cout << res << "\n";
    return 0;
}
posted @ 2022-09-13 14:09  PHarr  阅读(25)  评论(0编辑  收藏  举报