2022 China Collegiate Programming Contest (CCPC) Mianyang Onsite

A.Ban or Pick, What’s the Trick

d p dp dp 状态为 d p [ i ] [ j ] [ k ] dp[ i ][ j ][ k ] dp[i][j][k] ,为在第 i i i 轮,A变了 j j j 次,B变了 k k k 次,然后直接开始搜索

#include

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int MX = 2e5+7;
ll rem[MX][11][11];
bool flag[MX][11][11];

vector<int> va, vb;
ll dfs(int cur, int a, int b) {
    if (flag[cur][a][b]) return rem[cur][a][b];
    if (cur & 1) {
        if (!va.empty() && a > 0) {
            ll x = va.back(); va.pop_back();
            rem[cur][a][b] = x + dfs(cur+1, a-1, b);
            flag[cur][a][b] = 1;
            va.push_back(x);
        }
        if (!vb.empty()) {
            ll x = vb.back(); vb.pop_back();
            if (flag[cur][a][b]) rem[cur][a][b] = max(rem[cur][a][b], dfs(cur+1, a, b));
            else rem[cur][a][b] =  dfs(cur+1, a, b);
            flag[cur][a][b] = 1;
            vb.push_back(x);
        }
    } else {
        if (!vb.empty() && b > 0) {
            ll x = vb.back(); vb.pop_back();
            rem[cur][a][b] = dfs(cur+1, a, b-1) - x;
            flag[cur][a][b] = 1;
            vb.push_back(x);
        }
        if (!va.empty()) {
            ll x = va.back(); va.pop_back();
            if (flag[cur][a][b]) rem[cur][a][b] = min(rem[cur][a][b], dfs(cur+1, a, b));
            else rem[cur][a][b] =  dfs(cur+1, a, b);
            va.push_back(x);
        }
    }
    return rem[cur][a][b];
}

void solve() {
    int n, k;
    cin >> n >> k;
    va.resize(n); vb.resize(n);
    for (int i = 0; i < n; i++) cin >> va[i];
    for (int i = 0; i < n; i++) cin >> vb[i];
    sort(va.begin(), va.end());
    sort(vb.begin(), vb.end());
    cout << dfs(1, k, k) << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) solve();
    return 0;
}

C. Catch You Catch Me

签到~

#include

using namespace std;

typedef unsigned long long ull;


const int MX = 1e5+7;
vector<int> edges[MX];
int dep[MX];
 
void dfs(int cur, int fa) {
    for (auto it : edges[cur]) {
        if (it == fa) continue;
        dfs(it, cur);
        dep[cur] = max(dep[cur], dep[it] + 1);
    }
}

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        int u, v;
        cin >> u >> v;
        edges[u].push_back(v);
        edges[v].push_back(u);
    }
    dfs(1, 0);
    long long ans = 0;
    for (int i : edges[1]) {
        ans += dep[i] + 1;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) solve();
    return 0;
}

G. Let Them Eat Cake

模拟~
签到++

#include

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

void solve() {
    int n;
    cin >> n;
    vector<int> v(n), flag(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    int cnt = n-1, ans = 0;
    while (cnt > 0) {
        ans++;
        for (int i = 0; i <= cnt; i++) {
            flag[i] = 0;
            if (i && v[i] < v[i-1]) flag[i] = 1;
            if (i + 1 <= cnt && v[i] < v[i+1]) flag[i] = 1;
        }
        for (int i = 0, j = 0; i <= cnt; i++) {
            if (!flag[i]) continue;
            j = max(i, j);
            while (j <= cnt && flag[j]) j++;
            if (j > cnt) break;
            swap(v[i], v[j]);
            flag[j] = 1;
            flag[i] = 0;
        }
        while (cnt && flag[cnt] == 1) cnt--;
    }
    cout << ans << endl;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while(T--) solve();
    return 0;
}

H.Life is Hard and Undecidable, but…

赛场上画了两个半小时的生命游戏~
想到的话 其实很简单
存活两轮的图示:
2022 China Collegiate Programming Contest (CCPC) Mianyang Onsite_第1张图片
代码很简单~

#include

using namespace std;

typedef unsigned long long ull;

int ans1[5005],ans2[3004];
void solve() {
    int n;
    cin>>n;
    int cnt=0;
    for(int i=1; i<=n+n; i+=2) {
        ans1[++cnt]=i;
        ans2[cnt]=i;
        ans1[++cnt]=i;
        ans2[cnt]=i+1;
    }
    cout<<cnt<<endl;
    for(int i=1; i<=cnt; i++) {
        cout<<ans1[i]<<" "<<ans2[i]<<endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    srand(time(0));
    // cin >> T;
    while(T--) solve();
    return 0;
}

M. Rock-Paper-Scissors Pyramid

猜了个结论,过了~

#include

using namespace std;

typedef unsigned long long ull;
typedef pair<int, int> pii;

bool win(char a, char b) {
    return (a == 'R' && b == 'S') || (a == 'P' && b == 'R') || (a == 'S' && b == 'P');
}

void solve() {
    string s;
    cin >> s;
    stack<char> stk;
    for (int i = s.length() - 1; i >= 0; i--) {
        while (!stk.empty() && win(s[i], stk.top())) stk.pop();
        stk.push(s[i]);
    }
    char ans = -1;
    while (!stk.empty()) {
        ans = stk.top(); stk.pop();
    }
    cout << ans << '\n';
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    srand(time(0));
    cin >> T;
    while(T--) solve();
    return 0;
}

第一次也是最后一次打CCPC,五题尾,拿到了三个铁酱的第一块铜牌~

你可能感兴趣的:(深度优先,算法,图论)