题解:B4323 [科大国创杯小学组 2025] 改写

· · 题解

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

typedef long long ll;
typedef pair<char, ll> pli;

void solve() {
    int n;
    cin >> n;
    vector<pli> v(n);
    ll total_len = 0;
    for (int i = 0; i < n; i++) {
        cin >> v[i].first >> v[i].second;
        total_len += v[i].second;
    }
    if (n == 1) {
        cout << -1 << '\n';
        return;
    }
    if (n == 3 && v[0].second == 1 && v[1].second == 1 && v[2].second == 1
        && v[0].first == v[2].first) {
        cout << -1 << '\n';
        return;
    }
    ll res = 0;
    ll remain = 0; 
    char last = 0;

    for (auto &p : v) {
        char c = p.first;
        ll cnt = p.second;

        if (remain == 1) {

            res++;
            remain = 0;
            cnt--;
            if (cnt == 0) continue;
        }
        res += cnt / 2;
        remain = cnt % 2;
        last = c;
    }

    if (remain == 1) {
        res--;
    }

    cout << res << '\n';
}

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