CodeForces 1946C Tree Cutting

Tree Cutting 考虑从叶子节点开始切割,每次切割的时候只能切掉整颗子树,因此从叶子节点统计当前子树的节点的个数,当子树大小至少为 $x$ 的时候进行一次切割,最后切掉的子树的数量不少于 $k$。因为是从叶子节点考虑,所以可以考虑拓扑排序的过程,用 BFS 的思想从叶子开始切割。 更好的实现方式是从子树节点个数的角度考虑,这是一个经典的 DFS 问题,当子树的大小至少为 $x$ 时,此时进行一次切割,这个子树的大小不贡献给父节点。 DFS 实现,DFS 的过程中只需要记录上一次访问的节点就可以保证不重复访问。 // Date: Sat Mar 23 09:39:02 2024 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; typedef pair<ll, ll> PLL; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353; const ll INFL = 0x3f3f3f3f'3f3f3f3f; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } // int128 input and output #ifdef _DEBUG using lll = __int128; istream &operator>>(istream &is, lll &v) { string s; is >> s; v = 0; for (auto &it : s) if (isdigit(it)) v = v * 10 + it - '0'; if (s[0] == '-') v *= -1; return is; } ostream &operator<<(ostream &os, const lll &v) { if (v == 0) return (os << "0"); lll num = v; if (v < 0) os << '-', num = -num; string s; for (; num > 0; num /= 10) s.pb((char)(num % 10) + '0'); reverse(all(s)); return (os << s); } #endif // end of int128 void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 100100, M = 2 * N; int n, k, u, v, h[N], cnt; int idx, e[M], ne[M]; void Init() { idx = 0; memset(h, -1, sizeof h); } void Add(int a, int b) { e[idx] = b, ne[idx] = h[a], h[a] = idx++; } int dfs(int u, int pre, int x) { int sum = 1; ForE(i, u) { int j = e[i]; if (j == pre) continue; sum += dfs(j, u, x); } if (sum >= x) { cnt++; sum = 0; } return sum; } bool check(int x) { cnt = 0; dfs(1, -1, x); return cnt >= k + 1; } void solve() { cin >> n >> k; Init(); For(i, 1, n) { cin >> u >> v; Add(u, v); Add(v, u); } int l = 1, r = 1e5 + 10, mid; while (l < r) { mid = (l + r + 1) / 2; if (check(mid)) l = mid; else r = mid - 1; } cout << l << '\n'; } int main(void) { #ifdef _DEBUG freopen("1946c.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; cin >> T; while (T--) { solve(); } return 0; } BFS 实现,类似拓扑排序的思想,把度数为 $1$ 的叶子节点加入队列,此时需要记录节点是否已经被访问过。 ...

March 23, 2024 · 10 min · 1962 words

CodeForces 1721C Min-Max Array Transformation

Min-Max Array Transformation 第一问求 $d_{min}$ 很简单,只需要把 $b$ 排序,然后对 $a_i$ 找到最小的 $b_i \ge a_i$ 即可。 第二问很有意思,对于 $a_i$,需要找到尽量大的 $b_j \ge a_i$,同时满足 $a_{i+1}, \ldots, a_n$ 都能找到对应的匹配,同时尽可能小。我们可以对于每个 $a_j (j \ge i+1)$ 都找到最小的 $b_j \ge a_j$,然后从数组中删除这些 $b_j$,此时数组中剩余的最大值就可以匹配 $a_i$。我们可以发现,这样做不会让 $a_{i - 1}$ 的结果变差,在这个基础上,删除掉 $\ge a_i$ 的最小的 $b_j$ 之后,这恰好是 $a_{i - 1}$ 想要的 $b$ 数组。因此可以从右往左处理。 // Date: Fri Feb 2 23:06:22 2024 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353; const ll INFL = 0x3f3f3f3f'3f3f3f3f; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 200100; int n, a[N], b[N], d[N], d1[N]; multiset<int> s; void solve() { cin >> n; For1(i, 1, n) { cin >> a[i]; } sort(a + 1, a + 1 + n); s.clear(); For1(i, 1, n) { cin >> b[i]; s.insert(b[i]); } sort(b + 1, b + 1 + n); For1(i, 1, n) { int l = 1, r = n, mid; while (l < r) { mid = (l + r) / 2; if (b[mid] >= a[i]) r = mid; else l = mid + 1; } d[i] = r; } For1(i, 1, n) cout << b[d[i]] - a[i] << ' '; NL; Rof1(i, 1, n) { auto it = s.lower_bound(a[i]); d1[i] = *prev(s.end()) - a[i]; s.erase(it); } For1(i, 1, n) cout << d1[i] << ' '; NL; } int main(void) { #ifdef _DEBUG freopen("1721c.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; cin >> T; while (T--) { solve(); } return 0; }

