More problems

This commit is contained in:
2026-03-19 16:00:45 -03:00
parent 14deae4d42
commit e9cafa367b
9 changed files with 1312 additions and 1 deletions

View File

@@ -0,0 +1,146 @@
/* Problem URL: https://codeforces.com/problemset/problem/2045/C */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
string s, t;
cin >> s >> t;
int in = -1;
int jn = -1;
int ans = oo;
vi pos(26, -1);
rep(i, t.size() - 1) {
pos[t[i] - 'a'] = i;
}
nrep(i, 1, s.size()) {
int cur = s[i] - 'a';
if (pos[cur] == -1) {
continue;
}
int c = i + t.size() - pos[cur] + 1;
if (c < ans) {
ans = c;
in = i;
jn = pos[cur];
}
}
if (ans == oo) {
cout << "-1\n";
return;
}
rep(i, in) {
cout << s[i];
}
nrep(j, jn, t.size()) {
cout << t[j];
}
cout << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,248 @@
/* Problem URL: https://codeforces.com/problemset/problem/2045/M */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
int r, c;
cin >> r >> c;
V<string> a(r);
cin >> a;
auto getpos = [&](int i, int j, int k) {
return i * c * 4 + j * 4 + k;
};
int tot = 0;
vvi graph(r * c * 4);
vi ty(r * c * 4, -1);
rep(i, r) {
rep(j, c) {
int n = getpos(i, j, 0);
int e = getpos(i, j, 1);
int s = getpos(i, j, 2);
int w = getpos(i, j, 3);
if (i > 0) {
int ot = getpos(i - 1, j, 2);
graph[n].push_back(ot);
}
if (j > 0) {
int ot = getpos(i, j - 1, 1);
graph[w].push_back(ot);
}
if (i < r - 1) {
int ot = getpos(i + 1, j, 0);
graph[s].push_back(ot);
}
if (j < c - 1) {
int ot = getpos(i, j + 1, 3);
graph[e].push_back(ot);
}
if (a[i][j] == '.') {
graph[e].push_back(w);
graph[w].push_back(e);
graph[s].push_back(n);
graph[n].push_back(s);
continue;
}
tot++;
ty[n] = i * c + j;
ty[e] = i * c + j;
ty[s] = i * c + j;
ty[w] = i * c + j;
if (a[i][j] == '/') {
graph[n].push_back(w);
graph[w].push_back(n);
graph[s].push_back(e);
graph[e].push_back(s);
continue;
}
graph[n].push_back(e);
graph[e].push_back(n);
graph[w].push_back(s);
graph[s].push_back(w);
}
}
V<bool> vis(r * c * 4);
vi sz(r * c * 4, -1);
set<int> s;
function<void(int)> dfs = [&](int i) {
vis[i] = true;
if (ty[i] != -1) {
s.insert(ty[i]);
}
repv(j, graph[i]) {
if (vis[j]) {
continue;
}
dfs(j);
}
};
function<void(int)> dfs2 = [&](int i) {
sz[i] = s.size();
repv(j, graph[i]) {
if (sz[j] != -1) {
continue;
}
dfs2(j);
}
};
rep(i, r) {
rep(j, c) {
rep(k, 4) {
int ac = getpos(i, j, k);
if (vis[ac]) {
continue;
}
dfs(ac);
dfs2(ac);
s.clear();
}
}
}
V<string> ans;
rep(i, r) {
int w = getpos(i, 0, 3);
if (sz[w] == tot) {
ans.emplace_back("W" + to_string(i + 1));
}
int e = getpos(i, c - 1, 1);
if (sz[e] == tot) {
ans.emplace_back("E" + to_string(i + 1));
}
}
rep(i, c) {
int n = getpos(0, i, 0);
if (sz[n] == tot) {
ans.emplace_back("N" + to_string(i + 1));
}
int s = getpos(r - 1, i, 2);
if (sz[s] == tot) {
ans.emplace_back("S" + to_string(i + 1));
}
}
cout << ans.size() << '\n';
repv(i, ans) {
cout << i << ' ';
}
cout << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,118 @@
/* Problem URL: https://codeforces.com/problemset/problem/2181/F */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
int c = 0;
bool t = false;
rep(i, n) {
c += a[i] == 1;
t = t || a[i] != 1;
}
cout << (((c & 1) && !t) || ((~c & 1) && t) ? "Alice\n" : "Bob\n");
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,138 @@
/* Problem URL: https://codeforces.com/problemset/problem/2165/B */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
cin >> n;
ll mod = 998244353;
vl c(n);
ll ma = 0;
rep(i, n) {
int a;
cin >> a;
c[a - 1]++;
rmax(ma, c[a - 1]);
}
vl dp(n + 1);
dp[0] = 1;
rep(i, n) {
if (c[i] == 0) {
continue;
}
for (int j = n; j >= c[i]; j--) {
(dp[j] += dp[j - c[i]] * c[i]) %= mod;
}
}
ll ans = 0;
nrep(i, ma, n + 1) {
(ans += dp[i]) %= mod;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,167 @@
/* Problem URL: https://codeforces.com/problemset/problem/2000/E */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
int n, m, k;
cin >> n >> m >> k;
int w;
cin >> w;
vl a(w);
cin >> a;
vvl c(n, vl(m));
rep(i, n - k + 1) {
rep(j, m - k + 1) {
c[i][j]++;
int id = i + k;
int jd = j + k;
if (id < n) {
c[id][j]--;
}
if (jd < m) {
c[i][jd]--;
}
if (id < n && jd < m) {
c[id][jd]++;
}
}
}
rep(i, n) {
rep(j, m) {
if (i > 0) {
c[i][j] += c[i - 1][j];
}
if (j > 0) {
c[i][j] += c[i][j - 1];
}
if (i > 0 && j > 0) {
c[i][j] -= c[i - 1][j - 1];
}
}
}
vi t;
rep(i, n) {
rep(j, m) {
t.push_back(c[i][j]);
}
}
sort(all(t), greater<>());
sort(all(a), greater<>());
ll ans = 0;
rep(i, w) {
ans += a[i] * t[i];
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,156 @@
/* Problem URL: https://codeforces.com/problemset/problem/2005/C */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
bool inv[256];
void pre()
{
inv['n'] = true;
inv['a'] = true;
inv['r'] = true;
inv['e'] = true;
inv['k'] = true;
}
#define TEST 1
string fd = "narek";
void solve()
{
int n, m;
cin >> n >> m;
V<string> a(n);
cin >> a;
vvl memo(n, vl(5, -OO));
function<ll(int, int)> dp = [&](int i, int f) {
if (i >= n) {
return (ll)-f;
}
ll &ans = memo[i][f];
if (ans != -OO) {
return ans;
}
int pr = f;
ans = 0;
repv(j, a[i]) {
if (j == fd[f]) {
ans += 5 * (f == 4);
f = f + 1 - 5 * (f == 4);
continue;
}
ans -= inv[j];
}
ll tmp = -OO;
rmax(tmp, dp(i + 1, f));
return ans = max({ans + tmp, ans - f, dp(i + 1, pr)});
};
ll ans = 0;
rep(i, n) {
rmax(ans, dp(i, 0));
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,180 @@
/* Problem URL: https://codeforces.com/problemset/problem/2036/E */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
int n, m, q;
cin >> n >> m >> q;
vvl a(n, vl(m));
cin >> a;
rep(j, m) {
nrep(i, 1, n) {
a[i][j] |= a[i - 1][j];
}
}
while (q--) {
int l = 0;
int r = n - 1;
int q;
cin >> q;
while (q--) {
int p;
char op;
ll c;
cin >> p >> op >> c;
p--;
auto findl = [&]() {
int low = l;
int high = n - 1;
int ans = n;
while (low <= high) {
int mid = (low + high) >> 1;
if (a[mid][p] > c) {
ans = mid;
high = mid - 1;
continue;
}
low = mid + 1;
}
return ans;
};
auto findr = [&]() {
int low = 0;
int high = r;
int ans = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (a[mid][p] < c) {
ans = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
return ans;
};
if (op == '>') {
l = findl();
} else {
r = findr();
}
}
if (l > r) {
cout << "-1\n";
continue;
}
cout << l + 1 << '\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,147 @@
/* Problem URL: https://codeforces.com/problemset/problem/1796/D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
constexpr int MAXN = 2e5 + 10;
constexpr int MAXK = 30;
ll memo[MAXN][MAXK][4];
void solve()
{
int n, k;
ll x;
cin >> n >> k >> x;
vl a(n);
cin >> a;
ll otinf = -OO / 4;
vvvl memo(n, vvl(k + 1, vl(3, -OO)));
function<ll(int, int, int)> dp = [&](int i, int j, int s) {
if (j > k) {
return otinf;
}
if (i >= n) {
return j == k ? 0LL : otinf;
}
ll &ans = memo[i][j][s];
if (ans != -OO) {
return ans;
}
if (s == 0) {
return ans = max({dp(i + 1, j, 0), dp(i + 1, j + 1, 0),
dp(i + 1, j + 1, 1) + a[i] + x, dp(i + 1, j, 1) + a[i] - x});
}
if (s == 1) {
return ans = max({dp(i + 1, j, 1) + a[i] - x, dp(i + 1, j + 1, 1) + a[i] + x,
dp(i + 1, j, 2), dp(i + 1, j + 1, 2)});
}
return ans = max(dp(i + 1, j + 1, 2), dp(i + 1, j, 2));
};
cout << dp(0, 0, 0) << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -97,7 +97,18 @@ A little collection of interesting problems that I randomly decided to do.
- [D. Jellyfish and Mex](https://codeforces.com/problemset/problem/1875/D)
An interesting problem to thinkm about.
An interesting problem to think about.
- [C. Lazy Narek](https://codeforces.com/problemset/problem/2005/C)
I just liked the problem, not particularly hard.
- [D. Maximum Subarray](https://codeforces.com/problemset/problem/1796/D)
Interesting to think about the transitions and the states, although figuring out
it's a dp problem is already a challenge by itself.
## Graph