标签:ref ace while 个数 cti 构造 tps ring names
比赛链接:https://codeforces.com/contest/1352
将一个十进制数的每一个非零位分离出来。
#include <bits/stdc++.h> using namespace std; void solve() { string s; cin >> s; int n = s.size(); vector<string> ans; for (int i = n - 1; i >= 0; i--) { if (s[i] != ‘0‘) { ans.push_back(s[i] + string(n - 1 - i, ‘0‘)); } } cout << ans.size() << "\n"; for (auto i : ans) cout << i << ‘ ‘; cout << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
将整数 n 分解为 k 个奇偶性相同的数。
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; int n1 = n - (k - 1), n2 = n - (k - 1) * 2; if (n1 > 0 and n1 % 2 == 1) { cout << "YES\n"; for (int i = 0; i < k - 1; i++) cout << "1 "; cout << n1 << "\n"; } else if (n2 > 0 and n2 % 2 == 0) { cout << "YES\n"; for (int i = 0; i < k - 1; i++) cout << "2 "; cout << n2 << "\n"; } else { cout << "NO" << "\n"; } } int main() { int t; cin >> t; while (t--) solve(); }
输出第 k 个不能被 n 整除的数。
两个 n 之间有 n - 1 个不能被 n 整除的数,找出 k 中有多少个完整的 n - 1 区间,再对 n - 1 取余即可。
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k; cin >> n >> k; if (k % (n - 1) == 0) cout << k / (n - 1) * n - 1 << "\n"; else cout << k / (n - 1) * n + k % (n - 1) << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
n 堆糖果排成一排,Alice 只能从左边取,Bob 只能从右边取,每次一人取得的糖果数需多于另一人上一次取得的糖果数,问所有糖果取完时二人共取了多少次,以及各自取得的糖果个数。
模拟即可。
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; int a[n] = {}; for (auto &i : a) cin >> i; int l = 0, r = n - 1; int ans_l = 0, ans_r = 0; int pre_l = 0, pre_r = 0; int i; for (i = 0; l <= r; i++) { int sum = 0; if (i % 2 == 0) { while (l <= r and sum <= pre_r) { sum += a[l]; ++l; } ans_l += sum; pre_l = sum; } else { while (r >= l and sum <= pre_l) { sum += a[r]; --r; } ans_r += sum; pre_r = sum; } } cout << i << ‘ ‘ << ans_l << ‘ ‘ << ans_r << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
统计数组 a 中有多少元素可以被连续子数组加和求得。
数据范围较小,$O_{(n^2)}$ 枚举即可。
#include <bits/stdc++.h> using namespace std; const int M = 8100; int n, a[M], cnt[M]; void solve() { fill(cnt, cnt + M, 0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i], ++cnt[a[i]]; } int ans = 0; for (int i = 0; i < n; i++) { int sum = a[i]; for (int j = i + 1; j < n; j++) { sum += a[j]; if (sum < M and cnt[sum]) { ans += cnt[sum]; cnt[sum] = 0; } } } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
构造一个 01 串,要求:
依次构造 11,10,00 即可。
#include <bits/stdc++.h> using namespace std; void solve() { int n0, n1, n2; cin >> n0 >> n1 >> n2; vector<int> ans; if (n1 or n2) ans.push_back(1); for (int i = 0; i < n2; i++) ans.push_back(1); for (int i = 0; i < n1 / 2; i++) { ans.push_back(0); ans.push_back(1); } if (n1 & 1) { ans.push_back(0); for (int i = 0; i < n0; i++) ans.push_back(0); } else { if (ans.size()) { ans.pop_back(); for (int i = 0; i < n0; i++) ans.push_back(0); ans.push_back(1); } else { for (int i = 0; i < n0 + 1; i++) ans.push_back(0); } } for (auto i : ans) cout << i; cout << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
构造数 n 的一个排列,使得相邻两数相差在 [2, 4] 之间。
顺着 3 1 4 2 构造即可。
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n <= 3) { cout << -1 << "\n"; return; } else { for (int i = n - (n % 2 == 0); i >= 5; i -= 2) cout << i << ‘ ‘; cout << "3 1 4 2 "; for (int i = 6; i <= n - (n % 2 == 1); i += 2) cout << i << ‘ ‘; cout << "\n"; } } int main() { int t; cin >> t; while (t--) solve(); }
Codeforces Round #640 (Div. 4)
标签:ref ace while 个数 cti 构造 tps ring names
原文地址:https://www.cnblogs.com/Kanoon/p/12861746.html