CodeForces 1364B Most socially-distanced subsequence

Most socially-distanced subsequence 这道题目比较简单,对于一个递增或者递减的连续子数列,中间的数字对于答案是没有贡献的,只有两端的端点有意义,因此这道题就转化为求整个数列所有的极大值和极小值。 // Date: Wed Nov 29 22:01:47 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 = 100010; int t, n, a[N], b[N]; int main(void) { #ifdef _DEBUG freopen("1364b.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]; } int idx = 1; bool flag = true; b[idx++] = a[1]; if (a[2] > a[1]) flag = true; else flag = false; For1(i, 3, n) { if (flag) { if (a[i] > a[i - 1]) ; else { b[idx++] = a[i - 1]; flag = false; } } else { if (a[i] < a[i - 1]) ; else { b[idx++] = a[i - 1]; flag = true; } } } b[idx++] = a[n]; cout << idx - 1 << '\n'; For(i, 1, idx) { cout << b[i] << ' '; } cout << '\n'; } return 0; }

November 30, 2023 · 2 min · 413 words

CodeForces 1804B Vaccination

Vaccination 这是一道很不错的贪心题目。为了使得开箱的疫苗最少,我们尽量让每个人接种时间靠后,来延长疫苗的过期时间,使得疫苗能够被后面的人接种到。因此对于第一个人 $a_1$,我们就可以规定开箱时间是 $a_1 + w$,那么这箱的过期时间是 $a_1 + w + d$,在这箱过期之前最多能接种 $k$ 个人,如果这之前多于 $k$ 个人,那么只能再开一箱了。这样下去,就能够保证每个人都能接种,并且每箱都能最大化利用,也就是尽量少地开箱。 // Date: Thu Nov 30 21:15:29 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, k, d, w, a[N]; int main(void) { #ifdef _DEBUG freopen("1804b.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> k >> d >> w; For1(i, 1, n) { cin >> a[i]; } int res = 0, cnt = 0; For1(i, 1, n) { if (!cnt) { int top = a[i] + w + d; cnt = 1; res++; ++i; while (cnt < k && i <= n) { if (a[i] <= top) { cnt++; ++i; } else break; } --i; cnt = 0; } } cout << res << '\n'; } return 0; }

November 30, 2023 · 2 min · 396 words

CodeForces 1717C Madoka and Formal Statement

Madoka and Formal Statement 分三种情况,当 $a_i \ge b_i$ 的时候比较简单。当 $a_i \lt b_i$ 的时候,需要递增 $a_i$,此时它的上限是 $b_{i + 1} + 1$,如果达不到这个上限那么就没有解。 // Date: Sun Nov 26 20:01:43 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], b[N]; int main(void) { #ifdef _DEBUG freopen("1717c.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]; } For1(i, 1, n) { cin >> b[i]; } bool flag = true; For1(i, 1, n) { if (a[i] > b[i]) { flag = false; break; } else if (a[i] < b[i]) { int idx = i + 1; if (idx == n + 1) idx = 1; if (b[idx] + 1 < b[i]) { flag = false; break; } } } cout << (flag ? "YES\n" : "NO\n"); } return 0; }

November 26, 2023 · 2 min · 402 words

CodeForces 1810C Make It Permutation

Make It Permutation 两种操作都和顺序无关,要生成一个 $[1, n]$ 的排列,先把原数组排序,对于每个点有三种策略:1. 从这个点开始后面的元素都删除。2. 当前元素比需要的下标 $idx$ 小,此时删除这个元素。3. 当前元素比需要的下标 $idx$ 大,补足中间缺少的数字。注意整型溢出。 // Date: Sun Nov 26 16:48:05 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 = 100010; int t, n, c, d, a[N]; int main(void) { #ifdef _DEBUG freopen("1810c.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> c >> d; For1(i, 1, n) { cin >> a[i]; } sort(a + 1, a + 1 + n); ll res = ll(n) * c + d, cur = 0; int idx = 1; For1(i, 1, n) { if (a[i] == idx) { idx++; } else { ll cur1 = cur + ll(n - i + 1) * c; if (i > 1) { res = min(res, cur1); } else { res = min(res, cur1 + d); } if (a[i] > idx) { cur += ll(a[i] - idx) * d; idx = a[i] + 1; } else { cur += c; } } } res = min(res, cur); cout << res << '\n'; } return 0; }

November 26, 2023 · 3 min · 436 words

CodeForces 1667A Make it Increasing

Make it Increasing tutorial 若 $b_1 > 0$ 或者 $b_n < 0$ 那么肯定不是最优解,因为此时把 $b_1$ 或者 $b_n$ 设置成 0 不会使得结果更差,同时也能够保持数组递增。如果 $b_i - a_i > b_{i - 1}$,此时也不是最优解,此时把 $b_i$ 变成 $b_i - a_i$ 不会使得结果更差,也能够保持数组递增。猜测最终的数组 $b$ 中必有一个元素是 $0$(如何证明?),然后枚举为 $0$ 的位置即可。 根据上面的分析,最终的数组满足 $b_1 \le 0, b_n \ge 0$,由于数组 $b$ 是严格递增的,所以其中必定有一个元素是 $0$。 // Date: Fri Nov 24 19:17:58 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 = 5010; int t, n, a[N]; int main(void) { #ifdef _DEBUG freopen("1667a.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); while (cin >> n) { For1(i, 1, n) { cin >> a[i]; } if (n == 1) { cout << "0\n"; continue; } ll res = -1; For1(i, 1, n) { ll sum = 0, pre = 0; Rof1(j, 1, i - 1) { if (pre == 0) { pre = a[j]; sum++; } else { sum += pre / a[j]; if (pre % a[j] == 0) { pre += a[j]; sum++; } else { pre = pre - (pre % a[j]) + a[j]; sum++; } } } pre = 0; For1(j, i + 1, n) { if (pre == 0) { pre = a[j]; sum++; } else { sum += pre / a[j]; if (pre % a[j] == 0) { pre += a[j]; sum++; } else { pre = pre - (pre % a[j]) + a[j]; sum++; } } } if (res == -1) res = sum; else res = min(res, sum); } cout << res << '\n'; } return 0; }

November 24, 2023 · 3 min · 503 words