A - delete .
在输出时过滤一遍即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; const ll N = 2e5 + 5;
int main() { string s; cin >> s; string ans; for (int i = 0; i < s.size(); i++) { if (s[i] != '.') { ans += s[i]; } } cout << ans << endl; return 0; }
|
B - 3^A
打表后排序输出一遍即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; const ll N = 2e5 + 5; vector<ll> q; ll m;
bool cmp(ll a, ll b) { return a > b; }
void solve() { int temp = 1; while (temp <= m) { q.push_back(temp); temp *= 3; } }
int main() { cin >> m; solve(); sort(q.begin(), q.end(), cmp); vector<ll> ans; ll i = 0; while (m > 0) { while (q[i] <= m) { ans.push_back(q.size() - i - 1); m -= q[i]; } i++; } cout << ans.size() << endl; for (auto i : ans) { cout << i << " "; } return 0; }
|
C - Count ABC Again
根据题意模拟一遍即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; const ll N = 2e5 + 5;
int main() { int n, q; cin >> n >> q; string s; cin >> s; ll ans = 0; for (int i = 0; i < n; i++) { if (s[i] == 'A') { if (s[i + 1] == 'B' && s[i + 2] == 'C') { ans++; } } } while(q--) { int pos; string t; cin >> pos >> t; if (s[pos - 1] == t[0]) { cout << ans << endl; continue; } if (s[pos - 1] == 'A') { if (s[pos] == 'B' && s[pos + 1] == 'C') { if (t[0] != 'A') { ans--; } } } if (s[pos - 1] == 'B') { if (s[pos - 2] == 'A' && s[pos] == 'C') { if (t[0] != 'B') { ans--; } } } if (s[pos - 1] == 'C') { if (s[pos - 2] == 'B' && s[pos - 3] == 'A') { if (t[0] != 'C') { ans--; } } } s[pos - 1] = t[0]; if (t[0] == 'A') { if (s[pos] == 'B' && s[pos + 1] == 'C') { ans++; } } if (t[0] == 'B') { if (s[pos - 2] == 'A' && s[pos] == 'C') { ans++; } } if (t[0] == 'C') { if (s[pos - 2] == 'B' && s[pos - 3] == 'A') { ans++; } } cout << ans << endl; } return 0; }
|
D - Buildings
题目要求找到i之后有几个j可以使得a[i + 1 ….. j]中的所有值 <= a[j], 双循环写会超时,考虑枚举j,找到第一个i使得a[i] > a[j], 则i + 1 到 j的所以贡献都加1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include <bits/stdc++.h> #define endl '\n' using ll = long long; using namespace std; const ll N = 2e5 + 5;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } stack<ll> s; vector<ll> ans(n); for (int i = n - 1; i >= 0; i--) { ans[i] = s.size(); while (s.size() && a[s.top()] < a[i]) { s.pop(); } s.push(i); } for (int i = 0; i < n; i++) { cout << ans[i] << endl; } return 0; }
|