2024年中国大学生程序设计竞赛高职专场 vp

A.盒子

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
    
bool check (int x, int a, int b) {
    return x >= a && x <= b;
}

void solve() {
    int z_min, h, u0, v0, u1, v1;
    cin >> z_min >> h >> u0 >> v0 >> u1 >> v1;
    int z_max = z_min + h, u_min = min(u0, u1),
    u_max = max(u0, u1), v_min = min(v0, v1), 
    v_max = max(v0, v1);
    int Q;
    cin >> Q;
    for ( ; Q--; ) {
        int x, y, z;
        cin >> x >> y >> z;
        if (check(x, u_min, u_max) && check(y, v_min, v_max) && check(z, z_min, z_max)) {
            cout << "YES\n";
        } else {
            cout << "NO\n";
        }
    }

}

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

  

B.递增序列

 

C.CCPC

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
    
void solve() {
    string s;
    cin >> s;
    int cntC = 0, cntP = 0; 
    for (char c : s) {
        if (c == 'C') cntC++;
        else if (c == 'P') cntP++;
    }
    if (cntC < 3 && cntP < 1) cout << 0 << '\n';
    else cout << min(cntP, (cntC - 1) / 2) << '\n';
}
// CCPCCPC

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

  

D.重心树

Code:

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
constexpr int N = 2e5 + 5;
int f[N], sz[N]; 

int find (int x) {
    return f[x] == x ? x : f[x] = find(f[x]);
}

void merge (int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return ;
    if (x < y) f[y] = x;
    else f[x] = y; 
    return ;
} 

void solve() { 
    int n;
    cin >> n; 
    vector <vector<int>> c(n + 1);
    for (int i = 1; i <= n; i++) {
        f[i] = i;
        int x;
        cin >> x;
        for (int j = 1; j <= x; j++) {
            int ci; cin >> ci;
            c[i].push_back(ci);
        }
    }
    vector <pair<int, int>> ans;
    for (int i = n; i >= 1; i--) {
        for (int c : c[i]) {
            int dy = find(c);
            ans.emplace_back(i, dy);
            merge(i, dy);
        }
    }
    for (auto &[x, y] : ans) {
        cout << x << " " << y << "\n";
    }
}

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  

E.覆盖一棵树

树形dp

 

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
constexpr int N = 1e5 + 5;
using i64 = int64_t;
    
void solve() { 
    int n;
    cin >> n;
    vector <vector<int>> adj(n + 1);
    for (int i = 2; i <= n; i++) {
        int from;
        cin >> from; 
        adj[from].push_back(i);
        adj[i].push_back(from);
    }
    int ans = 0;  
    vector <bool> vis(n + 1, 0);
    vector <int> dp(n + 1, 1e9);
    auto dfs = [&] (auto self,int from, int fa) -> void {
        vis[from] = 1;
        if (fa != -1 && adj[from].size() == 1) { // 叶子节点 
            dp[from] = 0; 
            return ;
        }
        for (int to : adj[from]) {
            if (to == fa) continue; 
            if (!vis[to]) {
                self (self, to, from);
                if (adj[from].size() >= 3 || (adj[from].size() == 2 && fa == -1)) {
                    dp[from] = min(dp[from], dp[to] + 1);
                } else {
                    dp[from] = dp[to] + 1;
                } 
                if (adj[from].size() >= 3 || (adj[from].size() == 2 && fa == -1)) {
                    ans = max(ans, dp[to] + 1);
                }
            }
        }
    } ;   
    dfs (dfs, 1, -1);
    cout << max(ans, dp[1]) << '\n';
}

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  

F.拼图

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
    
void solve() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a < 4) cout << 0 << '\n';
    else {
        int sum = 0, mi = min(b, c), sum2 = 0;
        sum = 4 + mi * 2;
        for (int i = 1; i <= d; i++) {
            for (int j = 1; j <= sqrt(i); j++) {
                if (!(i % j)) {
                    if (mi >= (j + i / j)) {
                        sum2 = max(sum2, 4 + (j + i / j) * 2 + i);
                    }
                }
            }
        }
        cout << max(sum, sum2) << '\n';
    } 
}

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

  

H.平方根

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
    
void solve() {
    string s;
    cin >> s;
    int n = s.size(), cnt = 0;
    vector <int> ans;
    for (int i = 0; i < n; i++) {
        if (s[i] == '1') {
            cnt++;
        } else {
            if (cnt > 0) {
                ans.push_back(cnt); 
            }
            cnt = 0;
        }
    }
    if (cnt >= 1) ans.push_back(cnt);  
    double sum = 0;
    for (int i = 0; i < ans.size(); i++) {
        if (ans[i] & 1) {
            sum += double (ans[i] / 2 + 1);
        } else {
            sum += double (ans[i] / 2 - 1) + double (sqrt(2)); 
        }
    }
    cout << fixed << setprecision(10) << sum << '\n';
}

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

/*
1 1
2 sqrt(2)
3 2
4 1 + sqrt(2) 
*/

  

L修正解法

//https://github.com/Ckeyliuhi/acm-iters
#include <bits/stdc++.h>
 
using namespace std;
using i64 = int64_t;
constexpr int N = 2e5 + 5;
int a[N], ans[N];

void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    bool ok = 0;
    if (a[1] < 0) {
        ok = 1; 
        cout << '-';
    }
    for (int i = n; i >= 1; i--) { 
        if (ok) { 
            if (a[i] <= 0) {
                ans[i] = -a[i];
            } else {
                ans[i] = 10 - a[i];
                a[i - 1] ++; 
            }
        } else {
            if (a[i] >= 0) {
                ans[i] = a[i];
            } else {
                ans[i] = 10 + a[i];
                a[i - 1] --;
            }
        }
    }
    ok = 1;
    for (int i = 1; i <= n; i++) {
        if (ok && !ans[i]) {
            continue;
        } else ok = 0;
        cout << ans[i];
    }
    cout << '\n';
}

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

  

posted @ 2024-11-03 18:23  Iter-moon  阅读(84)  评论(0)    收藏  举报