Codeforces Round 928 (Div. 4)
B题:
看每行是否有单独的一个
主要需要注意的是这里要用
000 011 011
一个个输出的话是
0 11 11 0 0 0 0 0 0
也就是说在输入第二行
(其实题目已经规定了输入的得是字符没看到qwq)
#include <bits/stdc++.h> void solve() { int n; std::cin >> n; std::vector<std::string> s(n); for (int i = 0; i < n; i++) std::cin >> s[i]; for (int i = 0; i < n; i++) { if (std::count(s[i].begin(), s[i].end(), '1') == 1) { std::cout << "TRIANGLE\n"; return; } } std::cout << "SQUARE\n"; } int main() { std::ios::sync_with_stdio(false), std::cin.tie(nullptr); int t; std::cin >> t; while (t--) solve(); return 0; }
C题:
如果直接暴力做的话是会超时的:
#include <bits/stdc++.h> using namespace std; using i64 = long long; int f(int x) { int ans = 0; while (x) { ans += x % 10; x /= 10; } return ans; } void solve() { int n; cin >> n; i64 sum = 0; for (int i = 1; i <= n; i++) sum += f(i); cout << sum << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int t; cin >> t; while (t--) solve(); return 0; }
可以观察发现性质,一个数
#include <bits/stdc++.h> using namespace std; using i64 = long long; const int N = 2e5 + 5; i64 f[N]; void solve() { int n; cin >> n; cout << f[n] << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int t; cin >> t; for (int i = 1; i <= N; i++) { f[i] = f[i - 1]; int x = i; while (x) f[i] += x % 10, x /= 10; } while (t--) solve(); return 0; }
D题:
本题可以利用异或的性质。与一个数
C++ 20 写法:
#include <bits/stdc++.h> using i64 = long long; void solve() { int n; std::cin >> n; std::vector<int> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } std::multiset<int> s; int ans = n; for (int i = 0; i < n; i++) { if (s.contains(a[i] ^ ((1 << 31) - 1))) { s.extract(a[i] ^ ((1 << 31) - 1)); ans--; } else { s.insert(a[i]); } } std::cout << ans << "\n"; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int t; std::cin >> t; while (t--) { solve(); } return 0; }
另一种等价的写法:
#include <bits/stdc++.h> using namespace std; using LL = long long; const int INF = 0x3f3f3f3f; const LL mod = 1e9 + 7; const int N = 200005; int a[N]; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); multiset<int> se; int ans = 0; for (int i = 1; i <= n; i++) { if (se.find(a[i] ^ INT_MAX) != se.end()) { se.erase(se.find(a[i] ^ INT_MAX)); } else { se.insert(a[i]); ans++; } } printf("%d\n", ans); } return 0; }
E题:
思路:第一轮是
第二轮是
...
在
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n, k; cin >> n >> k; int cnt = 0; while (k > (n + 1) / 2) { int x = (n + 1) / 2; n -= x; k -= x; cnt++; } cout << (2 * k - 1) * (1 << cnt) << "\n"; } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int t; cin >> t; while (t--) solve(); return 0; }
本文作者:pangyou3s
本文链接:https://www.cnblogs.com/pangyou3s/p/18178717
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步