A
比较明显的题,贪心往下做就可以。
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1e5 + 7;
int k;
int a[N];
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> k;
for (int i = 1; i <= k; i++) {
std::cin >> a[i];
}
if (k == 1 && a[1] == 1) { std::cout << "1\n"; exit(0); }
std::sort(a + 1, a + k + 1);
int dep = 1, ans = 1;
for (int i = 1; i <= k; i++) {
ans += (a[i] - dep);
dep = a[i] - 1;
}
std::cout << ans << "\n";
return 0;
}
B
比较明显的题,模拟一下往下做就可以。
#include <bits/stdc++.h>
using i64 = long long;
void solve() {
int n;
std::string s;
std::cin >> n >> s;
int cnt = 1;
std::vector<int> a(n + 1);
if (s[0] == '=')
cnt = 2;
else
a[1] = 1;
char lst = s[0];
for (int i = 1; i < n - 1; i++) {
if (s[i] == '=') {
a[i + 1] = a[i];
cnt++;
}
else if (s[i] == s[i - 1]) {
a[i + 1] = a[i] + 1;
cnt = 1;
}
else if (s[i] != lst) {
a[i + 1] = cnt;
cnt = 1;
lst = s[i];
}
else if (s[i] == lst) {
a[i + 1] = a[i] + cnt;
cnt = 1;
}
}
i64 ans = 0;
for (int i = 0; i <= n; i++)
ans += a[i];
std::cout << ans << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
标签:std,cnt,int,long,else,Div.2,lst,补题,LGR
From: https://www.cnblogs.com/xsyc/p/18530873