March 2, 2024 · 4 min · 849 words

CodeForces 1701C Schedule Management

Schedule Management 如果一个任务不能由最擅长它的员工完成,那么其他任何一个员工都是可以的,花费的时间都是 $2$,为了让总时间最小,应该让剩余时间多的员工优先完成,对于给定的总时间,尽量让每个员工的工作量是饱和的。可以发现判定某个总时间是否合法是可以通过上面的贪心思想 $O(n)$ 完成,因此可以二分答案。 // 2024/1/29 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353; const ll INFL = 0x3f3f3f3f'3f3f3f3f; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #define dbg(x...) \ cerr << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 200100; int n, m, a[N], b[N]; bool check(int top) { int cnt {}; For1(i, 1, n) { if (b[i] > top) { cnt += (b[i] - top); } else if (cnt) { int rem = top - b[i]; rem /= 2; if (rem >= cnt) cnt = 0; else cnt -= rem; } } return !cnt; } void solve() { cin >> n >> m; fill(b, b + 1 + n, 0); For1(i, 1, m) { cin >> a[i]; b[a[i]]++; } sort(b + 1, b + 1 + n, greater<int>()); int l = 1, r = m, mid; while (l < r) { mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid + 1; } cout << r << '\n'; } int main(void) { #ifdef _DEBUG freopen("input.txt", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; cin >> T; while (T--) { solve(); } return 0; }

February 27, 2024 · 4 min · 797 words

CodeForces 1840D Wooden Toy Festival

Wooden Toy Festival tutorial 先把原数组排序,分成三段,每一段分别给一个人,假设对于答案 $t$,第一段的范围是 $[a_1, a_1 + 2t]$,最后一段的范围是 $[a_n - 2t, a_n]$,只需要看中间那段的数据范围是不是在 $2t$ 的范围内。这个判定步骤可以 $O(N)$,或者二分 $O(log(N))$。所以可以二分答案 $t$。 // Date: Mon Jan 8 22:33:03 2024 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; template <class T> using pq = priority_queue<T>; template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7, MOD1 = 998'244'353; const ll INFL = 0x3f3f3f3f'3f3f3f3f; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nemp(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #define NL cout << '\n'; template <class T> bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << '\'' << x << '\''; } void __print(const char *x) { cerr << '\"' << x << '\"'; } void __print(const string &x) { cerr << '\"' << x << '\"'; } void __print(bool x) { cerr << (x ? "true" : "false"); } template <typename T, typename V> void __print(const pair<T, V> &x) { cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}'; } template <typename T> void __print(const T &x) { int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}"; } void _print() { cerr << "]\n"; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #define dbg(x...) \ cerr << "\e[91m" << __func__ << ":" << __LINE__ << " [" << #x << "] = ["; \ _print(x); \ cerr << "\e[39m" << endl; #else #define debug1 #define debug2 #define debug3 #define dbg(x...) #endif const int N = 200100; int n; ll a[N]; bool check(ll t) { int l = 1; while (l <= n && a[l] <= a[1] + 2 * t) ++l; int r = n; while (r >= 1 && a[r] >= a[n] - 2 * t) --r; if (l > r) return true; return a[r] - a[l] <= 2 * t; } void solve() { cin >> n; For1(i, 1, n) cin >> a[i]; sort(a + 1, a + 1 + n); ll lo = 0, hi = 1e9 + 10, mid; while (lo < hi) { mid = (lo + hi) / 2; if (check(mid)) hi = mid; else lo = mid + 1; } cout << hi << '\n'; } int main(void) { #ifdef _DEBUG freopen("1840d.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int T = 1; cin >> T; while (T--) { solve(); } return 0; }

