Add some more problems and update TODO list

This commit is contained in:
2025-10-30 17:11:49 -03:00
parent cf5a83fb09
commit 97ebdbf890
54 changed files with 8355 additions and 36 deletions

View File

@@ -0,0 +1,167 @@
/* Problem URL: https://codeforces.com/problemset/problem/2052/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;
array<string, 2> a;
cin >> a[0] >> a[1];
vvi memo(n, vi(4, -1));
function<int(int, int)> dp = [&](int i, int mask) -> int {
if (i >= n) {
return mask == 0;
}
int &ans = memo[i][mask];
if (ans != -1) {
return ans;
}
ans = 0;
int bit1 = mask & 1;
int bit2 = (mask >> 1) & 1;
if ((bit1 && a[0][i] == '#') || (bit2 && a[1][i] == '#')) {
return 0;
}
if (mask == 0 && a[0][i] == '.' && a[1][i] == '.') {
ans = dp(i + 1, 0) + dp(i + 1, 3);
rmin(ans, 2);
} else {
int now = 0;
if (!bit1 && a[0][i] == '.') {
now |= 1;
}
if (!bit2 && a[1][i] == '.') {
now |= 2;
}
ans += dp(i + 1, now);
rmin(ans, 2);
}
return ans;
};
int ans = dp(0, 0);
if (ans == 0) {
cout << "None\n";
return;
}
if (ans == 1) {
cout << "Unique\n";
return;
}
if (ans == 2) {
cout << "Multiple\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,221 @@
/* Problem URL: https://codeforces.com/problemset/problem/1896/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
void solve()
{
int n, q;
cin >> n >> q;
vi a(n);
cin >> a;
while (__builtin_popcount(n) != 1) {
n++;
a.push_back(0);
}
set<int> ones;
rep(i, n) {
if (a[i] == 1) {
ones.insert(i);
}
}
vi seg(n << 1);
nrep(i, n, n << 1) {
seg[i] = a[i - n];
}
for (int i = n - 1; i > 0; i--) {
seg[i] = seg[i * 2] + seg[i * 2 + 1];
}
auto update = [&](int i, int v) {
i += n;
seg[i] = v;
for (i >>= 1; i > 0; i >>= 1) {
seg[i] = seg[i * 2] + seg[i * 2 + 1];
}
};
function<int(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) {
if (l > tr || r < tl) {
return 0;
}
if (l >= tl && r <= tr) {
return seg[i];
}
int mid = (l + r) >> 1;
return query(i * 2, l, mid, tl, tr) + query(i * 2 + 1, mid + 1, r, tl, tr);
};
int l1 = -1;
int r1 = -1;
if (!ones.empty()) {
l1 = *ones.begin();
r1 = *prev(ones.end());
}
while (q--) {
int op;
cin >> op;
if (op == 2) {
int i, v;
cin >> i >> v;
i--;
if (a[i] == 1) {
ones.erase(i);
}
a[i] = v;
update(i, v);
if (v == 1) {
ones.insert(i);
}
if (!ones.empty()) {
l1 = *ones.begin();
r1 = *prev(ones.end());
} else {
l1 = -1;
r1 = -1;
}
continue;
}
int s;
cin >> s;
if (s & 1) {
if (l1 == -1) {
cout << "NO\n";
continue;
}
if (ones.size() & 1) {
cout << (query(1, 0, n - 1, 0, n - 1) >= s ? "YES\n" : "NO\n");
continue;
}
int ans = max(query(1, 0, n - 1, 0, r1 - 1), query(1, 0, n - 1, l1 + 1, n - 1));
cout << (ans >= s ? "YES\n" : "NO\n");
continue;
}
if (ones.size() & 1) {
int ans = max(query(1, 0, n - 1, 0, r1 - 1), query(1, 0, n - 1, l1 + 1, n - 1));
cout << (ans >= s ? "YES\n" : "NO\n");
continue;
}
cout << (query(1, 0, n - 1, 0, n - 1) >= s ? "YES\n" : "NO\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,162 @@
/* Problem URL: https://codeforces.com/problemset/problem/2089/A */
#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;
const int MAXN = 2e5 + 1;
bool prime[MAXN];
void pre()
{
fill(prime, prime + MAXN, true);
prime[1] = false;
prime[0] = false;
nrep(i, 2, MAXN) {
if (!prime[i]) {
continue;
}
for (int j = i << 1; j < MAXN; j += i) {
prime[j] = false;
}
}
}
#define TEST 1
void solve()
{
int n;
cin >> n;
// int n = 7;
// int cur = 3;
// nrep(i, 3, n + 1) {
// cur += i;
// cout << (cur + i - 1) / i << '\n';
// }
int l = n / 3;
int choice = l;
while (!prime[choice]) {
choice++;
}
int rm = 0;
int now = 1;
vi ans(n);
set<int> act;
rep(i, n) {
act.insert(i + 1);
}
rep(i, l) {
ans[i] = choice + rm * now;
act.erase(ans[i]);
if (now == 1) {
now = -1;
rm++;
} else {
now = 1;
}
}
nrep(i, l, n) {
ans[i] = *act.begin();
act.erase(act.begin());
}
cout << ans;
}
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,175 @@
/* Problem URL: https://codeforces.com/problemset/problem/2117/G */
#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;
V<tuple<ll, int, int>> edges(m);
for (auto &[c, u, v] : edges) {
cin >> u >> v >> c;
u--, v--;
}
sortv(edges);
vi dsu(n);
rep(i, n) {
dsu[i] = i;
}
function<int(int)> find_p = [&](int i) {
if (dsu[i] == i) {
return i;
}
return dsu[i] = find_p(dsu[i]);
};
auto join = [&](int a, int b) {
a = find_p(a);
b = find_p(b);
dsu[b] = a;
return a != b;
};
V<V<pair<int, ll>>> graph(n);
for (auto [c, u, v] : edges) {
if (join(u, v)) {
graph[u].emplace_back(v, c);
graph[v].emplace_back(u, c);
}
}
V<pair<ll, ll>> every;
every.reserve(n);
ll mians = OO;
ll maans = 0;
function<void(int, int, ll, ll)> dfs = [&](int i, int p, ll mi, ll ma) {
if (i == n - 1) {
mians = mi;
maans = ma;
}
every.emplace_back(mi, ma);
for (auto [j, c] : graph[i]) {
if (j == p) {
continue;
}
dfs(j, i, min(mi, c), max(ma, c));
}
};
dfs(0, 0, OO, 0);
ll ans = OO;
for (auto [mi, ma] : every) {
rmin(ans, min(mians, mi) + max(maans, ma));
}
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,148 @@
/* Problem URL: https://codeforces.com/problemset/problem/2138/C1 */
#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, k;
cin >> n >> k;
vi depth(n);
vi sums(n);
V<bool> leaf(n, true);
sums[0] = 1;
nrep(i, 1, n) {
int a;
cin >> a;
a--;
depth[i] = depth[a] + 1;
sums[depth[i]]++;
leaf[a] = false;
}
int maxdepth = oo;
rep(i, n) {
if (leaf[i]) {
rmin(maxdepth, depth[i]);
}
}
vi dp(n + 1);
dp[0] = 1;
rep(i, maxdepth) {
for (int j = n; j >= sums[i]; j--) {
dp[j] |= dp[j - sums[i]];
}
}
rep(i, n + 1) {
if (dp[i] && i <= k && n - i <= n - k) {
cout << maxdepth + 1 << '\n';
return;
}
}
cout << maxdepth << '\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,114 @@
/* Problem URL: https://codeforces.com/contest/2154/problem/A */
#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;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n, k;
cin >> n >> k;
string s;
cin >> s;
V<bool> fds(n);
int ans = 0;
rep(i, n) {
if (s[i] != '1') {
continue;
}
bool valid = true;
for (int j = i - 1; j >= 0 && i - j + 1 <= k; j--) {
if (s[j] == '1') {
valid = false;
break;
}
}
ans += valid;
}
cout << ans << '\n';
}
}

