跳到正文

题解

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

zzs2013约 2 分钟
原文
文章信息与版本

文章信息

发布
2026年4月14日
收录
2026年11月11日
更新
2026年11月16日
篇幅
约 2 分钟
题目
B4323

版本

  1. v1当前
C++
#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;
}
原文