#include<bits/stdc++.h> usingnamespace std; using i64=longlong; constint MAXN=5e5+10; voidsolve() { i64 n, m, k; std::cin >> n >> m >> k; std::vector<i64> a(k); for (int i = 0; i < k; ++i) { std::cin >> a[i]; } sort(begin(a), end(a), greater<int>()); //先按照列进行染色 bool ok = false; i64 res = 0, leave = 0; for (int i = 0; i < k; ++i) { i64 cal = a[i] / n; //可以填充的列数 cal = min (cal, m - res); //计算需要填充的数目 if(cal < 2) continue; res += 2; //现在至少可以填充两列 leave += cal - 2; //剩下可以填充的 } ok |= (res + leave >= m); //能否全部填充完毕
swap(n, m); //一样的处理 res = 0, leave = 0; for (int i = 0; i < k; ++i) { i64 cal = a[i] / n; cal = min (cal, m - res); if(cal < 2) continue; res += 2; leave += cal - 2; } ok |= (res + leave >= m); cout << (ok ? "YES" : "NO") << "\n"; } intmain(){ int t=1; cin >> t; for (int i = 0; i < t; ++i) { //cerr<<"case "<<i<<": "; solve(); } }
#include<bits/stdc++.h> usingnamespace std; using i64=longlong; constint MAXN=5e5+10; voidsolve() { int n; std::cin >> n; i64 x; std::cin >> x; std::vector<i64> a(n), l(n), r(n); for (int i = 0; i < n; ++i) { std::cin >> a[i]; l[i] = a[i] - x; r[i] = a[i] + x; } int ans = 0; i64 low = l[0], high = r[0]; for (int i = 1; i < n; ++i) { if(high >= l[i] && low <= l[i]) { low = max(low, l[i]); high = min(high, r[i]); } elseif(low <= r[i] && high >= r[i]) { low = max(low, l[i]); high = min(high, r[i]); } elseif(low >= l[i] && high <= r[i]) continue; else { ans++; low = l[i]; high = r[i]; } } std::cout << ans <<"\n"; } intmain(){ int t=1; cin >> t; for (int i = 0; i < t; ++i) { //cerr<<"case "<<i<<": "; solve(); } }
Code2
#include<bits/stdc++.h> #define lowbit(x) x & (-x) usingnamespace std; using i64=longlong; constint MAXN=1e5+10; voidsolve() { int n, x; cin >> n >> x; std::vector<int> a(n); for (int i = 0; i < n; ++i) { cin >> a[i]; } int l = a[0] - x, r = a[0] + x; int ans = 0; for (int i = 1; i < n ; ++i) { if(r < a[i] - x || a[i] + x < l) { ans++; l = a[i] - x; r = a[i] + x; } else { l = max(l, a[i] - x); r = min(r, a[i] + x); //continue; } } cout << ans << "\n"; } intmain(){ int t = 1; cin >> t; for (int i = 0; i < t; ++i) { //cerr<<"case "<<i<<": "; solve(); } }
这里提一嘴为什么会是构造:\(h=(\lceil
\sqrt{2n}\rceil)^2\),因为最大就是\(n +
n =2n\)。所以是这样构造。
代码
#include<bits/stdc++.h> usingnamespace std; using i64=longlong; constint MAXN=5e5+10; voidsolve() { int n; cin >> n; std::vector<int> ans(n+1); auto f = [&](auto f, int r) -> void { if(r < 0) return; int num = sqrt(2 * r); num *= num; int l = num - r; //注意一下l和r是互补对称的 f(f, l - 1); for (; l <= r; ++l, --r) { ans[l] = r; ans[r] = l; } }; f(f, n - 1); for (int i = 0; i < n; ++i) { cout << ans[i] <<" \n"[i==n-1]; } } intmain(){ int t=1; cin >> t; for (int i = 0; i < t; ++i) { //cerr<<"case "<<i<<": "; solve(); } }