Codeforces 4 A-D
题面
题解
A
题目大意:
判断一个正整数
能否表示成两个正偶数之和。
解题思路:
考虑分类讨论
对于 NO
;
对于 YES
;否则必然为 NO
。因为 结论的证明留作思考题。
综上,若 YES
,否则为 NO
。
#include <bits/stdc++.h> using namespace std; int main() { int w; scanf("%d", &w); if (w % 2 == 0 && w != 2) puts("YES"); else puts("NO"); return 0; }
B
题目大意:
构造一个数组
,使得 ,且 ,或输出 NO
表示不存在这样的数组。
解题思路:
先求出
接下来,考虑先设
#include <bits/stdc++.h> using namespace std; int sum, d, x, y, z; int a[100010], b[100010], c[100010]; int main() { scanf("%d%d", &d, &sum); for (int i = 1; i <= d; i++) scanf("%d%d", &b[i], &c[i]), x += b[i], y += c[i]; if (x <= sum && sum <= y) { puts("YES"); if (x == sum) { for (int i = 1; i <= d; i++) printf("%d ", b[i]); putchar('\n'); } else if (y == sum) { for (int i = 1; i <= d; i++) printf("%d ", c[i]); putchar('\n'); } else { for (int i = 1; i <= d; i++) a[i] = b[i]; z = sum - x; for (int i = 1; i <= d; i++) { int tmp = c[i] - b[i]; if (z > tmp) { z -= tmp; a[i] += tmp; } else { a[i] += z; break; } } for (int i = 1; i <= d; i++) printf("%d ", a[i]); putchar('\n'); } } else { puts("NO"); } return 0; }
C
题目大意:
给定
次操作,每次向集合中加入一个字符串(当集合中没有这个字符串时),或加入【该字符串,连接,最小能使其不重复的正整数】的字符串。
解题思路:
这道题可以用 STL 中的 map 解决。使用 map 统计字符串出现的次数即可。
#include <bits/stdc++.h> using namespace std; int n; map<string, int> mp; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; while (n--) { string s; cin >> s; if (!mp[s]) { mp[s] = 1; cout << "OK\n"; } else { cout << s << mp[s] << '\n'; mp[s]++; } } return 0; }
D
题目大意:
求去除一些元素之后,严格二维上升子排列。
解题思路:
注意到
可以使用链表存储序列。
#include <bits/stdc++.h> using namespace std; int n, d, w, a[5005], b[5005], f[5005], nxt[5005]; bool nok[5005]; int dfs(int x) { if (f[x]) return f[x]; f[x] = 1; for (int i = 1; i <= n; i++) { if (nok[i]) continue; if (a[i] > a[x] && b[i] > b[x] && f[x] < dfs(i) + 1) { f[x] = dfs(i) + 1; nxt[x] = i; } } return f[x]; } int main() { scanf("%d%d%d", &n, &d, &w); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i], &b[i]); if (a[i] <= d || b[i] <= w) nok[i] = 1; } printf("%d\n", dfs(0) - 1); int pos = nxt[0]; while (pos) { printf("%d ", pos); pos = nxt[pos]; } return 0; }
本文来自博客园,作者:cwkapn,转载请注明原文链接:https://www.cnblogs.com/chenaknoip/p/18515850
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)