View File

@@ -0,0 +1,117 @@
/* Problem URL: https://codeforces.com/contest/2154/problem/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;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vl a(n);
cin >> a;
ll maximal = 0;
rep(i, n) {
rmax(maximal, a[i]);
if (i & 1) {
a[i] = maximal;
}
}
int ans = 0;
for (int i = 1; i < n; i += 2) {
if (a[i] <= a[i - 1]) {
int diff = a[i - 1] - a[i] + 1;
ans += diff;
}
if (i < n - 1 && a[i] <= a[i + 1]) {
int diff = a[i + 1] - a[i] + 1;
ans += diff;
a[i + 1] -= diff;
}
}
cout << ans << '\n';
}
}

View File

@@ -0,0 +1,168 @@
/* Problem URL: https://codeforces.com/contest/2154/problem/C1 */
#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;
}
int divs[(int)2e5 + 2];
void pre()
{
fill(divs, divs + (int)2e5 + 2, 1);
nrep(i, 2, 2e5 + 2) {
if (divs[i] != 1) {
continue;
}
for (int j = i; j <= (ll)2e5 + 1; j += i) {
divs[j] = i;
}
}
}
void solve()
{
int n;
cin >> n;
vi a(n);
vi b(n);
cin >> a >> b;
set<int> primes;
auto getprimes = [&](int now) {
while (divs[now] != 1) {
if (primes.count(divs[now])) {
return true;
}
primes.insert(divs[now]);
int prev = divs[now];
while (divs[now] == prev) {
now /= divs[now];
}
}
return false;
};
auto checkprimes = [&](int now) {
while (divs[now] != 1) {
if (primes.count(divs[now])) {
return true;
}
int prev = divs[now];
while (divs[now] == prev) {
now /= divs[now];
}
}
return false;
};
rep(i, n) {
if (getprimes(a[i])) {
cout << "0\n";
return;
}
}
int ans = 2;
rep(i, n) {
if (checkprimes(a[i] + 1)) {
rmin(ans, 1);
break;
}
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t = 1;
cin >> t;
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,193 @@
/* Problem URL: https://codeforces.com/contest/2154/problem/C2 */
#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;
const int maxn = 2e5 + 2;
int divs[maxn];
void pre()
{
fill(divs, divs + maxn, 1);
nrep(i, 2, maxn) {
if (divs[i] != 1) {
continue;
}
for (int j = i; j < maxn; j += i) {
divs[j] = i;
}
}
}
#define TEST 1
void solve()
{
int n;
cin >> n;
V<pair<ll, int>> a(n);
repv(i, a) {
cin >> i.second;
}
repv(i, a) {
cin >> i.first;
}
set<int> primes;
auto getprimes = [&](int now) {
while (divs[now] != 1) {
if (primes.count(divs[now])) {
return true;
}
primes.insert(divs[now]);
int prev = divs[now];
while (divs[now] == prev) {
now /= divs[now];
}
}
return false;
};
auto findprimes = [&](int now) {
while (divs[now] != 1) {
if (primes.count(divs[now])) {
return true;
}
int prev = divs[now];
while (divs[now] == prev) {
now /= divs[now];
}
}
return false;
};
rep(i, n) {
if (getprimes(a[i].second)) {
cout << "0\n";
return;
}
}
sortv(a);
ll ans = a[0].first + a[1].first;
rep(i, n) {
if (findprimes(a[i].second + 1)) {
rmin(ans, a[i].first);
}
}
if (primes.empty()) {
cout << ans << '\n';
return;
}
repv(i, primes) {
if (a[0].second % i == 0) {
continue;
}
rmin(ans, (i - a[0].second % i) * a[0].first);
}
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,171 @@
/* Problem URL: https://codeforces.com/contest/2154/problem/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
void solve()
{
int n;
cin >> n;
vvi graph(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
vi color(n);
vi dis(n);
function<void(int, int)> dfs = [&](int i, int p) {
repv(j, graph[i]) {
if (j == p) {
continue;
}
dis[j] = dis[i] + 1;
color[j] = color[i] ^ 1;
dfs(j, i);
}
};
dfs(n - 1, n - 1);
int now = color[0];
V<pair<int, int>> edges;
rep(i, n - 1) {
edges.emplace_back(dis[i], i);
}
sort(all(edges), greater<>());
V<pair<int, int>> ops;
rep(i, n - 1) {
int j = edges[i].second;
if (color[j] == now) {
ops.emplace_back(1, 0);
now ^= 1;
}
ops.emplace_back(2, j + 1);
ops.emplace_back(1, 0);
now ^= 1;
}
cout << ops.size() << '\n';
repv(i, ops) {
cout << i.first;
if (i.first == 2) {
cout << ' ' << i.second;
}
cout << '\n';
}
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,115 @@
/* Problem URL: https://codeforces.com/contest/2156/problem/A */
#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 n;
cin >> n;
ll ans = 0;
while (n > 2) {
ans += n / 3;
n = n / 3 + n % 3;
}
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,141 @@
/* Problem URL: https://codeforces.com/contest/2156/problem/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, q;
cin >> n >> q;
string a;
cin >> a;
bool pos = true;
rep(i, n) {
if (a[i] == 'B') {
pos = false;
break;
}
}
while (q--) {
ll x;
cin >> x;
if (pos) {
cout << x << '\n';
continue;
}
int now = 0;
while (x > 0) {
if (a[now % n] == 'A') {
x--;
now++;
continue;
}
now++;
x /= 2;
}
cout << now << '\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/contest/2156/problem/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;
const int MAXN = 2e5 + 1;
int lim[MAXN];
void pre()
{
nrep(i, 1, MAXN) {
int low = 1;
int high = i / 3;
int ans = 0;
while (low <= high) {
int mid = (low + high) >> 1;
int cur = i - mid * 2;
int dis = cur % mid;
if (cur - dis >= mid + dis) {
ans = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
lim[i] = ans;
}
}
#define TEST 1
void solve()
{
int n, k;
cin >> n >> k;
vi a(n);
cin >> a;
vi count(n + 1);
auto getdivs = [&](int now) {
for (int i = 1; i * i <= now; i++) {
if (now % i == 0) {
count[i]++;
if (i * i != now) {
count[now / i]++;
}
}
}
};
vvi rem(n + 2);
rep(i, n) {
rem[lim[a[i]] + 1].push_back(a[i]);
}
int ans = 1;
int c = 0;
nrep(i, 1, n + 1) {
repv(j, rem[i]) {
getdivs(j);
c++;
}
int cur = c - count[i];
if (cur <= k) {
ans = 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,159 @@
/* Problem URL: https://codeforces.com/contest/2156/problem/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
void solve()
{
int n;
cin >> n;
int log = 32 - __builtin_clz(n);
set<int> cur;
rep(i, n - 1) {
cur.insert(i + 1);
}
set<int> all;
rep(i, n) {
all.insert(i + 1);
}
rep(i, log) {
vi ans(cur.size());
array<int, 2> val = {};
auto it = cur.begin();
rep(j, ans.size()) {
cout << "? " << *it << ' ' << (1 << i) << endl;
cin >> ans[j];
val[ans[j]]++;
it++;
}
array<int, 2> act = {};
repv(j, all) {
act[((j >> i) & 1)]++;
}
int tmp = act[1] != val[1];
auto itr = all.begin();
while (itr != all.end()) {
if (((*itr >> i) & 1) != tmp) {
itr = all.erase(itr);
} else {
itr++;
}
}
it = cur.begin();
rep(i, ans.size()) {
if (ans[i] == tmp) {
it++;
} else {
it = cur.erase(it);
}
}
}
cout << "! " << *all.begin() << endl;
}
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,132 @@
/* Problem URL: https://codeforces.com/contest/2167/problem/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;
vi primes;
void pre()
{
V<bool> prime(2e5 + 1);
prime[1] = true;
nrep(i, 2, 200001) {
if (prime[i]) {
continue;
}
primes.push_back(i);
for (int j = i * 2; j <= 2e5; j += i) {
prime[j] = true;
}
}
}
#define TEST 1
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
repv(i, primes) {
rep(j, n) {
if (a[j] % i != 0) {
cout << i << '\n';
return;
}
}
}
}
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,187 @@
/* Problem URL: https://codeforces.com/contest/2167/problem/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, k;
ll x;
cin >> n >> k >> x;
vl a(n);
cin >> a;
sortv(a);
a.erase(unique(all(a)), a.end());
if (x - a.size() + 1 < k) {
rep(i, k) {
cout << i << ' ';
}
cout << '\n';
return;
}
priority_queue<tuple<ll, ll, ll, ll, ll, int>> pq;
if (a[0] != 0) {
pq.emplace(a[0], 0, 0, -1e9, a[0], -1);
}
if (a.back() != x) {
pq.emplace(x - a.back(), x, x, a.back(), 2e9, -2);
}
nrep(i, 1, a.size()) {
if (a[i] == a[i - 1] - 1) {
continue;
}
ll med = (a[i] + a[i - 1]) >> 1;
ll dis = min(a[i] - med, med - a[i - 1]);
pq.emplace(dis, med, med, a[i - 1], a[i], i);
}
set<tuple<int, ll, ll>> ranges;
while (k--) {
auto [d, l, r, lim1, lim2, id] = pq.top();
pq.pop();
if (l < 0 || l <= lim1 || r >= lim2 || r > x) {
k++;
continue;
}
auto itr = ranges.lower_bound({id, -1, -1});
if (itr != ranges.end() && get<0>(*itr) == id) {
ranges.erase(itr);
}
ranges.emplace(id, l, r);
ll l1 = l - 1;
ll r1 = r;
ll l2 = l;
ll r2 = r + 1;
ll dis1 = min(l1 - lim1, lim2 - r1);
ll dis2 = min(l2 - lim1, lim2 - r2);
if (l1 < max(0LL, lim1 + 1) || r1 > min(x, lim2 - 1)) {
dis1 = -1;
}
if (l2 < max(0LL, lim1 + 1) || r2 > min(x, lim2 - 1)) {
dis2 = -1;
}
if (dis1 >= dis2) {
pq.emplace(dis1, l1, r1, lim1, lim2, id);
continue;
}
pq.emplace(dis2, l2, r2, lim1, lim2, id);
}
repv(i, ranges) {
auto [id, lim1, lim2] = i;
nrep(j, lim1, lim2 + 1) {
cout << 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,167 @@
/* Problem URL: https://codeforces.com/contest/2167/problem/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, k;
cin >> n >> k;
vvi graph(n);
rep(i, n - 1) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
ll ans = n;
vl dp(n);
function<ll(int, int)> dfs = [&](int i, int p) {
dp[i] = 1;
repv(j, graph[i]) {
if (j == p) {
continue;
}
dp[i] += dfs(j, i);
}
return dp[i];
};
dfs(0, 0);
function<void(int, int)> reroot = [&](int i, int p) {
repv(j, graph[i]) {
ll now = n - dp[j];
if (now < k) {
continue;
}
ans += dp[j];
}
ll prev = dp[i];
repv(j, graph[i]) {
if (j == p) {
continue;
}
dp[i] = n - dp[j];
reroot(j, i);
}
dp[i] = prev;
return;
};
reroot(0, 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,156 @@
/* Problem URL: https://codeforces.com/contest/2167/problem/G */
#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) {
if (i.empty()) {
cout << '\n';
continue;
}
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);
vl c(n);
cin >> a >> c;
map<ll, int> var;
repv(i, a) {
var[i] = 0;
}
int v = 0;
repv(i, var) {
i.second = v;
v++;
}
repv(i, a) {
i = var[i];
}
var.clear();
vvl dp(2, vl(n));
int now = 0;
rep(i, n) {
int prev = now^1;
ll mi = OO;
rep(j, a[i]) {
rmin(mi, dp[prev][j]);
dp[now][j] = mi + c[i];
}
rmin(mi, dp[prev][a[i]]);
dp[now][a[i]] = mi;
nrep(j, a[i] + 1, n) {
rmin(mi, dp[prev][j]);
dp[now][j] = mi + c[i];
}
now ^= 1;
}
cout << *min_element(all(dp[now^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,190 @@
/* Problem URL: https://codeforces.com/problemset/problem/1747/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 0
void solve()
{
int n, q;
cin >> n >> q;
vi a(n);
cin >> a;
vi pref(n + 1);
vi zero(n + 1);
unordered_map<int, vi> var;
unordered_map<int, vi> odd;
unordered_map<int, vi> even;
rep(i, n) {
pref[i + 1] = pref[i] ^ a[i];
zero[i + 1] = zero[i] + (a[i] == 0);
var[pref[i + 1]].push_back(i);
if (i & 1) {
odd[pref[i + 1]].push_back(i);
} else {
even[pref[i + 1]].push_back(i);
}
}
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
if (zero[r + 1] - zero[l] == r - l + 1) {
cout << "0\n";
continue;
}
if (r - l <= 1) {
cout << "-1\n";
continue;
}
int xo = pref[r + 1] ^ pref[l];
if (xo != 0) {
cout << "-1\n";
continue;
}
if ((r - l + 1) & 1) {
cout << "1\n";
continue;
}
auto fds = var.find(pref[r + 1]);
if (fds == var.end()) {
cout << "-1\n";
}
vi &tmp = fds->second;
auto one = lower_bound(all(tmp), l);
auto two = lower_bound(all(tmp), r);
if (two != tmp.begin()) {
two--;
}
if ((one != tmp.end() && *one == l) || (two != tmp.end() && *two == r - 1)) {
cout << "1\n";
continue;
}
if (r & 1) {
vi &act = even[pref[r + 1]];
auto itr = lower_bound(all(act), l);
if (itr == act.end() || *itr > r) {
cout << "-1\n";
continue;
}
} else {
vi &act = odd[pref[r + 1]];
auto itr = lower_bound(all(act), l);
if (itr == act.end() || *itr > r) {
cout << "-1\n";
continue;
}
}
cout << "2\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,130 @@
/* Problem URL: https://codeforces.com/problemset/problem/1772/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
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
ll l = 0;
ll r = 1e9;
nrep(i, 1, n) {
if (a[i] < a[i - 1]) {
rmax(l, (a[i] + a[i - 1] + 1) >> 1);
}
if (a[i] > a[i - 1]) {
rmin(r, (a[i] + a[i - 1]) >> 1);
}
}
if (l > r) {
cout << "-1\n";
return;
}
cout << l << '\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,161 @@
/* Problem URL: https://codeforces.com/problemset/problem/1775/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 n, x;
cin >> n >> x;
auto get = [&](ll l, ll r) {
ll step = 1;
while (l != r) {
l >>= 1;
r >>= 1;
step <<= 1;
}
return step * l;
};
ll low = n;
ll high = 5e18;
ll ans = -1;
while (low <= high) {
ll mid = (high - low) / 2 + low;
ll tmp = get(n, mid);
auto test = [&]() {
rep(i, 63) {
ll bit1 = (tmp >> i) & 1;
ll bit2 = (x >> i) & 1;
if (!bit1 && bit2) {
return 2;
}
if (bit1 && !bit2) {
return 1;
}
}
return 0;
};
int g = test();
if (!g) {
ans = mid;
high = mid - 1;
continue;
}
if (g == 1) {
low = mid + 1;
continue;
}
high = mid - 1;
}
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,155 @@
/* Problem URL: https://codeforces.com/problemset/problem/1799/D1 */
#include <bits/stdc++.h>
#include <algorithm>
#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, k;
cin >> n >> k;
vi a(n);
repv(i, a) {
cin >> i;
i--;
}
V<pair<ll, ll>> prog(k);
repv(j, prog) {
cin >> j.first;
}
repv(j, prog) {
cin >> j.second;
}
vi prev(n, -1);
vi pos(k, -1);
vl pref(n + 2);
pref[1] = prog[a[0]].first;
nrep(i, 1, n) {
pref[i + 1] = pref[i] + (a[i] == a[i - 1] ? prog[a[i]].second : prog[a[i]].first);
}
pref[n + 1] = pref[n];
vl dp(n + 1, 0);
dp[0] = prog[a[0]].first;
nrep(i, 1, n) {
prev[i] = pos[a[i]];
pos[a[i]] = i;
if (prev[i] == -1) {
dp[i] = dp[i - 1] + prog[a[i]].first;
continue;
}
if (prev[i] == i - 1) {
dp[i] = dp[i - 1] + prog[a[i]].second;
}
ll cur = dp[prev[i] + 1] + prog[a[i]].second + pref[i] - pref[prev[i] + 2];
dp[i] = min(dp[i - 1] + prog[a[i]].first, cur);
}
cout << dp[n - 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,149 @@
/* Problem URL: https://codeforces.com/problemset/problem/1824/A */
#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;
vi a(n);
cin >> a;
vi pref(m + 2);
vi empty(m + 1);
int right = 0;
int left = 0;
repv(i, a) {
if (i == -1) {
left++;
continue;
}
if (i == -2) {
right++;
continue;
}
pref[i] = 1;
}
rep(i, m + 1) {
pref[i + 1] += pref[i];
}
int ans = min(max(pref.back() + right, pref.back() + left), m);
nrep(i, 1, m + 1) {
if (pref[i] == pref[i - 1]) {
continue;
}
int guys1 = pref.back() - pref[i];
int guys2 = pref[i - 1];
rmax(ans, min(pref.back() - guys1 - guys2 + min(guys1 + right, m - i) + min(guys2 + left, i - 1), m));
}
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,132 @@
/* Problem URL: https://codeforces.com/problemset/problem/1857/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
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
ll maximal = -1e12;
repv(i, a) {
ll now;
cin >> now;
i -= now;
rmax(maximal, i);
}
set<int> ans;
rep(i, n) {
if (a[i] == maximal) {
ans.insert(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,181 @@
/* Problem URL: https://codeforces.com/problemset/problem/1868/B1 */
#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;
vi a(n);
ll total = 0;
repv(i, a) {
cin >> i;
total += i;
}
if (total % n) {
cout << "No\n";
return;
}
total /= n;
vi rem(32);
vi add(32);
int wild = 0;
rep(i, n) {
if (a[i] == total) {
wild++;
continue;
}
int x2 = 0;
int y2 = -1;
rep(j, 32) {
ll now = 1LL << x2;
if (total - a[i] + now <= 0) {
x2++;
continue;
}
if (__builtin_popcountll(total - a[i] + now) == 1) {
y2 = 63 - __builtin_clzll(total - a[i] + now);
break;
}
x2++;
}
if (y2 == -1) {
cout << "No\n";
return;
}
int re = rem[y2];
int ad = add[x2];
if (re) {
rem[y2]--;
} else {
add[y2]++;
}
if (ad) {
add[x2]--;
} else {
rem[x2]++;
}
}
rep(i, 32) {
if (rem[i] != add[i] || rem[i] > wild) {
cout << "No\n";
return;
}
wild -= rem[i];
}
cout << "Yes\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,221 @@
/* Problem URL: https://codeforces.com/problemset/problem/1873/H */
#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, a, b;
cin >> n >> a >> b;
a--, b--;
vi dsu(n);
rep(i, n) {
dsu[i] = i;
}
function<int(int)> find_p = [&](int i) {
if (dsu[i] == i) {
return dsu[i];
}
return dsu[i] = find_p(dsu[i]);
};
auto join = [&](int a, int b) {
a = find_p(a);
b = find_p(b);
dsu[b] = a;
return a == b;
};
vi loop;
pair<int, int> edge;
vvi graph(n);
rep(i, n) {
int u, v;
cin >> u >> v;
u--, v--;
if (!join(u, v)) {
graph[u].push_back(v);
graph[v].push_back(u);
continue;
}
edge = {u, v};
}
vi depth(n, -1);
vi parent(n);
function<void(int, int)> dfs = [&](int i, int p) {
depth[i] = depth[p] + 1;
parent[i] = p;
repv(j, graph[i]) {
if (j == p) {
continue;
}
dfs(j, i);
}
};
dfs(0, 0);
int u = edge.first;
int v = edge.second;
if (depth[u] > depth[v]) {
swap(u, v);
}
int diff = depth[v] - depth[u];
while (diff--) {
loop.push_back(v);
v = parent[v];
}
while (u != v) {
loop.push_back(u);
loop.push_back(v);
u = parent[u];
v = parent[v];
}
loop.push_back(u);
graph[edge.first].push_back(edge.second);
graph[edge.second].push_back(edge.first);
vi disa(n, oo);
vi disb(n, oo);
auto bfs = [&](int ini, vi &dis) {
dis[ini] = 0;
queue<int> q;
q.push(ini);
while (!q.empty()) {
auto i = q.front();
q.pop();
repv(j, graph[i]) {
if (dis[j] > dis[i] + 1) {
dis[j] = dis[i] + 1;
q.push(j);
}
}
}
};
bfs(a, disa);
bfs(b, disb);
repv(i, loop) {
if (disa[i] > disb[i]) {
cout << "YES\n";
return;
}
}
cout << "NO\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,157 @@
/* Problem URL: https://codeforces.com/problemset/problem/1883/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;
map<ll, int> var;
repv(i, a) {
var[i] = 0;
}
int v = 0;
repv(i, var) {
i.second = v;
v++;
}
repv(i, a) {
i = var[i];
}
vi next(n);
vi posr(n, -1);
rep(i, n) {
next[n - i - 1] = posr[a[n - i - 1]];
posr[a[n - i - 1]] = n - i - 1;
}
ll ans = 0;
int c = 0;
vi posl(n, -1);
rep(i, n) {
if (next[i] != -1) {
if (posl[a[i]] == -1) {
posl[a[i]] = 1;
c++;
}
continue;
}
if (posl[a[i]] == -1) {
posl[a[i]] = 1;
c++;
}
ans += c;
}
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,166 @@
/* Problem URL: https://codeforces.com/problemset/problem/1891/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;
cin >> n;
vl a(n);
cin >> a;
sortv(a);
ll x = 0;
int i = 0;
int j = n - 1;
ll ans = 0;
while (i < j) {
if (x + a[i] >= a[j]) {
ll diff = a[j] - x;
a[i] -= diff;
a[j] = 0;
ans += diff + 1;
j--;
i += a[i] == 0;
x = 0;
continue;
}
ans += a[i];
x += a[i];
a[i] = 0;
i++;
if (x >= a[j]) {
ans++;
x -= a[j];
a[j] = 0;
j--;
}
}
if (i > j) {
cout << ans << '\n';
return;
}
ll low = 0;
ll high = 1e9;
ll act = 0;
while (low <= high) {
ll mid = (low + high) >> 1;
if (x + (mid << 1) <= a[i]) {
act = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
ans += min(a[i], max(a[i] - (act << 1) - x + 1 + act, 0LL));
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,132 @@
/* Problem URL: https://codeforces.com/problemset/problem/1900/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;
cin >> n;
string a;
cin >> a;
V<array<int, 2>> tree(n);
rep(i, n) {
cin >> tree[i][0] >> tree[i][1];
tree[i][0]--;
tree[i][1]--;
}
function<int(int)> dfs = [&](int i) -> int {
if (i == -1) {
return oo;
}
if (tree[i][0] == -1 && tree[i][1] == -1) {
return 0;
}
return min(dfs(tree[i][0]) + (a[i] != 'L'), dfs(tree[i][1]) + (a[i] != 'R'));
};
cout << dfs(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

@@ -0,0 +1,162 @@
/* Problem URL: https://codeforces.com/problemset/problem/1945/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;
V<pair<ll, int>> a(n);
repv(i, a) {
cin >> i.first;
}
ordered_set<pair<int, ll>, greater<>> ord;
rep(i, n) {
int p;
cin >> p;
a[p - 1].second = i;
ord.insert({a[p - 1].second, a[p - 1].first});
}
sortv(a);
ll ans = 0;
int count = 0;
rep(i, n) {
ord.erase({a[i].second, a[i].first});
int low = 0;
int high = min((int)ord.size() - 1, a[i].second - 1);
int curc = 0;
while (low <= high) {
int mid = (low + high) >> 1;
int now = ord.find_by_order(mid)->first;
if (now - mid - 1 >= 0) {
curc = mid + 1;
low = mid + 1;
continue;
}
high = mid - 1;
}
curc++;
ll curans = a[i].first * curc;
if (curans > ans) {
ans = curans;
count = curc;
continue;
}
if (ans == curans && count > curc) {
count = curc;
}
}
cout << ans << ' ' << count << '\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,184 @@
/* Problem URL: https://codeforces.com/problemset/problem/1950/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;
string s;
cin >> n >> s;
vi divs;
auto getdivs = [&]() {
for (int i = 1; i * i <= n; i++) {
if (n % i == 0) {
divs.push_back(i);
if (i * i != n) {
divs.push_back(n / i);
}
}
}
};
getdivs();
sortv(divs);
string cur;
V<bool> diff;
repv(d, divs) {
fill(all(diff), false);
while (cur.size() < d) {
cur.push_back(s[cur.size()]);
diff.emplace_back();
}
int j = 0;
int c = 0;
rep(i, n) {
if (cur[j] != s[i]) {
diff[j] = true;
c++;
}
j = (j + 1) * (j < d - 1);
}
if (c <= 1) {
cout << d << '\n';
return;
}
if (c == n / d - 1) {
auto check = [&]() {
int choice = -1;
rep(i, d) {
if (diff[i]) {
if (choice != -1) {
return false;
}
choice = i;
}
}
char prev = cur[choice];
cur[choice] = s[choice + d];
int c = 0;
int j = 0;
rep(i, n) {
if (cur[j] != s[i]) {
c++;
}
j = (j + 1) * (j < d - 1);
}
cur[choice] = prev;
return c == 1;
};
if (check()) {
cout << d << '\n';
return;
}
}
}
}
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,157 @@
/* Problem URL: https://codeforces.com/problemset/problem/1973/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;
vi a(n);
cin >> a;
int low = 1;
int high = n;
int ans = n;
while (low <= high) {
int mid = (low + high) >> 1;
vi bit(20);
rep(i, mid) {
rep(j, 20) {
bit[j] += (a[i] >> j) & 1;
}
}
int now = 0;
rep(i, 20) {
now |= (bit[i] > 0) << i;
}
bool valid = true;
nrep(i, 1, n - mid + 1) {
rep(j, 20) {
bit[j] -= (a[i - 1] >> j) & 1;
bit[j] += (a[i + mid - 1] >> j) & 1;
}
int cur = 0;
rep(j, 20) {
cur |= (bit[j] > 0) << j;
}
if (cur != now) {
valid = false;
break;
}
}
if (valid) {
ans = mid;
high = mid - 1;
continue;
}
low = mid + 1;
}
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,153 @@
/* Problem URL: https://codeforces.com/problemset/problem/1986/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
void solve()
{
int n;
cin >> n;
string s;
cin >> s;
if (n == 2) {
cout << stoi(s) << '\n';
return;
}
repv(i, s) {
i -= '0';
}
if (n == 3) {
int onei = s[0] * 10 + s[1];
int twoi = s[2];
int onej = s[0];
int twoj = s[1] * 10 + s[2];
cout << min({onei + twoi, onei * twoi, onej + twoj, onej * twoj}) << '\n';
return;
}
ll total = 0;
rep(i, n) {
if (s[i] == 0) {
cout << "0\n";
return;
}
if (s[i] == 1) {
continue;
}
total += s[i];
}
ll ans = OO;
rep(i, n - 1) {
ll act = s[i] * 10 + s[i + 1];
rmin(ans, total - (s[i] == 1 ? 0 : s[i]) - (s[i + 1] == 1 ? 0 : s[i + 1]) + act);
}
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,140 @@
/* Problem URL: https://codeforces.com/problemset/problem/1995/B2 */
#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;
ll m;
cin >> n >> m;
V<pair<ll, ll>> a(n);
repv(i, a) {
cin >> i.first;
}
repv(i, a) {
cin >> i.second;
}
sortv(a);
ll ans = 0;
nrep(i, 1, n) {
rmax(ans, min(m / a[i - 1].first, a[i - 1].second) * a[i - 1].first);
if (a[i - 1].first != a[i].first - 1) {
continue;
}
ll c1 = min(a[i - 1].second, m / a[i - 1].first);
ll c2 = min(a[i].second, (m - a[i - 1].first * c1) / a[i].first);
ll c = m - c1 * a[i - 1].first - c2 * a[i].first;
ll r = min({c1, a[i].second - c2, c});
rmax(ans, (c1 - r) * a[i - 1].first + (c2 + r) * a[i].first);
}
rmax(ans, min(m / a.back().first, a.back().second) * a.back().first);
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,149 @@
/* Problem URL: https://codeforces.com/problemset/problem/2003/D1 */
#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;
}
bool b[(int)2e5 + 2];
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
ll m;
cin >> n >> m;
ll maximal = 0;
rep(i, n) {
int l;
cin >> l;
rep(j, l) {
int now;
cin >> now;
if (now > l) {
continue;
}
b[now] = true;
}
ll mex = 0;
while (b[mex]) {
mex++;
}
mex++;
while (b[mex]) {
mex++;
}
rmax(maximal, mex);
rep(j, l + 1) {
b[j] = false;
}
}
if (maximal >= m) {
cout << maximal * (m + 1) << '\n';
return;
}
cout << maximal * (maximal + 1) + (m - maximal) * (maximal + m + 1) / 2 << '\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,145 @@
/* Problem URL: https://codeforces.com/problemset/problem/2036/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
ll XOR(ll to)
{
ll ans[] = {to, 1, to + 1, 0};
return ans[to & 3];
}
void solve()
{
ll l, r, i, k;
cin >> l >> r >> i >> k;
ll ans = XOR(r) ^ (l > 0 ? XOR(l - 1) : 0);
if (r <= (1LL << i)) {
cout << (ans ^ (k >= l && k <= r ? k : 0));
return;
}
ll lcomp = l >> i;
ll rcomp = r >> i;
if (rcomp > 0) {
ans ^= (XOR(rcomp - 1) ^ (lcomp > 0 ? XOR(lcomp - 1) : 0)) << i;
}
if ((rcomp - lcomp) & 1) {
ans ^= k;
}
ll mask = 1LL << i;
if ((l & mask) <= k) {
ans ^= k;
ans ^= (l & ~mask);
}
if ((r & mask) >= k) {
ans ^= k;
ans ^= (r & ~mask);
}
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,171 @@
/* Problem URL: https://codeforces.com/problemset/problem/2028/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;
ll v;
cin >> n >> m >> v;
vl a(n);
cin >> a;
vl pref(n);
vl suf(n);
ll cur = 0;
rep(i, n) {
cur += a[i];
if (cur >= v) {
pref[i]++;
cur = 0;
}
}
cur = 0;
for (int i = n - 1; i >= 0; i--) {
cur += a[i];
if (cur >= v) {
suf[i]++;
cur = 0;
}
}
nrep(i, 1, n) {
pref[i] += pref[i - 1];
suf[n - i - 1] += suf[n - i];
}
if (pref.back() < m) {
cout << "-1\n";
return;
}
auto gettot = [&](int l, int r) {
ll tot = 0;
if (l > 0) {
tot += pref[l - 1];
}
if (r < n - 1) {
tot += suf[r + 1];
}
return tot;
};
ll ans = 0;
cur = 0;
int j = 0;
rep(i, n) {
cur += a[i];
while (gettot(j, i) < m) {
cur -= a[j];
j++;
}
rmax(ans, 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();
}
}

View File

@@ -1,16 +1,22 @@
/* Problem URL: https://codeforces.com/contest/2050/problem/F */ /* Problem URL: https://codeforces.com/problemset/problem/2050/F */
#include <bits/stdc++.h> #include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std; 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 V vector
#define rmin(a, b) a = min(a, b) #define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b) #define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (size_t i = 0; i < (lim); i++) #define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v)) #define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
@@ -69,25 +75,70 @@ auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
return is; return is;
} }
int dp(int i, int j, int k, string &a, string &b, string &c) const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{ {
if (i >= a.size() || j >= b.size() || k >= c.size()) {
return 0; }
#define TEST 1
void solve()
{
int n, q;
cin >> n >> q;
vl a(n);
cin >> a;
if (n == 1) {
while (q--) {
int l, r;
cin >> l >> r;
cout << "0" << " \n"[q == 0];
}
return;
} }
if (a[i] != c[k] && b[j] != c[k]) { vl diffs(n - 1);
return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k, a, b, c)) + 1; rep(i, n - 1) {
diffs[i] = abs(a[i] - a[i + 1]);
} }
if (a[i] == b[j]) { int size = n - 1;
return min(dp(i + 1, j, k + 1, a, b, c), dp(i, j + 1, k, a, b, c));
vvl sparse(20, vl(size));
rep(i, size) {
sparse[0][i] = diffs[i];
} }
if (a[i] == c[k]) { nrep(j, 1, 20) {
return dp(i + 1, j, k + 1, a, b, c); for (int i = 0; i + (1 << (j - 1)) < size; i++) {
sparse[j][i] = __gcd(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
}
} }
return dp(i, j + 1, k + 1, a, b, c); auto squery = [&](int l, int r) {
int log = 31 - __builtin_clz(r - l + 1);
return __gcd(sparse[log][l], sparse[log][r - (1 << log) + 1]);
};
while (q--) {
int l, r;
cin >> l >> r;
l--, r--;
if (l == r) {
cout << "0" << " \n"[q == 0];
continue;
}
cout << squery(l, r - 1) << " \n"[q == 0];
}
} }
int main() int main()
@@ -95,12 +146,11 @@ int main()
ios::sync_with_stdio(false); ios::sync_with_stdio(false);
cin.tie(nullptr); cin.tie(nullptr);
int t; pre();
cin >> t;
while (t--) {
string a, b, c;
cin >> a >> b >> c;
cout << dp(0, 0, 0, a, b, c) << '\n'; int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
} }
} }

