Codeforces-Round-927-(Div

[Dashboard - Codeforces Round 927 (Div. 3) - Codeforces](比赛链接)

A. Thorns and Coins

思路:遇到@加一, 连续两个*结束循环

#include <iostream>
using namespace std;

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    int ans = 0;
    for(int i = 0; i < n; i++) {
        if(i < n - 1 && s[i] == '*' && s[i + 1] == '*'){
            break;
        }
        if(s[i] == '@') ans++;
    }
    cout << ans << endl;
}

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

B. Chaya Calendar

思路:读懂题目就很简单了,

#include <iostream>
#include <vector>
using namespace std;

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

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

C. LR-remainders

思路:倒着模拟一遍即可, L最后一个弹出的数 a[l] 和 r最后一个弹出的数 a[r], 倒着遍历一遍s,遇到L 把a[l] 乘进结果,并且l 减一, 遇到 R 把 a[l] 乘进结果,并且 r 加一

#include <iostream>
#include <vector>
using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    for(int i = 0; i < n; i++) cin >> a[i];
    string s;
    cin >> s;
    int l = -1;
    for(auto ch : s) {
        if(ch == 'L') l++;
    }
    int r = l + 1, ot = 1;
    vector<int> ans(n);
    for(int i = n - 1; i >= 0; i--) {
        if(s[i] == 'L'){
            ot = (ot * a[l--]) % m;
        }else{
            ot = (ot * a[r++]) % m;
        }
        ans[i] = ot;
    }
    for(auto x : ans) cout << x << ' ';
    cout << '\n';
}

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

D. Card Game

思路:大模拟,读懂题自己模拟就行,应该不会比我写的 shi 了(

#include <bits/stdc++.h>
using namespace std;
typedef pair<string, string> pss;
bool st[200][10];
vector<pss> ans;

void Push(int x, char ch1, int y, char ch2) {
    string a = "ab", b = "ab";
    a[0] = x + '0', a[1] = ch1;
    b[0] = y + '0', b[1] = ch2;
    ans.push_back({a, b});
    st[ch1][x] = 1, st[ch2][y] = 1;
}

void solve() {
    ans.clear();
    memset(st, 0, sizeof(st));
    int n;
    cin >> n;
    char ch;
    cin >> ch;
    map<char, vector<int>> mp;
    for(int i = 0; i < n * 2; i++) {
        string s;
        cin >> s;
        mp[s[1]].push_back(s[0] - '0');
    }
    for(auto& [k, v] : mp) {
        sort(v.begin(), v.end());
    }
    int i = 0, j = mp[ch].size() - 1;
    int t = mp[ch].size();
    // cout << t << endl;
    while(t > n) {
        auto p = mp[ch];
        if(p[i] == p[j]){
            cout << "IMPOSSIBLE\n";
            return; 
        }
        t -= 2;
        Push(p[i], ch, p[j], ch);
        i++, j--;
    }
    int x = n - ans.size() - t;
    // cout << x << endl;
    for(auto& [k, v] : mp) {
        if(x == 0) break;
        if(k == ch) continue;
        for(int i = 0, j = v.size() - 1; i < j && v[i] < v[j] && x; i++, j--){
            // cout << k << ' ' << v[i] << endl;
            Push(v[i], k, v[j], k);
            x--;
        }
    }
    // cout << x << endl;
    if(x){
        cout << "IMPOSSIBLE\n";
        return; 
    }
    int p = ans.size();
    // cout << ans.size() << endl;
    // cout << p << endl;
    for(auto x : mp[ch]) {
        if(!st[ch][x]) {
            Push(0, 'a', x, ch);
        }
    }
    for(auto& [k, v] : mp) {
        if(k == ch) continue;
        for(int i = 0; i < v.size(); i++){
            if(!st[k][v[i]]){
                // cout << k << ' ' << v[i] << endl;
                string a = "ab";
                a[0] = v[i] + '0', a[1] = k;
                ans[p++].first = a;
            }
        } 
    }
    for(pss p : ans){
        cout << p.first << ' ' << p.second << '\n';
    }
    
}

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

E. Final Countdown

思路:计算每一位的贡献。Sn - 1, Sn - 2 ... Sn - n, 最左边(最高位) 的贡献是 Si ⋅ ∑(j = 0 到 i) 10^j, 把 i 带入n - 1, 自己手动模拟一下更容易理解。比如120,可以对应代码手动模拟

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

void solve() {
    int n;
    cin >> n;
    string s;
    cin >> s;
    reverse(s.begin(), s.end());
    vector<int> a(n + 1);
    for(int i = n - 1; i >= 0; i--) {
        a[i] = a[i + 1] + (s[i] - '0');
    }
    string ans;
    int c = 0;
    for(int i = 0; i < n; i++) {
        c += a[i];
        ans += (c % 10 + '0');
        c /= 10;
    }
    ans += (c + '0');
    while(ans.back() == '0'){
        ans.pop_back();
    }
    reverse(ans.begin(), ans.end());
    cout << ans << '\n';
}

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

谢谢观看

posted @ 2025-07-21 16:54  _nilv  阅读(10)  评论(0)    收藏  举报