CodeForces 1706C Qpwoeirut And The City

Qpwoeirut And The City 对于奇数长度的数组,为了让合法的元素最多,只有一种情况。 对于偶数长度的数组,如果把合法的点标记为 $1$,其它点标记为 $0$,可以发现 $0$ 和 $1$ 的排列规律,首先不能出现连续的 $1$,并且最多出现一次连续的两个 $0$,连续的两个 $0$ 出现的位置决定了最终的结果。 从 $a_2$ 开始,假设 $a_2 = 1, a_3 = 0, \ldots$ 这是一种情况。从 $a_{n - 1}$ 开始,假设 $a_{n - 1} = 1, a_{n - 2} = 0, \ldots$ 这是对称的另外一种情况。对这两种情况分别求出前缀和与后缀和,然后枚举 $i$,就可以求出答案。 // 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 = 300100; int n; ll a[N], p[N], p1[N]; void solve() { cin >> n; For1(i, 1, n) { cin >> a[i]; } ll ans{}; auto check = [](int start) { ll ans = 0; for (int i = start; i <= n - 1; i += 2) { if (a[i] > a[i - 1] && a[i] > a[i + 1]) continue; ll top = max(a[i - 1], a[i + 1]) + 1; ans += top - a[i]; } return ans; }; if (n & 1) { ans = check(2); cout << ans << '\n'; return; } p[1] = 0; for (int i = 2; i <= n; i += 2) { ll top = max(a[i - 1], a[i + 1]) + 1, delta = max(top - a[i], 0LL); p[i] = p[i + 1] = delta + p[i - 1]; } p1[n] = 0; for (int i = n - 1; i >= 1; i -= 2) { ll top = max(a[i - 1], a[i + 1]) + 1, delta = max(top - a[i], 0LL); p1[i] = p1[i - 1] = delta + p1[i + 1]; } ans = -1; for (int i = 2; i <= n; i += 2) { ll tmp = p[i - 1] + p1[i]; if (ans == -1) ans = tmp; else ckmin(ans, tmp); } cout << ans << '\n'; } int main(void) { #ifdef _DEBUG // freopen("1706c.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; }

February 28, 2024 · 5 min · 933 words

CodeForces 1535C Unstable String

Unstable String 有意思的 $DP$ 题目。如果 $s_i$ 是 $?$,它是 $1$ 还是 $0$ 取决于 $s_{i - 1}$,因此想到 $dp$,设 $d_{0, i}$ 表示以 $s_i$ 为结尾,并且 $s_i = 0$ 的合法的最长连续子字符串的个数,$d_{1, i}$ 同理。最终答案就是 $\sum max(d_{0, i}, d_{1, i})$ // Date: Sun Jan 14 23:57:50 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; string s; int n, d[2][N]; void solve() { cin >> s; memset(d, 0, sizeof d); n = SZ(s); if (s[0] == '?') d[0][0] = d[1][0] = 1; else if (s[0] == '1') d[1][0] = 1; else d[0][0] = 1; For(i, 1, n) { if (s[i] == '?') { d[0][i] = 1 + d[1][i - 1]; d[1][i] = 1 + d[0][i - 1]; } else if (s[i] == '1') { d[0][i] = 0; d[1][i] = d[0][i - 1] + 1; } else { d[1][i] = 0; d[0][i] = d[1][i - 1] + 1; } } ll ans = 0; For(i, 0, n) { ans = ans + max(d[0][i], d[1][i]); } cout << ans << '\n'; } int main(void) { #ifdef _DEBUG freopen("1535c.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 15, 2024 · 4 min · 820 words

CodeForces 1526C1 Potions (Easy Version)

Potions (Easy Version) tutorial $d_{i, j}$ 表示前 $i$ 个药瓶中,取 $j$ 个能够获得的最大价值,题目要求保证存活的情况下能够获得的最大的个数,也就是求当 $d_{n, j} \ge 0$ 时的最大的 $j$,为了让 $d_{i, j} \ge 0$,可以考虑求 $d_{i, j}$ 的最大值,这就转化成了常规的 $dp$,转移方程: $$ d_{i, j} = \begin{cases} max(d_{i - 1, j - 1} + a_i, d_{i - 1, j}) &\text{if } d_{i - 1, j - 1} + a_i \ge 0 \ d_{i - 1, j} &\text{if } d_{i - 1, j - 1} + a_i < 0 \end{cases} $$ ...

January 12, 2024 · 4 min · 817 words

CodeForces 1736C1 Good Subarrays (Easy Version)

Good Subarrays (Easy Version) tutorial 因为要求的好子数组是连续的,设 $d_i$ 表示以 $a_i$ 结尾的最长好数组的长度。当 $a_i \ge d_{i - 1} + 1$ 时,有 $d_i = d_{i - 1} + 1$。当 $a_i \lt d_{i - 1} + 1$ 时,注意到 $a_i \le d_{i - 1}$,并且以 $a_i$ 结尾的好数组的最大长度不能超过 $a_i$,这要求 $a_i$ 前面要有长度为 $a_i - 1$ 的好数组,此时恰好有 $a_i - 1 \lt a_i \le d_{i - 1}$,也就是 $a_i$ 前面必定有长度为 $a_i - 1$ 的好数组,所以此时 $d_i = a_i$。答案就是 $\sum_{i = 1}^n d_i$ // Date: Fri Dec 22 22:36:16 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, n, a[N], d[N]; int main(void) { #ifdef _DEBUG freopen("1736c1.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n; // TLE // memset(d, 0, sizeof d); For1(i, 1, n) cin >> a[i]; ll res = 0; d[0] = 0; For1(i, 1, n) { if (a[i] >= d[i - 1] + 1) { d[i] = d[i - 1] + 1; } else { d[i] = a[i]; } res += d[i]; } cout << res << '\n'; } return 0; }

