Compare commits
3 Commits
ccd62bc6e9
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e9cafa367b | |||
| 14deae4d42 | |||
| 07afa2b2a8 |
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
138
Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp
Normal file
138
Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
155
Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp
Normal file
155
Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2185/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, q;
|
||||||
|
cin >> n >> q;
|
||||||
|
int sz = 1 << n;
|
||||||
|
|
||||||
|
vl a(sz);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vl an(sz * 2);
|
||||||
|
|
||||||
|
function<ll(int)> calc = [&](int i) {
|
||||||
|
if (i >= sz) {
|
||||||
|
an[i] = a[i - sz];
|
||||||
|
return an[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
ll a1 = calc(i * 2);
|
||||||
|
ll a2 = calc(i * 2 + 1);
|
||||||
|
|
||||||
|
an[i] = a1 ^ a2;
|
||||||
|
return an[i];
|
||||||
|
};
|
||||||
|
|
||||||
|
calc(1);
|
||||||
|
|
||||||
|
while (q--) {
|
||||||
|
int b, c;
|
||||||
|
cin >> b >> c;
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
ll cur = a[b - 1];
|
||||||
|
bool add = false;
|
||||||
|
int st = 0;
|
||||||
|
b += sz;
|
||||||
|
b--;
|
||||||
|
|
||||||
|
while (b > 1) {
|
||||||
|
int now = b & 1;
|
||||||
|
b >>= 1;
|
||||||
|
|
||||||
|
ll cur = an[(b << 1) + (now ^ 1)];
|
||||||
|
if (cur > c || (cur == c && now)) {
|
||||||
|
ans += 1 << st;
|
||||||
|
}
|
||||||
|
|
||||||
|
st++;
|
||||||
|
c ^= cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
137
Codeforces Round 957 (Div. 3)/F. Valuable Cards.cpp
Normal file
137
Codeforces Round 957 (Div. 3)/F. Valuable Cards.cpp
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1992/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, x;
|
||||||
|
cin >> n >> x;
|
||||||
|
|
||||||
|
vi a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
set<int> ev;
|
||||||
|
ev.insert(x);
|
||||||
|
|
||||||
|
int ans = 1;
|
||||||
|
rep(i, n) {
|
||||||
|
set<int> tmp = ev;
|
||||||
|
repv(j, ev) {
|
||||||
|
if (j % a[i] == 0) {
|
||||||
|
tmp.insert(j / a[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tmp.count(1)) {
|
||||||
|
ans++;
|
||||||
|
ev.clear();
|
||||||
|
ev.insert(x);
|
||||||
|
if (x % a[i] == 0) {
|
||||||
|
ev.insert(x / a[i]);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
swap(ev, tmp);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
167
Codeforces Round 966 (Div. 3)/E. Photoshoot for Gorillas.cpp
Normal file
167
Codeforces Round 966 (Div. 3)/E. Photoshoot for Gorillas.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
156
Codeforces Round 972 (Div. 2)/C. Lazy Narek.cpp
Normal file
156
Codeforces Round 972 (Div. 2)/C. Lazy Narek.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,139 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2020/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 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
ll b, c, d;
|
||||||
|
cin >> b >> c >> d;
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
rep(i, 61) {
|
||||||
|
int bi = (b >> i) & 1;
|
||||||
|
int ci = (c >> i) & 1;
|
||||||
|
ll di = (d >> i) & 1;
|
||||||
|
|
||||||
|
if (bi && ci) {
|
||||||
|
ans |= (di^1) << i;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bi && !ci) {
|
||||||
|
if (!di) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!bi && ci) {
|
||||||
|
if (di) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ans |= di << 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
180
Codeforces Round 984 (Div. 3)/E. Reverse the Rivers.cpp
Normal file
180
Codeforces Round 984 (Div. 3)/E. Reverse the Rivers.cpp
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
160
Hello 2023/C. Least Prefix Sum.cpp
Normal file
160
Hello 2023/C. Least Prefix Sum.cpp
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1779/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 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
m--;
|
||||||
|
|
||||||
|
int ans = 0;
|
||||||
|
vl pref(n + 1);
|
||||||
|
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
pref[i] = pref[i - 1] + a[i - 1];
|
||||||
|
}
|
||||||
|
pref[0] = OO;
|
||||||
|
|
||||||
|
priority_queue<pair<ll, int>> pq;
|
||||||
|
|
||||||
|
ll cur = pref[m + 1];
|
||||||
|
for (int i = m; i >= 0; i--) {
|
||||||
|
pq.emplace(a[i], i);
|
||||||
|
|
||||||
|
while (cur > pref[i]) {
|
||||||
|
auto [c, j] = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
a[j] = -a[j];
|
||||||
|
cur -= c * 2;
|
||||||
|
ans++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!pq.empty()) {
|
||||||
|
pq.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
pref[0] = 0;
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
pref[i] = pref[i - 1] + a[i - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
priority_queue<ll, vl, greater<>> mpq;
|
||||||
|
|
||||||
|
cur = pref[m + 1];
|
||||||
|
nrep(i, m + 1, n) {
|
||||||
|
cur += a[i];
|
||||||
|
mpq.push(a[i]);
|
||||||
|
|
||||||
|
while (cur < pref[m + 1]) {
|
||||||
|
cur -= mpq.top() * 2;
|
||||||
|
mpq.pop();
|
||||||
|
ans++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
- [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
|
## Graph
|
||||||
|
|
||||||
@@ -124,3 +135,10 @@ A little collection of interesting problems that I randomly decided to do.
|
|||||||
- [F. Non-academic Problem](https://codeforces.com/problemset/problem/1986/F)
|
- [F. Non-academic Problem](https://codeforces.com/problemset/problem/1986/F)
|
||||||
|
|
||||||
Bridge to compress the graph into a tree.
|
Bridge to compress the graph into a tree.
|
||||||
|
|
||||||
|
## Divide and Conquer
|
||||||
|
|
||||||
|
- [F. BattleCows](https://codeforces.com/problemset/problem/2185/F)
|
||||||
|
|
||||||
|
Interesting problem to think about, good introduction to divide and conquer in my opinion and
|
||||||
|
can be used as a bridge to segment trees.
|
||||||
|
|||||||
Reference in New Issue
Block a user