A. Number of Apartments
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) int n; inline void solve(int T) { cin >> n; rep(i, 0, n / 3 + 1) rep(j, 0, n / 5 + 1) rep(k, 0, n / 7 + 1) if(i * 3 + j * 5 + k * 7 == n) { cout << i << " " << j << " " << k << endl; return; } cout << "-1" << endl; } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("ans.txt", "w", stdans); int T = 1; cin >> T; rep(i, 1, T) solve(i); }
B. Barrels
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n, k; ll a[200010]; inline void solve(int T) { cin >> n >> k; rep(i, 1, n) cin >> a[i]; sort(a + 1, a + n + 1); rep(i, 1, min(k, n - 1)) a[n] += a[n - i]; if(k == 0) cout << a[n] - a[1] << endl; else cout << a[n] << endl; } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("ans.txt", "w", stdans); int T = 1; cin >> T; rep(i, 1, T) solve(i); }
C. Numbers on Whiteboard
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, a, b) for (register int i = a; i <= b; i++) ll n; inline void solve(int T) { cin >> n; cout << 2 << endl; int tmp = n; rep(i, 1, n - 1) { cout << n - i << " " << tmp << endl; tmp = ceil((n - i + tmp) / 2.0); } } int main() { // ios_base::sync_with_stdio(0); // cin.tie(0); // cout.tie(0); // freopen("in.txt", "r", stdin); // freopen("ans.txt", "w", stdans); int T = 1; cin >> T; rep(i, 1, T) solve(i); }
D. String Deletion
模拟
E. String Reversal
还是挺好理解,贪心取每个字母移动的对应位置,然后就求个逆序对。
#include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i, a, b) for(int i = a; i <= b; i++) int n; char s[200010]; int a[200010]; stack<int> sta[200]; ll ans; void ksort(int l, int r) { int mid = (l + r) / 2; int i = l, j = mid + 1, id = 0; int f[r - l + 1]; while(i <= mid || j <= r) { if(i > mid) f[++id] = a[j++]; else if(j > r) f[++id] = a[i++]; else if(a[i] <= a[j]) f[++id] = a[i++]; else { f[++id] = a[j++]; ans += mid - i + 1; } } rep(i, 1, id) a[l + i - 1] = f[i]; } void merge(int l, int r) { if(l == r) return; int mid = (l + r) / 2; merge(l, mid); merge(mid + 1, r); ksort(l, r); } void run() { cin >> n; cin >> s + 1; rep(i, 1, n) sta[s[i]].push(n - i + 1); rep(i, 1, n) { a[i] = sta[s[i]].top(); sta[s[i]].pop(); } merge(1, n); cout << ans << endl; } int main() { cin.tie(0), cout.tie(0), ios::sync_with_stdio(0); // int t; cin >> t; while(t--) run(); }