AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int a, b, c, x, y; cin >> a >> b >> c >> x >> y; if (a + c >= x && b + c >= y && a + b + c >= x + y) { cout << "YES\n"; } else { cout << "NO\n"; } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = 0; for (int i = n - 2; i >= 0; i--) { if (a[i + 1] == 0) { cout << "-1\n"; return; } while (a[i] >= a[i + 1]) { a[i] >>= 1; ans++; } } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { string s; cin >> s; int len = s.size(); int now = -1; for (int i = 0; i < len; i++) { if (s[i] == '1' || s[i] == '?') { now = i; } else { break; } } int ans = 0; for (int i = len - 1; i >= 0; i--) { if (now >= i - 1) { ans++; } if (s[i] == '0' || s[i] == '?') { } else { break; } } cout << ans << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; int n; int p[200010]; int cnt; bool vis[200010], vis1[200010]; void solve() { cin >> n; for (int i = 1; i <= n; i++) { vis[i] = false; vis1[i] = false; } for (int i = 1; i <= n; i++) { cin >> p[i]; vis1[p[i]] = true; } if (n == 1) { cout << "1\n1\n1\n\n"; return; } cnt = 0; for (int i = 1; i <= n; i++) { if (!vis1[i]) { cnt++; } } cout << cnt << '\n'; for (int i = 1; i <= n; i++) { if (vis1[i]) { continue; } vector<int> ans; int j = i; while (!vis[j]) { ans.push_back(j); vis[j] = true; j = p[j]; } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for (auto it : ans) { cout << it << " "; } cout << "\n"; } cout << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n, k; cin >> n >> k; string s; cin >> s; int maxx = 0; for (int i = 0; i < n; i++) { if (s[i] - 'a' > k) { char l = s[i] - (k - maxx); char r = s[i]; for (int j = 0; j < n; j++) { if (s[j] >= l && s[j] <= r) { s[j] = l; } } break; } maxx = max(maxx, s[i] - 'a'); } for (int i = 0; i < n; i++) { if (s[i] <= maxx + 'a') { s[i] = 'a'; } } cout << s << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; while (t--) { solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> using namespace std; using i64 = long long; int n, k, ans, x, y, a[200010], vis[200010], deep[200010]; vector<int> edge[200010]; int dfs(int root, int dep, int fa) { deep[root] = dep; int num = vis[root]; for (auto it : edge[root]) { if (it != fa && dfs(it, dep + 1, root)) { ans += 2; num++; } } return num; } void Solve() { cin >> n >> k; cin >> x >> y; ans = 0; for (int i = 1; i <= n; i++) { edge[i].clear(); vis[i] = 0; deep[i] = 0; } for (int i = 1; i <= k; i++) { cin >> a[i]; vis[a[i]] = 1; } vis[y] = 1; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; edge[u].push_back(v); edge[v].push_back(u); } dfs(x, 0, 0); cout << ans - deep[y] << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int T; cin >> T; while (T--) { Solve(); } return 0; }
AC代码:
#include <bits/stdc++.h> #define rep(i,a,n) for(int i=a;i<n;i++) using namespace std; using LL = long long; int dp[255][255][255]; //dp[i][j+k][k]=min(dp[i][j+k][k],sum[i]-j-k+dp[i-1][j][p]) int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<int> a(n + 1); vector<int> sum(n + 1); for (int i = 1; i <= n; i++) { cin >> a[i]; } reverse(a.begin() + 1, a.end()); for (int i = 1; i <= n; i++) { sum[i] = sum[i - 1] + a[i]; } memset(dp, 0x3f, sizeof(dp)); dp[0][0][0] = 0; for (int i = 1; i <= n; i++) { rep (j, 0, m + 1) { int minn = 0x3f3f3f3f; rep (k, 0, m + 1) { if (j + k <= m) { minn = min(minn, dp[i - 1][j][k]); dp[i][j + k][k] = min(dp[i][j + k][k], minn + abs(sum[i] - j - k)); } } } } int ans = 0x3f3f3f3f; rep (i, 0, m + 1) { ans = min(ans, dp[n][m][i]); } cout << ans << '\n'; return 0; }
道阻且长
Codeforces Round #787 (Div. 3)(7/7)
最新推荐文章于 2024-07-22 09:38:37 发布