January 8, 2024 · 4 min · 809 words

CodeForces 1878E Iva & Pav

Iva & Pav tutorial 由于 $f(l, r) = a_l \And a_{l + 1} \ldots \And a_{r}$ 是有单调性的,因此可以考虑二分答案。接下来是如何快速求 $f(l, r)$,可以按位考虑,对每一个二进制位,计算区间内这个二进制位为 1 的个数,如果恰好等于区间的长度,说明区间内所有数字的这位都为 1,累加到答案中。数区间内 1 的个数可以使用前缀和快速得到。 // Date: Wed Dec 27 23:50:18 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 200010; int t, q, a[N], d[N][35], l, k, n; int check(int l, int r) { int res = 0; For(j, 0, 32) { int cn = d[r][j] - d[l - 1][j]; if (cn == r - l + 1) { res += (1 << j); } } return res; } int main(void) { #ifdef _DEBUG freopen("1878e.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; For1(i, 1, n) { cin >> a[i]; } cin >> q; For(j, 0, 32) d[0][j] = 0; For1(i, 1, n) { int tmp = a[i]; For(j, 0, 32) { int cnt = 0; if (tmp & 1) cnt = 1; tmp >>= 1; d[i][j] = d[i - 1][j] + cnt; } } while (q--) { cin >> l >> k; int r = n, mid; int le = l; while (le < r) { mid = (le + r + 1) / 2; if (check(l, mid) >= k) le = mid; else r = mid - 1; } if (check(l, le) >= k) cout << le; else cout << -1; cout << ' '; } cout << '\n'; } return 0; }

December 28, 2023 · 3 min · 547 words

CodeForces 1883G1 Dances (Easy version)

Dances (Easy version) 对于每个 $a_i$,找到尽量小的 $b_j > a_i$,如果不存在,那么就应该移除。可以把 $a, b$ 排序,二分找 $b_j$。 // Date: Wed Dec 27 15:26:41 2023 #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <climits> #include <iostream> #include <iomanip> #include <sstream> #include <string> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <algorithm> #include <utility> #include <functional> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(),(a).end() #define SZ(a) int((a).size()) template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } #ifdef _DEBUG #define debug1(x) cout << #x" = " << x << endl; #define debug2(x,y) cout << #x" = " << x << " "#y" = " << y << endl; #define debug3(x,y,z) cout << #x" = " << x << " "#y" = " << y << " "#z" = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 100010; int n, m, a[N], b[N], t; int solve(int x, int l) { int r = n, mid; if (l > r) return -1; while (l < r) { mid = (l + r) / 2; if (b[mid] > x) r = mid; else l = mid + 1; } if (b[r] > x) return r; return -1; } int main(void) { #ifdef _DEBUG freopen("1883g1.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> m; a[1] = 1; For1(i, 2, n) cin >> a[i]; For1(i, 1, n) cin >> b[i]; sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n); int res = 0, l = 1; For1(i, 1, n) { int pos = solve(a[i], l); if (pos == -1) { res = n - i + 1; break; } l = pos + 1; } cout << res << '\n'; } return 0; }

December 27, 2023 · 3 min · 486 words

CodeForces 1288D Minimax Problem