December 24, 2023 · 3 min · 486 words

CodeForces 1420C1 Pokémon Army (easy version)

Pokémon Army (easy version) 需要考虑的形式是 $-b+c$,所以元素可以分为两类,一种是前面带负号的,例如 $b$,另一种是前面带正号的,例如 $c$。 $d[i][0]$ 表示前 $i$ 个元素中,以 $c$ 类元素结尾的最大价值。$d[i][1]$ 表示前 $i$ 个元素中,以 $b$ 类元素结尾的最大价值。有了这两个状态,那么状态转移方程就比较好想了。 // Date: Thu Dec 21 19:53:02 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 = 300010; int t, n, q, a[N]; ll d[N][2]; int main(void) { #ifdef _DEBUG freopen("1420c1.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> n >> q; memset(d, 0, sizeof d); For1(i, 1, n) { cin >> a[i]; d[i][0] = a[i]; } For1(i, 2, n) { d[i][1] = max(d[i - 1][0] - a[i], d[i - 1][1]); d[i][0] = max(d[i][0], max(d[i - 1][1] + a[i], d[i - 1][0])); } cout << max(d[n][0], d[n][1]) << '\n'; } return 0; }

December 21, 2023 · 3 min · 431 words

CodeForces 1807G1 Subsequence Addition (Easy Version)

Subsequence Addition (Easy Version) tutorial 需要注意到因为 $c_i$ 中元素不超过 $5000$,所以从 $a_i$ 中挑出的子序列的元素的和也不超过 $5000$。因为数组 $c$ 中的元素顺序没有关系,所以可以排序之后从小到大判断 $c_i$ 是否可行。设 $d_s$ 为当前的 $c$ 数组的子序列能够得到的和为 $s$,那么 $d_s = d_s | d_{s - c_i}$,每新加入一个 $c_i$,就用它去更新之前可行的 $d_s$。 这道题目有另外一个解法。观察可以发现如果 $c_i$ 小于当前数组 $c$ 的前缀和 $p[i - 1]$,那么 $c_i$ 一定是可行的,也就是 $c_1, c_2, \ldots, c_{j - 1}$ 的某个子序列的和可以组成 $c_i$。下面用数学归纳法证明。 假设对于长度为 $l$ 的数组 $c$ 上述结论成立:设数组 $c$ 的元素和是 $sum$,那么 $c$ 的子序列的和可以组成 $[1, sum]$ 中的所有数字。设 $1 \le x \le sum$,下面证明在数组 $c$ 中加入 $x$ 之后,数组 $c$ 的子序列的和可以组成 $[1, sum + x]$ 中的所有数字。只需要证明 $[sum + 1, sum + x]$ 中的数字可以被 $c_1, c_2, \ldots, c_l, c_{l + 1}$ 组成即可。 ...

December 20, 2023 · 3 min · 586 words

CodeForces 1443B Saving the City

Saving the City tutorial 设 $d[i][1]$ 表示第 $i$ 个位置是激活状态时的最小花费,$d[i][0]$ 表示第 $i$ 个位置是非激活状态时的最小花费,并且规定当 $s[i] = 1$ 的时候,只有 $d[i][1]$ 合法。设计好状态,转移方程就比较好想了。 题解中的贪心做法挺好的。因为对于一段连续的房子,只需要激活其中任何一个即可,所以一段连续的可以看成是一个房子,对于由 $0$ 分隔的相邻的两段连续的 $1$:$[l_1, r_1]$ 和 $[l_2, r_2]$,有两种选择: 用 $2a$ 的代价分别激活 先用 $(l_2 - r_1)\cdot b$ 的代价把两段连接起来,再用 $a$ 的代价激活 比较两种方式,线性扫描即可。 // Date: Mon Dec 18 22:39:07 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 = 100010; int t, n, d[N][2], a, b; string s; int main(void) { #ifdef _DEBUG freopen("1443b.in", "r", stdin); #endif std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { cin >> a >> b >> s; n = SZ(s); memset(d, 0x3f, sizeof d); int res = INF; if (s[0] == '0') { d[0][0] = 0; d[0][1] = a + b; } else { d[0][1] = a; } For(i, 1, n) { if (s[i] == '0') { if (s[i - 1] == '0') { d[i][0] = d[i - 1][0]; d[i][1] = d[i - 1][1] + b; } else { d[i][0] = d[i - 1][1]; d[i][1] = d[i - 1][1] + b; } } else { if (s[i - 1] == '0') { d[i][1] = min(d[i - 1][0] + a, d[i - 1][1]); } else { d[i][1] = d[i - 1][1]; } } } if (s[n - 1] == '1') res = d[n - 1][1]; else res = min(d[n - 1][0], d[n - 1][1]); cout << res << '\n'; } return 0; }

December 18, 2023 · 3 min · 537 words