View File

@@ -0,0 +1,152 @@
/* Problem URL: https://codeforces.com/problemset/problem/2049/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, x, y;
cin >> n >> x >> y;
x--, y--;
vi ans(n, -1);
ans[x] = 0;
ans[y] = 1;
queue<int> q;
q.push(x - 1 + n * (x == 0));
q.push(x + 1 - n * (x == n - 1));
q.push(y - 1 + n * (y == 0));
q.push(y + 1 - n * (y == n - 1));
while (!q.empty()) {
auto i = q.front();
q.pop();
if (ans[i] != -1) {
continue;
}
int prev = i - 1 + n * (i == 0);
int next = i + 1 - n * (i == n - 1);
array<bool, 6> ex = {};
if (ans[prev] != -1) {
ex[ans[prev]] = true;
}
if (ans[next] != -1) {
ex[ans[next]] = true;
}
int mex = 0;
while (ex[mex]) {
mex++;
}
ans[i] = mex;
q.push(prev);
q.push(next);
}
cout << ans;
}
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,155 @@
/* Problem URL: https://codeforces.com/problemset/problem/1743/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 0
void solve()
{
int n;
string s;
cin >> n >> s;
int ind = 0;
while (ind < s.size() && s[ind] == '0') {
ind++;
}
if (ind >= s.size()) {
cout << "0\n";
return;
}
// s = s.substr(ind);
while (ind < s.size() && s[ind] == '1') {
ind++;
}
if (ind == s.size()) {
cout << s << '\n';
return;
}
int size = s.size() - ind;
string ans = s;
while (size < s.size()) {
for (int i = 0; i + size < s.size(); i++) {
string cur = s.substr(i, size);
string tmp = s;
rep(j, size) {
if (cur[size - j - 1] == '1') {
tmp[s.size() - j - 1] = '1';
}
}
rmax(ans, tmp);
}
size++;
}
int c = 0;
while (ans[c] == '0') {
c++;
}
cout << ans.substr(c) << '\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,131 @@
/* Problem URL: https://codeforces.com/problemset/problem/1809/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, k;
cin >> n >> k;
vi ans(n, -2);
vi pos(n);
rep(i, n) {
if (~i & 1) {
pos[i] = i >> 1;
} else {
pos[i] = n - (i >> 1) - 1;
}
}
for (int cur = 0; k > 0; cur++) {
k--;
if (k == 0) {
ans[pos[cur]] = 1;
continue;
}
int now = min(n - cur - 1, k);
ans[pos[cur]] = now * 2 + 1;
k -= now;
}
cout << ans;
}
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,119 @@
/* Problem URL: https://codeforces.com/problemset/problem/1814/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()
{
ll a, b;
cin >> a >> b;
auto calc = [&](ll now) {
return now - 1 + (a + now - 1) / now + (b + now - 1) / now;
};
ll ans = OO;
nrep(i, 1, (int)1e5 + 1) {
rmin(ans, calc(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,147 @@
/* Problem URL: https://codeforces.com/problemset/problem/1814/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;
ll s1, s2;
cin >> n >> s1 >> s2;
V<pair<int, int>> ball(n);
rep(i, n) {
cin >> ball[i].first;
ball[i].second = i + 1;
}
sort(all(ball), greater<>());
vi a;
vi b;
int now = 0;
ll rb1 = s1;
ll rb2 = s2;
while (now < n) {
if (rb1 <= rb2) {
a.push_back(ball[now].second);
now++;
rb1 += s1;
continue;
}
b.push_back(ball[now].second);
now++;
rb2 += s2;
}
cout << a.size();
rep(i, a.size()) {
cout << ' ' << a[i];
}
cout << '\n';
cout << b.size();
rep(i, b.size()) {
cout << ' ' << b[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,137 @@
/* Problem URL: https://codeforces.com/problemset/problem/1821/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
void solve()
{
int n;
ll k;
cin >> n >> k;
V<pair<ll, ll>> ed(n);
repv(i, ed) {
cin >> i.first;
}
repv(i, ed) {
cin >> i.second;
}
ll ans = OO;
ll total = 0;
priority_queue<ll, vl, greater<>> pq;
rep(i, n) {
ll cost = ed[i].second - ed[i].first + 1;
pq.push(cost);
total += cost;
while (total - pq.top() >= k && pq.top() == 1) {
total -= pq.top();
pq.pop();
}
if (total >= k) {
rmin(ans, ed[i].second - total + k + (ll)pq.size() * 2);
}
}
cout << (ans == OO ? -1 : 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,152 @@
/* Problem URL: https://codeforces.com/problemset/problem/1845/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()
{
string s, l, r;
int m;
cin >> s >> m >> l >> r;
vvi next(s.size() + 1);
vi prev(10, s.size() + 1);
for (int i = s.size() - 1; i >= 0; i--) {
next[i + 1] = prev;
prev[s[i] - '0'] = i + 1;
}
next[0] = prev;
rep(i, m) {
l[i] -= '0';
r[i] -= '0';
}
vvi memo(s.size() + 1, vi(m, -1));
function<bool(int, int)> dp = [&](int i, int j) {
if (i > s.size()) {
return true;
}
if (j >= m) {
return false;
}
int &ans = memo[i][j];
if (ans != -1) {
return false;
}
ans = 0;
nrep(k, l[j], r[j] + 1) {
if (dp(next[i][k], j + 1)) {
return true;
}
}
return false;
};
cout << (dp(0, 0) ? "YES\n" : "NO\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,163 @@
/* Problem URL: https://codeforces.com/problemset/problem/1879/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;
const int MAXN = 2e5 + 1;
const ll mod = 998244353;
ll fact[MAXN];
ll inv[MAXN];
void pre()
{
fact[0] = 1;
nrep(i, 1, MAXN) {
fact[i] = (fact[i - 1] * i) % mod;
}
inv[0] = 1;
inv[1] = 1;
nrep(i, 2, MAXN) {
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
nrep(i, 2, MAXN) {
inv[i] = (inv[i] * inv[i - 1]) % mod;
}
}
#define TEST 1
void solve()
{
string s;
cin >> s;
vl count;
int now = 1;
ll total = 0;
int c = s.size() - 1;
nrep(i, 1, s.size()) {
if (s[i] == s[i - 1]) {
now++;
continue;
}
c--;
if (now > 1) {
count.push_back(now);
total += now - 1;
}
now = 1;
}
if (now > 1) {
total += now - 1;
count.push_back(now);
}
auto comb = [&](ll n, ll k) {
return (fact[n] * inv[k] % mod * inv[n - k]) % mod;
};
ll ans = 1;
repv(i, count) {
ans = (ans * fact[i]) % mod;
ans = (ans * comb(total, i - 1)) % mod;
total -= i - 1;
}
cout << c << ' ' << 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,133 @@
/* Problem URL: https://codeforces.com/problemset/problem/2026/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;
string a;
cin >> n >> a;
ll ans = 0;
priority_queue<ll> ac;
for (int i = n - 1; i >= 0; i--) {
if (a[i] == '0') {
if (!ac.empty()) {
ans -= ac.top();
ac.pop();
}
ans += i + 1;
continue;
}
ans += i + 1;
ac.push(i + 1);
}
int rem = ac.size() >> 1;
while (rem--) {
ans -= ac.top();
ac.pop();
}
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,173 @@
/* Problem URL: https://codeforces.com/problemset/problem/2026/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 0
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
vl pref(n + 1);
vl preft(n + 1);
rep(i, n) {
pref[i + 1] = pref[i] + a[i];
}
rep(i, n) {
preft[i + 1] = pref.back() - pref[i] + preft[i];
}
// cout << preft;
ll total = (ll)n * (n + 1) / 2;
int q;
cin >> q;
while (q--) {
ll l, r;
cin >> l >> r;
l--, r--;
ll low = 0;
ll high = n;
ll lans = 0;
while (low <= high) {
ll mid = (low + high) >> 1;
if (mid * (n + mid) / 2 <= l) {
lans = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
l -= lans * (lans + n) / 2;
low = 0;
high = n;
ll rans = 0;
while (low <= high) {
ll mid = (low + high) >> 1;
if (mid * (n + mid) / 2 <= r) {
rans = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
r -= rans * (rans + n) / 2;
if (rans == lans) {
cout << pref[rans + r + 1] - pref[lans + l] << '\n';
continue;
}
cout << preft[rans] - preft[lans] << '\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,173 @@
/* Problem URL: https://codeforces.com/edu/course/2/lesson/2/1/practice/contest/269100/problem/A */
#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 a;
cin >> a;
a.push_back('$');
int n = a.size();
int log = 1;
if (n > 1) {
log = 32 - __builtin_clz(n - 1);
}
vvi suffix(log + 1, vi(n));
rep(i, n) {
suffix[0][i] = i;
}
sort(all(suffix[0]), [&](int i, int j){
return a[i] < a[j];
});
vi prev(n);
nrep(i, 1, n) {
if (a[suffix[0][i]] == a[suffix[0][i - 1]]) {
prev[suffix[0][i]] = prev[suffix[0][i - 1]];
continue;
}
prev[suffix[0][i]] = prev[suffix[0][i - 1]] + 1;
}
V<tuple<int, int, int>> cur(n);
nrep(j, 1, log + 1) {
rep(i, n) {
int next = (i + (1 << (j - 1))) - n * ((i + (1 << (j - 1))) >= n);
cur[i] = {prev[i], prev[next], i};
}
sort(all(cur), [&](tuple<int, int, int> &a, tuple<int, int, int> &b){
auto [ai, aj, ak] = a;
auto [bi, bj, bk] = b;
if (ai == bi) {
return aj < bj;
}
return ai < bi;
});
prev[get<2>(cur[0])] = 0;
suffix[j][0] = get<2>(cur[0]);
nrep(i, 1, n) {
auto [ai, aj, ak] = cur[i];
auto [bi, bj, bk] = cur[i - 1];
suffix[j][i] = ak;
if (ai == bi && aj == bj) {
prev[ak] = prev[bk];
continue;
}
prev[ak] = prev[bk] + 1;
}
}
cout << suffix[log];
}
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,140 @@
/* Problem URL: https://codeforces.com/problemset/problem/1991/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;
cin >> n;
vl a(n);
cin >> a;
bool act[2] = {false, false};
rep(i, n) {
act[a[i] & 1] = true;
}
if (act[0] && act[1]) {
cout << "-1\n";
return;
}
vl ops;
auto sub = [&](ll now) {
ops.push_back(now);
rep(i, n) {
a[i] = abs(a[i] - now);
}
};
rep(i, 30) {
sub(1 << (29 - i));
}
if (a[0] == 1) {
ops.push_back(1);
}
cout << ops.size() << '\n';
cout << ops;
}
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,131 @@
/* Problem URL: https://codeforces.com/problemset/problem/1774/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()
{
ll n, m, k;
cin >> n >> m >> k;
vl a(m);
cin >> a;
if (k == 1) {
cout << "YES\n";
return;
}
ll lim = n / k;
ll mod = n % k;
rep(i, m) {
if (a[i] > lim) {
if (a[i] == lim + 1 && mod) {
mod--;
continue;
}
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

61
TODO.md
View File

@@ -13,58 +13,79 @@ Official divs from codeforces
- Educational - Educational
1. [F. Frogs and Mosquitoes](https://codeforces.com/contest/609/problem/F) - [ ] [F. Frogs and Mosquitoes](https://codeforces.com/contest/609/problem/F)
I have no clue where to even start. I have no clue where to even start.
2. [E. Water Taps](https://codeforces.com/contest/954/problem/E) - [ ] [E. Water Taps](https://codeforces.com/contest/954/problem/E)
I have an idea, but it's really annoying to solve it. I have an idea, but it's really annoying to solve it.
3. [F. Fibonacci String Subsequences](https://codeforces.com/contest/946/problem/F) - [ ] [F. Fibonacci String Subsequences](https://codeforces.com/contest/946/problem/F)
Obviously DP, but not sure how to do it... Obviously DP, but not sure how to do it...
4. [F. Imbalance Value of Tree](https://codeforces.com/contest/915/problem/F) - [ ] [F. Imbalance Value of Tree](https://codeforces.com/contest/915/problem/F)
Tree problem, looks interesting. Tree problem, looks interesting.
5. [F. Clear The Matrix](https://codeforces.com/contest/903/problem/F) - [ ] [F. Clear The Matrix](https://codeforces.com/contest/903/problem/F)
DP with optimization? DP with optimization?
- [ ] [D. Sums of Segments](https://codeforces.com/problemset/problem/2026/D)
Another problem that I didn't have time to finish...
- Div 1/2 - Div 1/2
1. [D. Division Versus Addition](https://codeforces.com/contest/2152/problem/D) - [ ] [D. Division Versus Addition](https://codeforces.com/contest/2152/problem/D)
The idea is simple, but I have to think through it better. The idea is simple, but I have to think through it better.
2. [D2. Inversion Graph Coloring](https://codeforces.com/contest/2143/problem/D2) - [ ] [D2. Inversion Graph Coloring](https://codeforces.com/contest/2143/problem/D2)
DP with optimization. DP with optimization.
3. [E. Yet Another MEX Problem](https://codeforces.com/contest/2146/problem/E) - [ ] [E. Yet Another MEX Problem](https://codeforces.com/contest/2146/problem/E)
Maybe MEX with two pointers? Maybe MEX with two pointers?
4. [D. A Cruel Segment's Thesis](https://codeforces.com/contest/2140/problem/D) - [ ] [D. A Cruel Segment's Thesis](https://codeforces.com/contest/2140/problem/D)
Looks greedy. Looks greedy.
5. [F. Flint and Steel](https://codeforces.com/contest/2133/problem/F) - [ ] [F. Flint and Steel](https://codeforces.com/contest/2133/problem/F)
I tried this a long time ago, don't remember what it's about, just remembered I tried this a long time ago, don't remember what it's about, just remembered
that I tried a greedy solution and it didn't work. that I tried a greedy solution and it didn't work.
- [ ] [C. Music Festival](https://codeforces.com/problemset/problem/1801/C)
Looks like DP.
- [ ] [C1. Maple and Tree Beauty (Easy Version)](https://codeforces.com/problemset/problem/2138/C1)
I finished with one type of knapsack, but apparently there's a better way to do
it that's going to be used to optimize later.
- [ ] [D1. Hot Start Up (easy version)](https://codeforces.com/contest/1799/problem/D1)
DP, but couldn't do during the simulation...
- Div 3 - Div 3
1. [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G) - [ ] [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G)
Graph problem, don't remember the details. Graph problem, don't remember the details.
- [ ] [XORificator 3000](https://codeforces.com/problemset/problem/2036/F)
I had an idea, but didn't have time to implement it.
- Div 4 - Div 4
1. [F. Gravity Falls](https://codeforces.com/contest/2148/problem/F) - [ ] [F. Gravity Falls](https://codeforces.com/contest/2148/problem/F)
I barely read it, not sure were I found it either, but looks pretty easy. I barely read it, not sure were I found it either, but looks pretty easy.
@@ -74,7 +95,7 @@ Gym from simulations or contests that I did
- ICPC Brazil first phase - ICPC Brazil first phase
1. [D. Dominoes](https://codeforces.com/gym/106073/problem/D) - [ ] [D. Dominoes](https://codeforces.com/gym/106073/problem/D)
If I did this one, I would have passed into second phase, must try to solve it If I did this one, I would have passed into second phase, must try to solve it
ASAP. ASAP.
@@ -83,22 +104,28 @@ Gym from simulations or contests that I did
[Link](https://codeforces.com/gym/104875) because it has no problem page in gym. [Link](https://codeforces.com/gym/104875) because it has no problem page in gym.
1. L. Last Guess - [ ] L. Last Guess
I think I can solve it with flow. I think I can solve it with flow.
- USP Try-outs - USP Try-outs
1. [B. Tuk-Tuk Express](https://codeforces.com/gym/103934/problem/B) - [ ] [B. Tuk-Tuk Express](https://codeforces.com/gym/103934/problem/B)
I have NO clue why my code keeps getting WA, awful implementation problem. I have NO clue why my code keeps getting WA, awful implementation problem.
2. [J. Apep, the Lord of Chaos](https://codeforces.com/gym/103934/problem/J) - [ ] [J. Apep, the Lord of Chaos](https://codeforces.com/gym/103934/problem/J)
I don't remember what the problem was about, I just know that I tried it before. I don't remember what the problem was about, I just know that I tried it before.
- think-cell round 1
- [X] [C. Lexicographically Largest](https://codeforces.com/problemset/problem/1930/C)
I'll be honest, I just got bored in the middle.
## Atcoder ## Atcoder
1. [F. Operate K](https://atcoder.jp/contests/abc386/tasks/abc386_f) - [ ] [F. Operate K](https://atcoder.jp/contests/abc386/tasks/abc386_f)
DP with optimization. DP with optimization.

View File

@@ -0,0 +1,229 @@
/* Problem URL: https://codeforces.com/problemset/problem/1787/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
const int MAXN = 2e5 + 10;
bool vis[MAXN];
bool frame[MAXN];
bool loop[MAXN];
int sz[MAXN];
void solve()
{
int n;
cin >> n;
vi a(n);
cin >> a;
rep(i, n) {
a[i] += i;
}
vi next(n + 2);
rep(i, n) {
int ind = a[i];
if (ind < 0) {
next[i + 1] = 0;
continue;
}
if (ind >= n) {
next[i + 1] = n + 1;
continue;
}
next[i + 1] = ind + 1;
}
fill(vis, vis + n + 2, false);
fill(frame, frame + n + 2, false);
fill(loop, loop + n + 2, false);
ll c = 0;
function<bool(int)> dfs = [&](int i) -> bool {
if (i == 0 || i == n + 1) {
return false;
}
vis[i] = true;
frame[i] = true;
int j = next[i];
if (frame[j] || loop[j]) {
loop[i] = true;
}
if (!vis[j] && dfs(j)) {
loop[i] = true;
}
frame[i] = false;
c += !loop[i];
return loop[i];
};
rep(i, n) {
if (!vis[i + 1]) {
dfs(i + 1);
}
}
stack<int> topsort;
fill(vis, vis + n + 2, false);
function<void(int)> ss = [&](int i) {
if (i == 0 || i == n + 1) {
return;
}
vis[i] = true;
if (!vis[next[i]]) {
ss(next[i]);
}
topsort.push(i);
};
rep(i, n) {
if (!vis[i + 1] && !loop[i + 1]) {
ss(i + 1);
}
}
fill(sz, sz + n + 2, 1);
while (!topsort.empty()) {
auto i = topsort.top();
topsort.pop();
sz[next[i]] += sz[i];
}
if (loop[1]) {
ll ans = c + n + 1;
int now = next[1];
fill(frame, frame + n + 2, false);
frame[1] = true;
while (!frame[now]) {
ans += c + n + 1;
frame[now] = true;
now = next[now];
}
cout << ans << '\n';
return;
}
ll ans = c - sz[1] + n + 1;
int now = next[1];
ll count = 1;
while (now != n + 1 && now != 0) {
ans += c - sz[now] + n + 1;
count++;
now = next[now];
}
ans += (n - count) * 2 * n + (n - count);
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,212 @@
/* Problem URL: https://codeforces.com/problemset/problem/1930/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
struct dymseg {
struct node {
int left;
int right;
int val;
};
V<node> seg;
dymseg(int n) {
seg.emplace_back(-1, -1, 0);
}
int create_node() {
seg.emplace_back(-1, -1, 0);
return seg.size() - 1;
}
void create_nodes(int i) {
if (seg[i].left == -1) {
seg[i].left = create_node();
}
if (seg[i].right == -1) {
seg[i].right = create_node();
}
}
void update(int i, ll l, ll r, int p) {
if (l == r) {
seg[i].val = 1;
return;
}
seg[i].val++;
create_nodes(i);
ll mid = (l + r) >> 1;
if (mid >= p) {
update(seg[i].left, l, mid, p);
return;
}
update(seg[i].right, mid + 1, r, p);
}
ll tl, tr;
ll query(int i, ll l, ll r) {
if (l > tr || r < tl || seg[i].val == r - l + 1) {
return -1;
}
if (l == r) {
return l;
}
ll mid = (l + r) >> 1;
create_nodes(i);
int ans = query(seg[i].right, mid + 1, r);
if (ans != -1) {
return ans;
}
return query(seg[i].left, l, mid);
}
public:
void update(ll p) {
update(0, 0, 1LL << 31, p);
}
ll query(ll l, ll r) {
tl = l;
tr = r;
return query(0, 0, 1LL << 31);
}
};
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
dymseg seg(n);
set<ll, greater<>> ans;
rep(i, n) {
ll choice = seg.query(a[i], a[i] + i + 1);
if (choice == -1) {
continue;
}
ans.insert(choice);
seg.update(choice);
}
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();
}
}