签到,贪心
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (i == 0 and s[i] == '.' and s[i + 1] != 'L') { s[i] = 'C'; } else if (i == n - 1 and s[i] == '.' and s[i - 1] != 'L') { s[i] = 'C'; } else { if (s[i] == '.' and s[i - 1] != 'L' and s[i + 1] != 'L') { s[i] = 'C'; } } } cout << s << '\n'; return 0; }
模拟,树状数组,逆序对
AC代码:
#include <bits/stdc++.h> using namespace std; using LL = long long; #define int long long const int mod = 10; template <typename T> struct Fenwick { const int n; vector<T> a; Fenwick(int n) : n(n), a(n + 1) {} void add(int x, T v) { for (int i = x; i <= n; i += i & -i) { a[i] += v; } } T sum(int x) { T ans = 0; for (int i = x; i > 0; i -= i & -i) { ans += a[i]; } return ans; } T rangeSum(int l, int r) { return sum(r) - sum(l - 1); } void rangeAdd(int l, int r, T x) { add(l, x); add(r + 1, -x); } }; signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n + 1); Fenwick<int> fen(n); string s; LL cnt = 0, sum = n * (n - 1) / 2, now = 1; for (int i = 1; i <= n; i++) { cin >> a[i]; cnt += i - fen.sum(a[i]) - 1; fen.add(a[i], 1); } cin >> s; cout << cnt << '\n'; bool ok = true; for (int i = 0; i < m; i++) { if (s[i] == 'S') { if (ok) { cnt -= 2 * a[now] - 1 - n; now++; if (now == n + 1) { now = 1; } cout << cnt % mod; } else { cnt -= 2 * a[now] - 1 - n; now--; if (now == 0) { now = n; } cout << cnt % mod; } } else { if (ok) { ok = false; cnt = sum - cnt; cout << cnt % mod; now--; if (now == 0) { now = n; } } else { ok = true; cnt = sum - cnt; cout << cnt % mod; now++; if (now == n + 1) { now = 1; } } } } return 0; }
2022 China Collegiate Programming Contest (CCPC) Guilin Site(比冠军少9题)
最新推荐文章于 2023-12-06 10:25:43 发布