Minimax Problem tutorial 要求数组 $b$ 的最小值最大,考虑二分,可以对 $min_{k = 1}^m b_k$ 进行二分,对于 $x$,判定是否存在两个数组 $a_i, a_j$,使得它们组成的数组 $b$ 中的所有的值都 $\ge x$,数组 $a_i$ 的个数有 $3 \cdot 10^5$ 个,但是长度只有 $8$,可以想到用二进制表示数组 $a_i$,当 $a_{i,k} \ge x$ 时,对应的二进制位为 1。这样数组 $a_i$ 最多只有 $2^8$ 个,因此一次判定操作的复杂度是 $O(n \cdot 8 + (2^8)^{2})$ // Date: Sun Dec 24 09:26:26 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) template <typename t> istream &operator>>(istream &in, vector<t> &vec) { for (t &x : vec) in >> x; return in; } template <typename t> ostream &operator<<(ostream &out, vector<t> &vec) { int n = SZ(vec); For(i, 0, n) { out << vec[i]; if (i < n - 1) out << ' '; } return out; } #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 3000'10, M = 10; int n, m, a[N][M]; PII check(int x) { set<int> s; map<int, int> b2i; For1(i, 1, n) { int tmp = 0, base = 1; For1(j, 1, m) { if (a[i][j] < x) { } else { tmp += base; } base <<= 1; } s.insert(tmp); b2i[tmp] = i; } for (auto x : s) { for (auto y : s) { int z = (x | y); if (z == ((1 << m) - 1)) { return {b2i[x], b2i[y]}; } } } return {-1, -1}; } int main(void) { #ifdef _DEBUG freopen("1288d.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); while (cin >> n >> m) { For1(i, 1, n) { For1(j, 1, m) { cin >> a[i][j]; } } int l = 0, r = 1e9 + 10, mid; while (l < r) { mid = (l + r + 1) / 2; if (check(mid) != PII{-1, -1}) l = mid; else r = mid - 1; } auto res = check(l); cout << res.f1 << ' ' << res.f2 << '\n'; } return 0; }

December 24, 2023 · 3 min · 549 words

CodeForces 1703F Yet Another Problem About Pairs Satisfying an Inequality

Yet Another Problem About Pairs Satisfying an Inequality 先找到所有 $a_i < i$ 的元素放到数组 $b$,数组根据 $a_i$ 排序,再对每个 $b_i$ 找到第一个大于 $i$ 的元素,可以使用 upper_bound。另外, lower_bound 可以找到第一个 $\ge$ 给定元素的位置。 // Date: Sat Dec 16 09:52:26 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 200010; int t, n, a[N]; vector<PII> b; int main(void) { #ifdef _DEBUG freopen("1703f.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; b.clear(); For(i, 0, n) { cin >> a[i]; if (a[i] < i + 1) { b.pb({a[i], i + 1}); } } ll res = 0; if (b.empty()) { cout << "0\n"; continue; } sort(all(b)); for (auto &[x, y] : b) { auto it = upper_bound(b.begin(), b.end(), PII{y, INF}); if (it != b.end()) { res += b.end() - it; } } cout << res << '\n'; } return 0; }

December 16, 2023 · 2 min · 398 words

CodeForces 1809B Points on Plane

Points on Plane 模拟前几个样例可以发现需要根据奇偶性讨论。反向考虑,当结果为 $0, 1, 2, 3, 4$ 的时候的节点分配是怎样的,可以得到当答案 $k$ 是奇数的时候,能够放下的最多的点数是 $2(k + 1)(\frac{k}{2} + 1)$;当答案 $k$ 是偶数的时候,能够放下的最多的点数是 $(k + 1)^2$。然后就可以二分答案了。 // Date: Thu Dec 7 21:25:48 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif int t; ll n; bool check(ll x) { ll cnt; if (x & 1) { cnt = (x + 1) * (x / 2 + 1) * 2; } else { cnt = (1 + x) * (1 + x); } return cnt < n; } int main(void) { #ifdef _DEBUG freopen("1809b.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; if (n == 1) { cout << "0\n"; continue; } ll l = 0, r = 1e10, mid; while (l < r) { mid = (l + r + 1) / 2; if (check(mid)) l = mid; else r = mid - 1; } cout << l + 1 << '\n'; } return 0; }

December 7, 2023 · 2 min · 416 words

CodeForces 1476B Inflation

Inflation tutorial 这题有点意思,首先要发现递增 $p_0$ 总是最优的:设 $c_j = \frac{p_j}{preSum_{j-1}}$,如果递增的是 $p_i$,如果 $i > j$,此时 $c_j$ 不变;如果 $i = j$,此时 $c_j$ 变大;如果 $i < j$,那么 $c_j$ 变小。我们希望对某个元素 $p_j$ 递增的时候,之前的性质能够保持不变,所以每次都递增 $p_0$ 即可。判定整个数列满足题目要求的性质是线性的,所以可以二分答案。 // Date: Sun Dec 3 17:54:52 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 110; int t; ll n, k, p[N], b[N]; bool check(ll x) { For1(i, 2, n) { ll l = 100 * p[i], r = k * (x + b[i - 1]); if (l > r) return false; } return true; } int main(void) { #ifdef _DEBUG freopen("1476b.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> k; memset(b, 0, sizeof b); For1(i, 1, n) { cin >> p[i]; b[i] = b[i - 1] + p[i]; } ll l = 0, r = 1e11 + 10, mid; while (l < r) { mid = (l + r) / 2; if (check(mid)) r = mid; else l = mid + 1; } cout << r << '\n'; } return 0; }

