Codeforces Round #787 (Div. 3)(7/7)

Problem - A - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
    int a, b, c, x, y;
    cin >> a >> b >> c >> x >> y;
    if (a + c >= x && b + c >= y && a + b + c >= x + y) {
        cout << "YES\n";
    }
    else {
        cout << "NO\n";
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Problem - B - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    int ans = 0;
    for (int i = n - 2; i >= 0; i--) {
        if (a[i + 1] == 0) {
            cout << "-1\n";
            return;
        }
        while (a[i] >= a[i + 1]) {
            a[i] >>= 1;
            ans++;
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Problem - C - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

void solve() {
    string s;
    cin >> s;
    int len = s.size();
    int now = -1;
    for (int i = 0; i < len; i++) {
        if (s[i] == '1' || s[i] == '?') {
            now = i;
        }
        else {
            break;
        }
    }
    int ans = 0;
    for (int i = len - 1; i >= 0; i--) {
        if (now >= i - 1) {
            ans++;
        }
        if (s[i] == '0' || s[i] == '?') {

        }
        else {
            break;
        }
    }
    cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Problem - D - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int n;
int p[200010];
int cnt;
bool vis[200010], vis1[200010];
void solve() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        vis[i] = false;
        vis1[i] = false;
    }
    for (int i = 1; i <= n; i++) {
        cin >> p[i];
        vis1[p[i]] = true;
    }
    if (n == 1) {
        cout << "1\n1\n1\n\n";
        return;
    }
    cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (!vis1[i]) {
            cnt++;
        }
    }
    cout << cnt << '\n';
    for (int i = 1; i <= n; i++) {
        if (vis1[i]) {
            continue;
        }
        vector<int> ans;
        int j = i;
        while (!vis[j]) {
            ans.push_back(j);
            vis[j] = true;
            j = p[j];
        }
        cout << ans.size() << '\n';
        reverse(ans.begin(), ans.end());
        for (auto it : ans) {
            cout << it << " ";
        }
        cout << "\n";
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Problem - E - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int maxx = 0;
    for (int i = 0; i < n; i++) {
        if (s[i] - 'a' > k) {
            char l = s[i] - (k - maxx);
            char r = s[i];
            for (int j = 0; j < n; j++) {
                if (s[j] >= l && s[j] <= r) {
                    s[j] = l;
                }
            }
            break;
        }
        maxx = max(maxx, s[i] - 'a');
    }
    for (int i = 0; i < n; i++) {
        if (s[i] <= maxx + 'a') {
            s[i] = 'a';
        }
    }
    cout << s << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}

Problem - F - Codeforces

AC代码:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
int n, k, ans, x, y, a[200010], vis[200010], deep[200010];
vector<int> edge[200010];
int dfs(int root, int dep, int fa) {
    deep[root] = dep;
    int num = vis[root];
    for (auto it : edge[root]) {
        if (it != fa && dfs(it, dep + 1, root)) {
            ans += 2;
            num++;
        }
    }
    return num;
}
void Solve() {
    cin >> n >> k;
    cin >> x >> y;
    ans = 0;
    for (int i = 1; i <= n; i++) {
        edge[i].clear();
        vis[i] = 0;
        deep[i] = 0;
    }
    for (int i = 1; i <= k; i++) {
        cin >> a[i];
        vis[a[i]] = 1;
    }
    vis[y] = 1;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dfs(x, 0, 0);
    cout << ans - deep[y] << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;
    while (T--) {
        Solve();
    }

    return 0;
}

Problem - G - Codeforces

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

int dp[255][255][255];
//dp[i][j+k][k]=min(dp[i][j+k][k],sum[i]-j-k+dp[i-1][j][p])
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1);
    vector<int> sum(n + 1);
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    reverse(a.begin() + 1, a.end());
    for (int i = 1; i <= n; i++) {
        sum[i] = sum[i - 1] + a[i];
    }
    memset(dp, 0x3f, sizeof(dp));
    dp[0][0][0] = 0;
    for (int i = 1; i <= n; i++) {
        rep (j, 0, m + 1) {
            int minn = 0x3f3f3f3f;
            rep (k, 0, m + 1) {
                if (j + k <= m) {
                    minn = min(minn, dp[i - 1][j][k]);
                    dp[i][j + k][k] = min(dp[i][j + k][k], minn + abs(sum[i] - j - k));
                }
            }
        }
    }
    int ans = 0x3f3f3f3f;
    rep (i, 0, m + 1) {
        ans = min(ans, dp[n][m][i]);
    }
    cout << ans << '\n';

    return 0;
}

道阻且长

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值