A. Circle Coloring
#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, a[110][3]; int flag; int ans[110]; void dfs(int step) { if(step == n + 1) { if(a[n][ans[n]] == a[1][ans[1]]) return; rep(i, 1, n) cout << a[i][ans[i]] << " "; cout << endl; flag = 1; } if(flag) return; rep(k, 0, 2) { if(a[step][k] == a[step - 1][ans[step - 1]]) continue; ans[step] = k; dfs(step + 1); if(flag) break; } } inline void solve(int T) { cin >> n; rep(k, 0, 2) rep(i, 1, n) cin >> a[i][k]; flag = 0; dfs(1); } 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. Arrays Sum
每一个B序列最多消除$k - 1$个上升项
特判一下$k$等于$0$的情况
#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, k; int a[110], cnt; inline void solve(int T) { cin >> n >> k; k--; a[0] = cnt = -1; rep(i, 1, n){ cin >> a[i]; if(a[i] > a[i - 1]) cnt++; } if(cnt == 0) cout << "1" << endl; else if(k == 0) cout << "-1" << endl; else cout << ceil(1.0 * cnt / k) << 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. Discrete Acceleration
模拟
#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, l; int a[100010]; double s1[100010], s2[100010]; inline void solve(int T) { cin >> n >> l; rep(i, 1, n) { cin >> a[i]; s1[i] = s1[i - 1] + 1.0 * (a[i] - a[i - 1]) / i; } a[n + 1] = l; s2[n + 1] = 0; for(int i = n; i; i--) s2[i] = s2[i + 1] + 1.0 * (a[i + 1] - a[i]) / (n - i + 1); int tmp = n; rep(i, 1, n) if(s1[i] > s2[i]) { tmp = i - 1; break; } double len = a[tmp + 1] - a[tmp]; if(s1[tmp] <= s2[tmp + 1]) len -= (tmp + 1) * (s2[tmp + 1] - s1[tmp]); else len -= (n - tmp + 1) * (s1[tmp] - s2[tmp + 1]); printf("%.15lf ", max(s1[tmp], s2[tmp + 1]) + len / (n + 2)); } 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. Searchlights
从大到小枚举一个方向需要改变的长度,再记录一下另一个方向需要改变的长度的最大值
#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, m; int a[2010], b[2010], c[2010], d[2010]; vector<pair<int, int> > cnt; bool cmp(pair<int, int> x, pair<int, int> y) {return x.first > y.first;} inline void solve(int T) { cin >> n >> m; rep(i, 1, n) cin >> a[i] >> b[i]; rep(i, 1, m) cin >> c[i] >> d[i]; rep(i, 1, n) rep(j, 1, m) if(a[i] <= c[j] && b[i] <= d[j]) cnt.push_back(make_pair(c[j] - a[i] + 1, d[j] - b[i] + 1)); if(cnt.empty()) { cout << "0" << endl; return; } sort(cnt.begin(), cnt.end(), cmp); int num = cnt.size(), ans = cnt[0].first, mx = cnt[0].second; rep(i, 1, num - 1){ if(cnt[i].first != cnt[i - 1].first) ans = min(cnt[i].first + mx, ans); mx = max(mx, cnt[i].second); } ans = min(ans, mx); cout << ans << 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); }