voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int x; cin >> x;
int d[5], ans = 0; for (int &i: d) { i = x % 10; if (i == 0) i = 10; x /= 10; } d[4] = 1; for (int i = 3; i >= 0; --i) ans += abs(d[i] - d[i + 1]) + 1; cout << ans << endl; } }
B. Chemistry
大致题意
给你一个字符串,在恰好删除掉 $k$ 个字母之后,再重新排列,能否得到一个回文串
思路
只要 $k$ 至少比字母出现次数为基数次的字母个数 $- 1$ 还要多就行了
AC code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
voidsolve(){ int _; cin >> _;
string str; str.reserve(1e5 + 10); for (int ts = 0; ts < _; ++ts) { int n, k; cin >> n >> k >> str; map<char, int> cnt; for (auto &item: str) cnt[item]++; int edd = 0; for (auto &item: cnt) edd += item.second % 2; cout << ((k >= edd - 1) ? "YES" : "NO") << endl; } }
for (int i = 0; i < n; ++i) { char op; int l, r; cin >> op >> l >> r; if (op == '+') { prq1.emplace(l, i); prq2.emplace(r, i); } else { mvd1[l]++; mvd2[r]++; }
while (!prq1.empty()) { auto item = prq1.top(); auto iter = mvd1.find(item.first); if (iter != mvd1.end()) { while (iter->second--) prq1.pop(); mvd1.erase(iter); } elsebreak; } while (!prq2.empty()) { auto item = prq2.top(); auto iter = mvd2.find(item.first); if (iter != mvd2.end()) { while (iter->second--) prq2.pop(); mvd2.erase(iter); } elsebreak; }
voidsolve(){ int _; cin >> _; for (int ts = 0; ts < _; ++ts) { int n, m; cin >> n >> m; vector<int> a(n - 1), b(n); for (auto &i: a) cin >> i; for (auto &i: b) cin >> i; sort(a.begin(), a.end()); sort(b.begin(), b.end());
int lastB = -1, base = 0, cur = 0; for (int &i: a) { while (cur < n && i >= b[cur]) lastB = b[cur++]; if (cur >= n) base++; cur++; } if (lastB == -1) lastB = b.back();
cout << 1LL * base * m + max(0, m - lastB + 1) << endl; } }