20级训练赛Round #4

给20级学弟学妹们带带榜单

A - 凯少的动作序列

枚举情况替换即可

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void solve() {
    int n; string s;
    cin >> n >> s;
    int cnt = 0;
    for (int i = 1; i < n; ++i)if (s[i] != s[i - 1])i++, cnt++;
    cout << n - cnt;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}

B - 凯少的秘密消息

根据题意模拟

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
map<string, int>mp;
int cost[N], e[N], v[N];
void solve() {
    int n, k, m;
    cin >> n >> k >> m;
    memset(v, 0x3f, sizeof(v));
    for (int i = 1; i <= n; ++i) {
        string s;
        cin >> s, mp[s] = i;
    }
    for (int i = 1, t; i <= n; ++i) {
        cin >> t;
        cost[i] = t;
    }
    for (int i = 1; i <= k; i++) {
        int n;
        cin >> n;
        while (n--) {
            int a;
            cin >> a;
            e[a] = i;
            v[i] = min(v[i], cost[a]);
        }
    }
    ll ans = 0;
    for (int i = 0; i < m; ++i) {
        string s; cin >> s;
        ans += v[e[mp[s]]];
    }
    cout << ans;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}

C - 尚佬的投篮得分

利用前缀和差值比较可以快速得到区间最大分数

#include<bits/stdc++.h>
using namespace std;
const int N =  100000 + 10;
int a[N], pre[N], cnt, mx;
int main() {
    int n, k; cin >> n >> k;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1, x; i <= n; ++i) {
        cin >> x;
        pre[i] = pre[i - 1] + (1 - x) * a[i];
        cnt += x * a[i];
    }
    for (int i = k; i <= n; ++i) mx = max(mx, pre[i] - pre[i - k]);
    cout << cnt + mx;
    return 0;
}

D - 以旧换新

可以DFS剪枝搜索,也可以排序以后比较ASCII值做交换

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10, mod = 1e9 + 7;
void solve() {
    string a, b;
    cin >> a >> b;
    sort(a.begin(), a.end());
    for (int i = 0; i < a.length(); i++)
        for (int j = i + 1; j < a.length(); j++) {
            string t = a;
            swap(t[i], t[j]);
            if (stoll(t) > stoll(a) && stoll(t) <= stoll(b)) a = t;
        }
    cout << a << endl;
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr);
    solve();
    return 0;
}
posted @ 2021-06-04 21:04  RioTian  阅读(53)  评论(0编辑  收藏  举报