December 3, 2023 · 3 min · 430 words

CodeForces 1538C Number of Pairs

Number of Pairs 题目和元素顺序无关,先排序,然后对每个元素二分找符合条件的左端点和右端点,注意边界,二分之前如果左右端点是空集,直接返回 $-1$。 // Date: Sat Nov 25 19:10:08 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif const int N = 200010; int t, n, l, r, a[N]; int find_left(int left, int right, int x) { if (left > right) return -1; int l = left, r = right, mid; while (l < r) { mid = (l + r) / 2; if (a[mid] >= x) r = mid; else l = mid + 1; } if (a[r] >= x) return r; return -1; } int find_right(int left, int right, int x) { if (left > right) return -1; int l = left, r = right, mid; while (l < r) { mid = (l + r + 1) / 2; if (a[mid] <= x) l = mid; else r = mid - 1; } if (a[l] <= x) return l; return -1; } int main(void) { #ifdef _DEBUG freopen("1538c.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> l >> r; For1(i, 1, n) { cin >> a[i]; } sort(a + 1, a + 1 + n); ll res = 0; For1(i, 1, n) { int left = find_left(i + 1, n, l - a[i]), right = find_right(i + 1, n, r - a[i]); if (left != -1 && right != -1) { res += right - left + 1; } } cout << res << '\n'; } return 0; }

November 25, 2023 · 3 min · 499 words

CodeForces 1612C Chat Ban

Chat Ban 等差数列求和,找到中点 $k$,前半部分求和之后,分三种情况,前半部分和后半部分再利用等差数列求和来二分答案。 // Date: Fri Nov 24 21:14:51 2023 #include <climits> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <functional> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; const int INF = 0x3f3f3f3f, MOD = 1e9 + 7; const double eps = 1e-8; const int dir[8][2] = { {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}, }; typedef long long ll; typedef unsigned long long ull; typedef vector<int> VI; typedef pair<int, int> PII; const ull Pr = 131; #define For(i, a, b) for (int i = int(a); i < int(b); ++i) #define Rof(i, a, b) for (int i = int(b) - 1; i >= int(a); --i) #define For1(i, a, b) for (int i = int(a); i <= int(b); ++i) #define Rof1(i, a, b) for (int i = int(b); i >= int(a); --i) #define ForE(i, j) for (int i = h[j]; i != -1; i = ne[i]) #define f1 first #define f2 second #define pb push_back #define has(a, x) (a.find(x) != a.end()) #define nonempty(a) (!a.empty()) #define all(a) (a).begin(), (a).end() #define SZ(a) int((a).size()) #ifdef _DEBUG #define debug1(x) cout << #x " = " << x << endl; #define debug2(x, y) cout << #x " = " << x << " " #y " = " << y << endl; #define debug3(x, y, z) \ cout << #x " = " << x << " " #y " = " << y << " " #z " = " << z << endl; #else #define debug1 #define debug2 #define debug3 #endif int t; int main(void) { #ifdef _DEBUG freopen("1612c.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; ll k, x; while (t--) { cin >> k >> x; ll res = 0, top = (1 + k) * k / 2, l, r, mid, sum; if (x == top) { res = k; } else if (x < top) { l = 1, r = k; while (l < r) { mid = (l + r) / 2; sum = mid * (1 + mid) / 2; if (sum >= x) r = mid; else l = mid + 1; } res = r; } else { res = k; x -= top; l = 1, r = k - 1; while (l < r) { mid = (l + r) / 2; sum = k * mid - (mid + 1) * mid / 2; if (sum >= x) r = mid; else l = mid + 1; } sum = k * r - (r + 1) * r / 2; if (sum < x) { res = 2 * k - 1; } else { res += r; } } cout << res << '\n'; } return 0; }

November 24, 2023 · 3 min · 478 words