CF1716
james1BadCreeper · · 个人记录
被染成灰色的大地,也别具风情呢。
A. Maxmina
观察样例不难发现只有全 NO。
#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[55];
void solve(void) {
cin >> n >> k; int s = 0;
for (int i = 1; i <= n; ++i) cin >> a[i], s += a[i];
if (s) cout << "YES\n";
else cout << "NO\n";
}
int main(void) {
ios::sync_with_stdio(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
B. Rebellion
双指针前后扫,贪心填即可。
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005];
void solve(void) {
cin >> n; a[n + 1] = 1;
for (int i = 1; i <= n; ++i) cin >> a[i];
int p = 0, q = n + 1, ans = 0;
while (p < q) {
while (p < n && a[p] == 0) ++p;
while (q > 1 && a[q] == 1) --q;
if (p >= q) break;
swap(a[p], a[q]); ++ans;
++p; --q;
}
cout << ans << "\n";
}
int main(void) {
ios::sync_with_stdio(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
C. Permutation Operations
由于自身是一个排列,因此直接从前往后扫,每个
#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005], p[100005];
void solve(void) {
set<int> s;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i], s.emplace(i);
for (int i = 2; i <= n; ++i) if (a[i] < a[i - 1]) {
auto it = s.lower_bound(a[i - 1] - a[i]);
p[*it] = i; s.erase(it);
}
for (int x : s) p[x] = 1;
for (int i = 1; i <= n; ++i) cout << p[i] << " \n"[i == n];
}
int main(void) {
ios::sync_with_stdio(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
D. Paths on the Tree
考虑做的暴力一点,设 dfs(x, k)代表
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
int n, k, s[200005];
vector<int> G[200005];
map<pair<int, int>, i64> ans;
i64 dfs(int x, int k) {
if (ans.find({x, k}) != ans.end()) return ans[{x, k}];
i64 &Ans = ans[{x, k}]; Ans = 1ll * s[x] * k;
int c = G[x].size();
if (!c) return Ans;
if (k % c == 0) {
for (int y : G[x]) Ans += dfs(y, k / c);
return Ans;
}
vector<i64> f1(c), f2(c), diff(c);
for (int i = 0; i < c; ++i) f1[i] = dfs(G[x][i], k / c), f2[i] = dfs(G[x][i], k / c + 1), diff[i] = f2[i] - f1[i], Ans += f1[i];
sort(diff.begin(), diff.end(), greater<i64>());
for (int i = 0; i < k % c; ++i) Ans += diff[i];
return Ans;
}
void solve(void) {
cin >> n >> k; ans.clear();
for (int i = 1; i <= n; ++i) G[i].clear();
for (int i = 2, u; i <= n; ++i) cin >> u, G[u].emplace_back(i);
for (int i = 1; i <= n; ++i) cin >> s[i];
cout << dfs(1, k) << "\n";
}
int main(void) {
ios::sync_with_stdio(0);
int T; cin >> T;
while (T--) solve();
return 0;
}
E. Joking
可能是我见过的最有趣的交互题之一。甚至超过 CF1764G3。
一个笨蛋做法是,一个询问连续询问两次。回答相同则一定是真的,但是不同就死了。
因此考虑将回答全部变成 YES。很简单,如果回答的是 NO,那么相当于我们询问了一个补集。
由于连续两次询问
问题是如何选择合适的
那么我们将
最后需要特判
问题出在了哪里?上一轮的
假定全集为 YES,那么
考虑 DP 计算出最优的划分方式。设
前者是回答 YES 时新的 NO 时新的
比较大的时候直接折半是很优秀的。
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
const int N = 100;
int n;
int f[105][105], x[105][105], y[105][105];
int main(void) {
ios::sync_with_stdio(0);
memset(f, 0x3f, sizeof(f));
for (int s = 0; s <= N; ++s)
for (int i = 0; i <= s; ++i) {
int j = s - i;
if (s <= 2) { f[i][j] = 0; continue; }
for (int x = 0; x <= i; ++x)
for (int y = 0; y <= j; ++y) {
int v = max(f[x + y][i - x], f[s - x - y][x]) + 1;
if (v < f[i][j])
f[i][j] = v, ::x[i][j] = x, ::y[i][j] = y;
}
}
cin >> n; VI S, T;
for (int i = 1; i <= n; ++i) S.push_back(i);
while (S.size() + T.size() > 2) {
int x = S.size() / 2, y = T.size() / 2;
if (S.size() + T.size() <= N) x = ::x[S.size()][T.size()], y = ::y[S.size()][T.size()];
VI S0(S.begin(), S.begin() + x);
VI S1(S.begin() + x, S.end());
VI T0(T.begin(), T.begin() + y);
VI T1(T.begin() + y, T.end());
cout << "? " << S0.size() + T0.size() << " ";
for (int i : S0) cout << i << " ";
for (int i : T0) cout << i << " ";
cout << endl;
string res; cin >> res; S.clear(), T.clear();
if (res == "YES") {
for (int i : S0) S.push_back(i);
for (int i : T0) S.push_back(i);
for (int i : S1) T.push_back(i);
} else {
for (int i : S1) S.push_back(i);
for (int i : T1) S.push_back(i);
for (int i : S0) T.push_back(i);
}
}
for (int i : T) S.emplace_back(i);
for (int i : S) {
cout << "! " << i << endl;
string res; cin >> res;
if (res == ":)") return 0;
}
return 0;
}
F. Kazaee
将每个数哈希成一个随机数,然后树状数组维护区间和,如果能被
#include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const i64 N = 1e13;
int n, m, a[300005], b[300005], B[600005], tot;
struct Query {
int op, l, r, k, ans;
} Q[300005];
mt19937_64 Rand(time(0));
inline i64 rnd(i64 l = 1, i64 r = N) { return uniform_int_distribution<i64>(l, r)(Rand); }
i64 C[300005];
inline void add(int x, i64 k) { for (; x <= n; x += x & -x) C[x] += k; }
inline i64 sum(int x) { i64 r = 0; for (; x; x -= x & -x) r += C[x]; return r; }
i64 mp[600005];
void solve(void) {
memset(C, 0, sizeof C);
for (int i = 1; i <= tot; ++i) mp[i] = rnd();
for (int i = 1; i <= n; ++i) add(i, mp[b[i] = a[i]]);
for (int i = 1; i <= m; ++i)
if (Q[i].op == 1) add(Q[i].l, -mp[b[Q[i].l]]), add(Q[i].l, mp[b[Q[i].l] = Q[i].k]);
else if (Q[i].ans && (sum(Q[i].r) - sum(Q[i].l - 1)) % Q[i].k != 0) Q[i].ans = 0;
}
int main(void) {
ios::sync_with_stdio(0);
cin >> n >> m;
for (int i = 1; i <= n; ++i) cin >> a[i], B[++tot] = a[i];
for (int i = 1; i <= m; ++i) {
cin >> Q[i].op;
if (Q[i].op == 1) cin >> Q[i].l >> Q[i].k, B[++tot] = Q[i].k;
else cin >> Q[i].l >> Q[i].r >> Q[i].k, Q[i].ans = ((Q[i].r - Q[i].l + 1) % Q[i].k == 0);
}
sort(B + 1, B + tot + 1); tot = unique(B + 1, B + tot + 1) - (B + 1);
for (int i = 1; i <= n; ++i) a[i] = lower_bound(B + 1, B + tot + 1, a[i]) - B;
for (int i = 1; i <= m; ++i) if (Q[i].op == 1) Q[i].k = lower_bound(B + 1, B + tot + 1, Q[i].k) - B;
for (int op = 1; op <= 30; ++op) solve();
for (int i = 1; i <= m; ++i)
if (Q[i].op == 2) cout << (Q[i].ans ? "YES" : "NO") << "\n";
return 0;
}
G. Olympiad Training
太牛逼了,不会。