Add a bunch more stuff

This commit is contained in:
2026-03-16 20:14:14 -03:00
parent 41d9ca1dc8
commit 23630c1356
52 changed files with 7572 additions and 1 deletions

View File

@@ -0,0 +1,359 @@
/* Problem URL: https://codeforces.com/gym/102361/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;
struct pt {
ll x, y;
int c;
pt() = default;
pt(ll x, ll y): x(x), y(y) {}
pt(pt a, int c): x(a.x), y(a.y), c(c) {}
friend istream &operator >> (istream &is, pt &a) {
is >> a.x >> a.y;
return is;
}
pt operator - (pt b) {
return pt(x - b.x, y - b.y);
}
ll operator * (pt b) {
return x * b.x + y * b.y;
}
ll operator ^ (pt b) {
return x * b.y - y * b.x;
}
};
int ccw(pt a, pt b, pt c)
{
ll r = (b - a) ^ (c - b);
return (r > 0) - (r < 0);
}
ll cdot(pt a, pt b, pt c)
{
return (b - a) * (c - a);
}
int quad(pt a)
{
static int q[][2] = {{0, 1}, {3, 2}};
return q[a.x < 0][a.y < 0];
}
bool polar_cmp(pt a, pt b)
{
if (quad(a) != quad(b)) {
return quad(a) < quad(b);
}
return ccw(pt(0, 0), a, b) < 0;
}
void pre()
{
}
#define TEST 0
void solve()
{
int n, q;
cin >> n >> q;
V<pt> pts(n + q);
cin >> pts;
vl ans(q);
vi inv(n);
vi va(q);
vi ev(n + q);
iota(all(inv), 0);
iota(all(va), n);
iota(all(ev), 0);
rep(i, n) {
auto cmp = [&](int j, int k) {
if (j == i) {
return false;
}
if (k == i) {
return true;
}
return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);
};
sort(all(va), cmp);
sort(all(inv), cmp);
int sz = inv.size() - 1;
V<pair<int, ll>> tmp;
int c = 1;
nrep(j, 1, sz) {
if (ccw(pts[i], pts[inv[j - 1]], pts[inv[j]]) == 0) {
c++;
continue;
}
tmp.emplace_back(inv[j - 1], c);
c = 1;
}
tmp.emplace_back(inv.end()[-2], c);
int j = 0;
int k = 0;
auto valid1 = [&]() {
auto p1 = pts[va[j]];
if (ccw(pts[i], p1, pts[inv[0]]) > 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) > 0) {
return false;
}
if (ccw(pts[i], p1, pts[inv[0]]) < 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) < 0 &&
cdot(pts[i], p1, pts[inv[0]]) > 0 && cdot(pts[i], p1, pts[inv.end()[-2]]) > 0) {
return false;
}
return true;
};
while (j < va.size()) {
auto p1 = pts[va[j]];
auto p2 = pts[tmp[k % tmp.size()].first];
if (!valid1()) {
j++;
continue;
}
if (ccw(pts[i], p1, p2) > 0) {
k++;
continue;
}
ll dot = cdot(pts[i], p1, p2);
if (dot < 0) {
j++;
continue;
}
if (dot > 0) {
auto p3 = pts[tmp[(k + 1) % tmp.size()].first];
if (ccw(pts[i], p1, p3) > 0 || cdot(pts[i], p1, p3) < 0) {
j++;
continue;
}
k++;
continue;
}
ans[va[j] - n] += tmp[k % tmp.size()].second;
j++;
}
auto valid2 = [&]() {
auto p1 = pts[va[j]];
if (ccw(pts[i], p1, pts[inv[0]]) < 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) < 0) {
return false;
}
if (ccw(pts[i], p1, pts[inv[0]]) > 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) > 0 &&
cdot(pts[i], p1, pts[inv[0]]) > 0 && cdot(pts[i], p1, pts[inv.end()[-2]]) > 0) {
return false;
}
return true;
};
j = va.size() - 1;
k = tmp.size() - 1;
auto kpos = [&]() {
return (k % tmp.size() + tmp.size()) % tmp.size();
};
while (j >= 0) {
auto p1 = pts[va[j]];
auto p2 = pts[tmp[kpos()].first];
if (!valid2()) {
j--;
continue;
}
if (ccw(pts[i], p1, p2) < 0) {
k--;
continue;
}
ll dot = cdot(pts[i], p1, p2);
if (dot < 0) {
j--;
continue;
}
if (dot > 0) {
auto p3 = pts[tmp[((k - 1) % tmp.size() + tmp.size()) % tmp.size()].first];
if (ccw(pts[i], p1, p3) > 0 || cdot(pts[i], p1, p3) < 0) {
j--;
continue;
}
k--;
continue;
}
ans[va[j] - n] += tmp[kpos()].second;
j--;
}
}
nrep(i, n, n + q) {
auto cmp = [&](int j, int k) {
return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);
};
sort(all(inv), cmp);
V<pair<int, ll>> tmp;
int c = 1;
nrep(j, 1, n) {
if (ccw(pts[i], pts[inv[j - 1]], pts[inv[j]]) == 0) {
c++;
continue;
}
tmp.emplace_back(inv[j - 1], c);
c = 1;
}
tmp.emplace_back(inv.end()[-1], c);
int j = 0;
int k = 0;
while (j < tmp.size()) {
while (k <= j) {
k++;
}
auto p1 = pts[tmp[j].first];
auto p2 = pts[tmp[k % tmp.size()].first];
ll dot = (p1 - pts[i]) * (p2 - pts[i]);
if (ccw(pts[i], p1, p2) >= 0 || dot < 0) {
j++;
continue;
}
if (dot > 0) {
k++;
continue;
}
ans[i - n] += tmp[j].second * tmp[k % tmp.size()].second;
j++;
}
}
repv(i, ans) {
cout << i << '\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,223 @@
/* Problem URL: https://codeforces.com/gym/102361/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;
struct pt {
ll x, y;
pt() = default;
pt(ll x, ll y): x(x), y(y) {}
friend istream &operator >> (istream &is, pt &a) {
is >> a.x >> a.y;
return is;
}
pt operator - (pt b) {
return pt(x - b.x, y - b.y);
}
ll operator ^ (pt b) {
return x * b.y - y * b.x;
}
ll operator * (pt b) {
return x * b.x + y * b.y;
}
bool operator < (pt b) const {
if (x == b.x) {
return y < b.y;
}
return x < b.x;
}
};
int quad(pt a)
{
static const int q[][2] = {{0, 1}, {3, 2}};
return q[a.x < 0][a.y < 0];
}
int ccw(pt a, pt b, pt c)
{
ll r = (b - a) ^ (c - b);
return (r > 0) - (r < 0);
}
bool polar_cmp(pt a, pt b)
{
if (quad(a) != quad(b)) {
return quad(a) < quad(b);
}
return ccw(pt(0, 0), a, b) < 0;
}
ll dot(pt a, pt b, pt c)
{
return (b - a) * (c - a);
}
void pre()
{
}
#define TEST 0
void solve()
{
int n, q;
cin >> n >> q;
V<pt> a(n + q);
cin >> a;
vl ans(q);
vi p(n + q);
iota(all(p), 0);
rep(i, n + q) {
sort(all(p), [&](int j, int k){
return polar_cmp(a[j] - a[i], a[k] - a[i]);
});
int j = 0;
int now = 1;
while (j < n + q) {
int p1 = p[j];
int p2 = p[now];
if (p1 == i) {
j++;
continue;
}
if (now == j || p2 == i) {
now = (now + 1) % (q + n);
continue;
}
if (ccw(a[i], a[p1], a[p2]) >= 0) {
j++;
continue;
}
ll d = dot(a[i], a[p1], a[p2]);
if (d < 0) {
j++;
continue;
}
if (d > 0) {
now = (now + 1) % (q + n);
continue;
}
if (i >= n && p1 < n && p2 < n) {
ans[i - n]++;
} else if (i < n && p1 >= n && p2 < n) {
ans[p1 - n]++;
} else if (i < n && p1 < n && p2 >= n) {
ans[p2 - n]++;
}
j++;
}
}
repv(i, ans) {
cout << i << '\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/2038/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()
{
int n, k;
cin >> n >> k;
vl a(n);
vl b(n);
cin >> a >> b;
ll total = 0;
vl ans(n);
rep(i, n) {
ans[i] = a[i] / b[i];
total += ans[i];
}
if (total < k) {
rep(i, n) {
cout << "0" << " \n"[i == n - 1];
}
return;
}
int inc = total - k;
rep(i, n) {
if (ans[i] < inc) {
inc -= ans[i];
ans[i] = 0;
continue;
}
ans[i] -= inc;
break;
}
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/2181/M */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
string a, b;
cin >> a >> b;
int n = a.size();
vvi dp(n, vi(2));
dp[n - 1][0] = b[n - 1] != '0';
dp[n - 1][1] = b[n - 1] != '1';
for (int i = n - 2; i >= 0; i--) {
dp[i][0] = (b[i] != '0') + min(dp[i + 1][1] + (a[i + 1] != '1'), dp[i + 1][0] + (a[i + 1] != '0'));
dp[i][1] = (b[i] != '1') + min(dp[i + 1][0] + (a[i + 1] != '1'), dp[i + 1][1] + (a[i + 1] != '0'));
}
cout << min(dp[0][0] + (a[0] != '0'), dp[0][1] + (a[0] != '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,144 @@
/* Problem URL: https://codeforces.com/problemset/problem/1984/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;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
cin >> n;
vl a(n);
cin >> a;
const ll mod = 998244353;
map<ll, ll> c1;
map<ll, ll> c2;
c1[0] = 1;
rep(i, n) {
if (c1.size() == 1) {
auto itr = c1.begin();
c2[itr->first + a[i]] += itr->second;
c2[itr->first + a[i]] %= mod;
c2[abs(itr->first + a[i])] += itr->second;
c2[abs(itr->first + a[i])] %= mod;
swap(c1, c2);
c2.clear();
continue;
}
auto itr1 = c1.begin();
auto itr2 = prev(c1.end());
c2[itr1->first + a[i]] += itr1->second;
c2[itr1->first + a[i]] %= mod;
c2[abs(itr1->first + a[i])] += itr1->second;
c2[abs(itr1->first + a[i])] %= mod;
c2[itr2->first + a[i]] += itr2->second;
c2[itr2->first + a[i]] %= mod;
c2[abs(itr2->first + a[i])] += itr2->second;
c2[abs(itr2->first + a[i])] %= mod;
swap(c1, c2);
c2.clear();
}
cout << prev(c1.end())->second << '\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,172 @@
/* Problem URL: https://codeforces.com/problemset/problem/2071/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, st, en;
cin >> n >> st >> en;
st--, en--;
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 ne(n, -1);
function<bool(int, int)> find_p = [&](int i, int p) {
if (i == en) {
return true;
}
repv(j, graph[i]) {
if (j == p) {
continue;
}
if (find_p(j, i)) {
ne[i] = j;
return true;
}
}
return false;
};
find_p(st, -1);
vi ans;
function<void(int, int)> rem = [&](int i, int p) {
repv(j, graph[i]) {
if (j == p) {
continue;
}
rem(j, i);
}
ans.push_back(i + 1);
};
function<void(int, int)> getans = [&](int i, int p) {
repv(j, graph[i]) {
if (j == p || j == ne[i]) {
continue;
}
rem(j, i);
}
ans.push_back(i + 1);
if (i != en) {
getans(ne[i], i);
}
};
getans(st, -1);
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,126 @@
/* Problem URL: https://codeforces.com/problemset/problem/2190/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;
string s;
cin >> s;
int ind = 0;
while (ind < s.size() - 1 && (s[ind] == '(' || s[ind + 1] == ')')) {
ind++;
}
int ne = ind + 2;
while (ne < s.size() && s[ne] != '(') {
ne++;
}
if (ne >= s.size()) {
cout << "-1\n";
return;
}
cout << n - 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,127 @@
/* Problem URL: https://codeforces.com/contest/2197/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 x;
cin >> x;
auto calc = [&](ll a) {
ll sum = 0;
ll tmp = a;
while (tmp > 0) {
sum += tmp % 10;
tmp /= 10;
}
return a - sum;
};
int ans = 0;
rep(i, 1000) {
if (calc(x + i) == x) {
ans++;
}
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,132 @@
/* Problem URL: https://codeforces.com/contest/2197/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;
cin >> n;
vi a(n);
vi b(n);
cin >> a >> b;
vi pos(n);
rep(i, n) {
pos[a[i] - 1] = i;
}
vi need(n);
rep(i, n) {
need[i] = pos[b[i] - 1];
}
int mi = oo;
for (int i = n - 1; i >= 0; i--) {
if (need[i] > mi) {
cout << "NO\n";
return;
}
rmin(mi, need[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,120 @@
/* Problem URL: https://codeforces.com/contest/2197/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;
void pre()
{
}
#define TEST 1
void solve()
{
ll p, q;
cin >> p >> q;
if (p >= q) {
cout << "Alice\n";
return;
}
ll diff = q - p;
if (p >= 2 * diff && q >= 3 * diff) {
cout << "Bob\n";
return;
}
cout << "Alice\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,123 @@
/* Problem URL: https://codeforces.com/contest/2202/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 x, y;
cin >> x >> y;
ll xlim = 0;
if (y > 0) {
xlim += y * 2;
}
if (y < 0) {
xlim += -y * 4;
}
if (xlim > x || (x - xlim) % 3) {
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();
}
}

View File

@@ -0,0 +1,143 @@
/* Problem URL: https://codeforces.com/contest/2202/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;
cin >> n;
string a;
cin >> a;
vvvi dp(n, vvi(2, vi(2)));
if (a.back() == 'a') {
dp.back()[0][0] = 1;
} else if (a.back() == 'b') {
dp.back()[1][1] = 1;
} else {
dp.back()[0][0] = 1;
dp.back()[1][1] = 1;
}
for (int i = n - 2; i >= 0; i--) {
rep(j, 4) {
int b1 = (j >> 1) & 1;
int b2 = j & 1;
if (a[i] == '?') {
dp[i][b1][b2] |= dp[i + 1][b1^1][b2];
dp[i][b1][b2] |= dp[i + 1][b1][b2^1];
continue;
}
int c = a[i] - 'a';
if (b1 == c) {
dp[i][b1][b2] |= dp[i + 1][b1^1][b2];
}
if (b2 == c) {
dp[i][b1][b2] |= dp[i + 1][b1][b2^1];
}
}
}
cout << (dp[0][0][~n & 1] ? "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,126 @@
/* Problem URL: https://codeforces.com/contest/2202/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;
}
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;
set<ll> t;
int c = 0;
rep(i, n) {
if (t.count(a[i])) {
c++;
while (*prev(t.end()) > a[i] + 1) {
t.erase(prev(t.end()));
}
} else {
t.clear();
}
t.insert(a[i] + 1);
}
cout << n - 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,118 @@
/* Problem URL: https://codeforces.com/contest/2205/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()
{
int n;
cin >> n;
vi p(n);
cin >> p;
rep(i, n) {
if (p[i] == n) {
swap(p[i], p[0]);
break;
}
}
cout << p;
}
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/contest/2205/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;
constexpr int MAXN = 1e5;
bool prime[MAXN];
vl primes;
void pre()
{
fill(prime, prime + MAXN, true);
prime[1] = false;
nrep(i, 2, MAXN) {
if (!prime[i]) {
continue;
}
primes.push_back(i);
for (int j = i * 2; j < MAXN; j += i) {
prime[j] = false;
}
}
}
#define TEST 1
void solve()
{
ll n;
cin >> n;
vl ans;
for (int i = 0; primes[i] * primes[i] <= n; i++) {
while (n % primes[i] == 0) {
n /= primes[i];
ans.push_back(primes[i]);
}
}
ans.push_back(n);
ans.erase(unique(all(ans)), ans.end());
ll act = 1;
repv(i, ans) {
act *= i;
}
cout << act << '\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,197 @@
/* Problem URL: https://codeforces.com/contest/2205/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;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
cin >> n;
vvi act(n);
rep(i, n) {
int m;
cin >> m;
act[i].resize(m);
cin >> act[i];
}
V<bool> used(1e6 + 1);
vi t(n);
iota(all(t), 0);
vi ans;
while (true) {
if (t.empty()) {
break;
}
vi ch = t;
vi tmp;
while (ch.size() > 1) {
int a = oo;
repv(j, ch) {
while (!act[j].empty() && used[act[j].back()]) {
act[j].pop_back();
}
if (act[j].empty()) {
a = oo;
break;
}
if (act[j].back() < a) {
tmp.clear();
tmp.push_back(j);
a = act[j].back();
continue;
}
if (act[j].back() == a) {
tmp.push_back(j);
}
}
if (a == oo) {
break;
}
used[a] = true;
ans.push_back(a);
swap(ch, tmp);
tmp.clear();
}
bool rem = false;
int cur = 0;
rep(i, t.size()) {
while (!act[t[i]].empty() && used[act[t[i]].back()]) {
act[t[i]].pop_back();
}
if (act[t[i]].empty()) {
rem = true;
continue;
}
t[cur] = t[i];
cur++;
}
while (t.size() > cur) {
t.pop_back();
}
if (rem) {
continue;
}
while (!act[ch[0]].empty()) {
int j = act[ch[0]].back();
act[ch[0]].pop_back();
if (used[j]) {
continue;
}
used[j] = true;
ans.push_back(j);
}
}
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,172 @@
/* Problem URL: https://codeforces.com/contest/2205/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;
vi p(n);
cin >> p;
vi pos(n);
rep(i, n) {
pos[p[i] - 1] = i;
}
vi lf(n, -1);
vi rg(n, n);
stack<pair<int, int>> s;
rep(i, n) {
while (!s.empty() && s.top().first < p[i]) {
s.pop();
}
if (!s.empty()) {
lf[i] = s.top().second;
}
s.emplace(p[i], i);
}
while (!s.empty()) {
s.pop();
}
for (int i = n - 1; i >= 0; i--) {
while (!s.empty() && s.top().first < p[i]) {
s.pop();
}
if (!s.empty()) {
rg[i] = s.top().second;
}
s.emplace(p[i], i);
}
vvi dp(n, vi(2));
int ans = oo;
for (int i = n - 1; i >= 0; i--) {
int ps = pos[i];
if (lf[ps] == -1) {
dp[i][0] = ps;
} else {
int dis = ps - lf[ps] - 1;
dp[i][0] = dis + dp[p[lf[ps]] - 1][0];
}
if (rg[ps] == n) {
dp[i][1] = n - ps - 1;
} else {
int dis = rg[ps] - ps - 1;
dp[i][1] = dis + dp[p[rg[ps]] - 1][1];
}
rmin(ans, dp[i][0] + dp[i][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,111 @@
/* Problem URL: https://codeforces.com/contest/2200/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()
{
int n;
cin >> n;
vi a(n);
cin >> a;
int b = *max_element(all(a));
cout << count(all(a), b) << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,118 @@
/* Problem URL: https://codeforces.com/contest/2200/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;
cin >> n;
vi a(n);
cin >> a;
nrep(i, 1, n) {
if (a[i] < a[i - 1]) {
cout << "1\n";
return;
}
}
cout << n << '\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/contest/2200/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;
void pre()
{
}
#define TEST 1
void solve()
{
int n;
string a;
cin >> n >> a;
if (n & 1) {
cout << "NO\n";
return;
}
vi c(n, 1);
for (int i = 1; i <= n; i += 2) {
vi pref = c;
nrep(j, 1, n) {
pref[j] += pref[j - 1];
}
for (int j = 0; j + i < n; j++) {
if (c[j] && c[j + i] && a[j] == a[j + i] && pref[j + i - 1] - pref[j] == 0) {
c[j] = 0;
c[j + i] = 0;
}
}
}
cout << (find(all(c), 1) == c.end() ? "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,150 @@
/* Problem URL: https://codeforces.com/contest/2200/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, x, y;
cin >> n >> x >> y;
vi p(n);
cin >> p;
vi act;
nrep(i, x, y) {
act.push_back(p[i]);
}
int pos = min_element(all(act)) - act.begin();
vi ans;
nrep(i, pos, act.size()) {
ans.push_back(act[i]);
}
rep(i, pos) {
ans.push_back(act[i]);
}
vi ot;
rep(i, x) {
ot.push_back(p[i]);
}
nrep(i, y, n) {
ot.push_back(p[i]);
}
if (ot.empty()) {
cout << ans;
return;
}
int i = 0;
for (; i < ot.size() && ot[i] < ans[0]; i++) {
cout << ot[i] << ' ';
}
repv(i, ans) {
cout << i << ' ';
}
for (; i < ot.size(); i++) {
cout << ot[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,156 @@
/* Problem URL: https://codeforces.com/contest/2200/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;
constexpr int MAXN = 1e6 + 1;
int d[MAXN];
void pre()
{
fill(d, d + MAXN, 1);
nrep(i, 1, MAXN) {
if (d[i] != 1) {
continue;
}
for (int j = i; j < MAXN; j += i) {
d[j] = i;
}
}
}
#define TEST 1
void solve()
{
int n;
cin >> n;
vi a(n);
cin >> a;
bool pos = false;
nrep(i, 1, n) {
if (a[i] < a[i - 1]) {
pos = true;
break;
}
}
if (!pos) {
cout << "Bob\n";
return;
}
vi act;
repv(i, a) {
if (i == 1) {
act.push_back(i);
continue;
}
while (i > 1) {
act.push_back(d[i]);
i /= d[i];
}
}
nrep(i, 1, act.size()) {
if (act[i] < act[i - 1]) {
cout << "Alice\n";
return;
}
}
cout << "Bob\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,148 @@
/* Problem URL: https://codeforces.com/contest/2200/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, m;
cin >> n >> m;
vvi par(n + 1);
rep(i, n) {
int x, y;
cin >> x >> y;
par[y].push_back(x);
}
ll ans = 0;
vvl b(n + 1, vl(2));
multiset<ll> s;
ll tot = 0;
for (int i = n; i >= 0; i--) {
repv(j, par[i]) {
s.insert(j);
tot += j;
}
while (s.size() > i + 1) {
tot -= *s.begin();
s.erase(s.begin());
}
b[i][0] = tot;
b[i][1] = tot - *s.begin() * (s.size() == i + 1);
rmax(ans, b[i][0]);
}
vl ma(n + 1);
ma[0] = 0;
nrep(i, 1, n + 1) {
ma[i] = max(ma[i - 1], b[i][1]);
}
while (m--) {
ll x, y;
cin >> x >> y;
cout << max({b[y][0], ma[y] + x, ans}) << " \n"[m == 0];
}
}
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/contest/2207/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()
{
int n;
cin >> n;
string s;
cin >> s;
nrep(i, 1, n - 1) {
if (s[i - 1] == '1' && s[i + 1] == '1') {
s[i] = '1';
}
}
int ans1 = 0;
int ans2 = 0;
int c = 0;
rep(i, n) {
if (s[i] == '1') {
c++;
continue;
}
ans1 += c - (c - 1) / 2;
ans2 += c;
c = 0;
}
ans1 += c - (c - 1) / 2;
ans2 += c;
cout << ans1 << ' ' << ans2 << '\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/contest/2207/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, m, l;
cin >> n >> m >> l;
vi rem(l);
rep(i, n) {
int a;
cin >> a;
rem[a - 1] = 1;
}
multiset<int> pq;
rep(i, min(n + 1, m)) {
pq.insert(0);
}
int c = n;
rep(i, l) {
while (pq.size() > c + 1) {
pq.erase(pq.begin());
}
auto itr = pq.begin();
int val = *itr;
pq.erase(itr);
pq.insert(val + 1);
if (rem[i]) {
pq.erase(prev(pq.end()));
pq.insert(0);
c--;
}
}
cout << *prev(pq.end()) << '\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,106 @@
/* Problem URL: https://codeforces.com/contest/2207/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;
void pre()
{
}
#define TEST 1
void solve()
{
}
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,168 @@
/* Problem URL: https://codeforces.com/problemset/problem/1808/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 vvvvvi = vector<vvvvi>;
using vvvvvvi = vector<vvvvvi>;
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 a, b;
cin >> a >> b;
string zer(b.size() - a.size(), '0');
a = zer + a;
int n = a.size();
rep(i, n) {
a[i] -= '0';
b[i] -= '0';
}
vvvvvvi memo(n, vvvvvi(2, vvvvi(2, vvvi(2, vvi(10, vi(10, -1))))));
function<int(int, int, int, int, int, int)> dp = [&](int i, int u, int o, int s, int mi, int ma){
if (i >= n) {
return ma - mi;
}
int &ans = memo[i][u][o][s][mi][ma];
if (ans != -1) {
return ans;
}
ans = oo;
for (int j = o ? 0 : a[i]; j <= (u ? 9 : b[i]); j++) {
rmin(ans, dp(i + 1, u || j < b[i], o || j > a[i], s || j != 0, s || j != 0 ? min(mi, j) : 9, s || j != 0 ? max(ma, j) : 0));
}
return ans;
};
int target = dp(0, 0, 0, 0, 9, 0);
string ans;
function<void(int, int, int, int, int, int)> rec = [&](int i, int u, int o, int s, int mi, int ma) {
for (int j = o ? 0 : a[i]; j <= (u ? 9 : b[i]); j++) {
bool nu = u || j < b[i];
bool no = o || j > a[i];
bool ns = s || j != 0;
int nmi = ns ? min(mi, j) : 9;
int nma = ns ? max(ma, j) : 0;
if (i == n - 1) {
if (nma - nmi == target) {
ans += j + '0';
return;
}
continue;
}
if (memo[i + 1][nu][no][ns][nmi][nma] == target) {
ans += j + '0';
rec(i + 1, nu, no, ns, nmi, nma);
return;
}
}
};
rec(0, 0, 0, 0, 9, 0);
cout << stoll(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,143 @@
/* Problem URL: https://codeforces.com/problemset/problem/1797/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, m;
cin >> n >> m;
auto ask = [&](int i, int j) {
cout << "? " << i << ' ' << j << endl;
ll ans;
cin >> ans;
return ans;
};
auto answer = [&](int i, int j) {
cout << "! " << i << ' ' << j << endl;
return;
};
ll c1 = ask(1, 1);
if (c1 >= n) {
ll c2 = ask(1, c1 + 1);
answer(c2 + 1, c1 + 1);
return;
}
ll c2 = ask(c1 + 1, 1);
if (c1 != c2) {
ll c3 = ask(c1 + 1, c2 + 1);
if (c3 == 0) {
answer(c1 + 1, c2 + 1);
return;
}
answer(n - c2, c1 + 1);
return;
}
ll c3 = ask(1, c1 + 1);
answer(c3 + 1, c1 + 1);
}
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/problemset/problem/1856/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 k;
cin >> n >> k;
vl a(n);
cin >> a;
ll ans = *max_element(all(a));
rep(i, n - 1) {
ll co = 0;
nrep(j, i + 1, n) {
if (a[j] < a[i] - (j - i - 1)) {
co += a[i] - (j - i - 1) - a[j];
if (co >= k) {
break;
}
continue;
}
if (co == k) {
break;
}
ll lk = k - co;
ll low = 0;
ll high = a[j] - a[i] + j - i - 1;
ll act = 0;
while (low <= high) {
ll mid = (low + high) >> 1;
ll cost = mid * (j - i);
if (cost < lk) {
low = mid + 1;
act = mid;
continue;
}
high = mid - 1;
}
rmax(ans, a[i] + act + 1);
a[i] += act;
co += act * (j - i);
if (a[j] < a[i] - (j - i - 1)) {
co += a[i] - (j - i - 1) - a[j];
if (co >= k) {
break;
}
}
}
}
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,158 @@
/* Problem URL: https://codeforces.com/problemset/problem/1858/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, m, d;
cin >> n >> m >> d;
vi t(m);
cin >> t;
vl suf(m);
suf[m - 1] = (n - t[m - 1]) / d + 1;
for (int i = m - 2; i >= 0; i--) {
suf[i] = suf[i + 1] + (t[i + 1] - t[i] - 1) / d + 1;
}
ll ans = OO;
int c = 1;
ll pref = 0;
rep(i, m) {
if (i == 0) {
ll tot = (t[i + 1] - 2) / d + suf[1] + 1;
ans = tot;
pref += max((t[i] - 2) / d, 0) + 1 + (t[0] != 1);
continue;
}
if (i == m - 1) {
ll tot = (n - t[i - 1]) / d + pref;
if (tot < ans) {
ans = tot;
c = 1;
continue;
}
if (tot == ans) {
c++;
}
continue;
}
ll tot = (t[i + 1] - t[i - 1] - 1) / d + suf[i + 1] + pref;
if (tot < ans) {
ans = tot;
c = 1;
} else if (tot == ans) {
c++;
}
pref += (t[i] - t[i - 1] - 1) / d + 1;
}
cout << ans << ' ' << 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,135 @@
/* Problem URL: https://codeforces.com/problemset/problem/1873/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()
{
string s;
cin >> s;
int n = s.size();
if (n == 1) {
cout << "0\n";
return;
}
vi pos = {0};
rep(i, n) {
if (s[i] == 'B') {
pos.push_back(i + 1);
}
}
pos.push_back(n + 1);
if (pos.size() == 2) {
cout << "0\n";
return;
}
vvi dp(pos.size(), vi(2));
nrep(i, 1, pos.size() - 1) {
dp[i][0] = pos[i] - pos[i - 1] - 1 + dp[i - 1][0];
dp[i][1] = pos[i + 1] - pos[i] - 1 + max(dp[i - 1][0], dp[i - 1][1]);
}
cout << max(dp.end()[-2][0], dp.end()[-2][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,116 @@
/* Problem URL: https://codeforces.com/problemset/problem/1907/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()
{
ll n;
cin >> n;
ll ans = 1;
while (n > 0) {
ll now = n % 10;
n /= 10;
ans *= (now + 1) * (now + 2) / 2;
}
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,208 @@
/* Problem URL: https://codeforces.com/problemset/problem/1986/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, m;
cin >> n >> m;
V<V<pair<int, int>>> graph(n);
V<bool> rem(m);
while (m--) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].emplace_back(b, m);
graph[b].emplace_back(a, m);
}
V<bool> vis(n);
vi t(n);
vi mi(n);
int tim = 0;
int tt = 1;
function<int(int, int)> dfs = [&](int i, int p) {
vis[i] = true;
t[i] = tim++;
mi[i] = t[i];
for (auto [j, ind] : graph[i]) {
if (j == p) {
continue;
}
if (vis[j]) {
rmin(mi[i], t[j]);
continue;
}
rmin(mi[i], dfs(j, i));
if (mi[j] > t[i]) {
rem[ind] = true;
tt++;
}
}
return mi[i];
};
dfs(0, 0);
vl c(tt);
vi g(n);
vvi act(tt);
int cur = 0;
fill(all(vis), false);
function<void(int)> func = [&](int i) {
vis[i] = true;
g[i] = cur;
c[cur]++;
for (auto [j, ind] : graph[i]) {
if (vis[j] || rem[ind]) {
continue;
}
func(j);
}
};
rep(i, n) {
if (vis[i]) {
continue;
}
func(i);
cur++;
}
rep(i, n) {
for (auto [j, ind] : graph[i]) {
if (rem[ind]) {
act[g[i]].push_back(g[j]);
act[g[j]].push_back(g[i]);
}
}
}
repv(i, act) {
sortv(i);
i.erase(unique(all(i)), i.end());
}
ll ans = (ll)n * (n - 1) / 2;
function<ll(int, int)> calc = [&](int i, int p) {
ll count = c[i];
repv(j, act[i]) {
if (j == p) {
continue;
}
ll tm = calc(j, i);
rmin(ans, tm * (tm - 1) / 2 + (n - tm) * (n - tm - 1) / 2);
count += tm;
}
return count;
};
calc(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,161 @@
/* Problem URL: https://codeforces.com/problemset/problem/1996/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;
ll k;
cin >> n >> k;
vl a(n);
vl b(n);
cin >> a >> b;
ll low = 0;
ll high = 1e9;
ll act = 1e9;
while (low <= high) {
ll mid = (low + high) >> 1;
ll cos = 0;
rep(i, n) {
cos += max((a[i] - mid + b[i] - 1) / b[i], 0LL);
}
if (cos <= k) {
act = mid;
high = mid - 1;
continue;
}
low = mid + 1;
}
ll ans = 0;
ll cos = 0;
priority_queue<pair<ll, ll>> pq;
rep(i, n) {
ll t = max((a[i] - act + b[i] - 1) / b[i], 0LL);
cos += t;
ll mod = a[i] % b[i];
ll add = t * (a[i] / b[i] * 2 - t + 1) / 2 * b[i] + mod * t;
a[i] -= b[i] * t;
ans += add;
pq.emplace(max(a[i], 0LL), b[i]);
}
k -= cos;
while (k > 0) {
auto [x, y] = pq.top();
if (x == 0) {
break;
}
pq.pop();
ans += x;
pq.emplace(max(x - y, 0LL), y);
k--;
}
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,142 @@
/* Problem URL: https://codeforces.com/problemset/problem/1993/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;
vl a(n);
cin >> a;
ll mod = k << 1;
vl pref((mod << 1) + 1);
ll ma = 0;
repv(i, a) {
pref[i % mod]++;
pref[i % mod + k]--;
rmax(ma, i);
}
nrep(i, 1, (mod << 1) + 1) {
pref[i] += pref[i - 1];
}
int act = 0;
nrep(i, ma % mod, (mod << 1)) {
ll sum = 0;
if (i >= mod) {
sum = pref[i] + pref[i - mod];
} else {
sum = pref[i] + pref[i + mod];
}
if (sum == n) {
cout << ma + i - ma % mod << '\n';
return;
}
}
cout << "-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,132 @@
/* Problem URL: https://codeforces.com/problemset/problem/2009/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()
{
ll n, k;
cin >> n >> k;
auto calc = [&](ll l, ll r) {
return (r - l + 1) * (l + r) / 2;
};
ll low = k;
ll high = k + n - 2;
ll ans = k;
while (low <= high) {
ll mid = (low + high) / 2;
ll f = calc(k, mid);
ll s = calc(mid + 1, k + n - 1);
ll a = f - s;
if (a <= 0) {
ans = mid;
low = mid + 1;
continue;
}
high = mid - 1;
}
cout << min(abs(calc(k, ans) - calc(ans + 1, k + n - 1)), abs(calc(k, ans + 1) - calc(ans + 2, k + 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,163 @@
/* Problem URL: https://codeforces.com/problemset/problem/2013/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;
auto ask = [&](string a) {
cout << "? " << a << endl;
int ans;
cin >> ans;
return ans;
};
auto answer = [&](string a) {
cout << "! " << a << endl;
};
if (n == 1) {
bool t = ask("0");
if (t) {
answer("0");
} else {
answer("1");
}
return;
}
string ans;
if (ask("0")) {
ans = "0";
} else {
ans = "1";
}
bool back = false;
rep(i, n - 1) {
if (back) {
if (ask("0" + ans)) {
ans = "0" + ans;
continue;
}
ans = "1" + ans;
continue;
}
if (ask(ans + "0")) {
ans += "0";
continue;
}
if (ask(ans + "1")) {
ans += "1";
continue;
}
back = true;
i--;
}
answer(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,127 @@
/* Problem URL: https://codeforces.com/problemset/problem/2033/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()
{
ll n, k;
cin >> n >> k;
const ll mod = 1e9 + 7;
n %= mod;
if (k == 1) {
cout << n << '\n';
return;
}
ll cur = 2;
ll a = 1;
ll b = 1;
while (b % k != 0) {
ll c = b;
b = (a + b) % k;
a = c;
cur++;
}
cout << cur * n % mod << '\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,169 @@
/* Problem URL: https://codeforces.com/problemset/problem/2044/G1 */
#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 p(n);
repv(i, p) {
cin >> i;
i--;
}
V<bool> vis(n);
V<bool> loop(n);
function<void(int)> getloop = [&](int i) {
vis[i] = true;
int t = p[i];
int h = p[t];
while (t != h) {
if (vis[t]) {
return;
}
vis[t] = true;
t = p[t];
h = p[p[h]];
}
vis[t] = true;
loop[t] = true;
h = p[h];
while (h != t) {
loop[h] = true;
vis[h] = true;
h = p[h];
}
};
rep(i, n) {
if (vis[i]) {
continue;
}
getloop(i);
}
vi ans(n, -1);
function<int(int)> dfs = [&](int i) {
if (loop[i]) {
return ans[i] = 0;
}
if (ans[i] != -1) {
return ans[i];
}
return ans[i] = dfs(p[i]) + 1;
};
rep(i, n) {
dfs(i);
}
cout << *max_element(all(ans)) + 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,127 @@
/* Problem URL: https://codeforces.com/problemset/problem/1792/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;
vi p(n);
cin >> p;
vi pos(n);
rep(i, n) {
pos[p[i] - 1] = i;
}
int ans = n - 1;
int l = 0;
int ma = pos[0];
nrep(i, 1, n) {
if (pos[i] < pos[i - 1]) {
l = i;
}
rmin(ans, max(l, n - i - 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,127 @@
/* Problem URL: https://codeforces.com/problemset/problem/1860/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;
vi p(n);
cin >> p;
int ans = 0;
vi lis;
rep(i, n) {
auto itr = lower_bound(all(lis), p[i]);
int pos;
if (itr == lis.end()) {
lis.push_back(p[i]);
pos = lis.size() - 1;
} else {
*itr = p[i];
pos = itr - lis.begin();
}
ans += pos == 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,163 @@
/* Problem URL: https://codeforces.com/problemset/problem/1895/C */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
int n;
cin >> n;
V<string> ev;
V<string> od;
rep(i, n) {
string a;
cin >> a;
if (a.size() & 1) {
od.emplace_back(a);
} else {
ev.emplace_back(a);
}
}
auto scmp = [&](string &a, string &b) {
return a.size() < b.size();
};
sort(all(ev), scmp);
sort(all(od), scmp);
auto calc = [&](V<string> &a) {
ll ans = 0;
V<map<int, int>> c(6);
repv(i, a) {
nrep(j, 1, min(6, (int)i.size() + 1)) {
int lim = (i.size() + j) >> 1;
ll cur = 0;
rep(k, lim - j) {
cur -= i[k] - '0';
}
nrep(k, lim - j, i.size()) {
cur += i[k] - '0';
}
ans += c[j][cur];
cur = 0;
rep(k, lim) {
cur += i[k] - '0';
}
nrep(k, lim, i.size()) {
cur -= i[k] - '0';
}
ans += c[j][cur];
}
c[i.size()][accumulate(all(i), 0) - '0' * i.size()]++;
}
return ans;
};
cout << calc(ev) + calc(od) << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,167 @@
/* Problem URL: https://codeforces.com/problemset/problem/1976/C */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 1
void solve()
{
int n, m;
cin >> n >> m;
int lim[] = {n, m};
V<array<int, 2>> a(n + m + 1);
vi team(n + m + 1);
rep(i, n + m + 1) {
cin >> a[i][0];
}
rep(i, n + m + 1) {
cin >> a[i][1];
team[i] = a[i][1] > a[i][0];
}
vi pref0(n + m + 2);
vi pref1(n + m + 2);
nrep(i, 1, n + m + 2) {
pref0[i] = pref0[i - 1] + (team[i - 1] == 0);
pref1[i] = pref1[i - 1] + (team[i - 1] == 1);
}
int lim0 = (int)(upper_bound(all(pref0), n) - pref0.begin()) - 1;
int lim1 = (int)(upper_bound(all(pref1), m) - pref1.begin()) - 1;
int lims[] = {lim0, lim1};
ll cou[] = {0, 0};
ll ans = 0;
for (int i = n + m; i >= 0; i--) {
if (i > lims[team[i]]) {
ans += a[i][team[i]^1];
continue;
}
if (i == lims[team[i]]) {
cou[team[i]] = a[i][team[i]];
cou[team[i]^1] = a[i][team[i]^1];
continue;
}
ans += a[i][team[i]];
}
rep(i, n + m + 1) {
if (i < lims[team[i]]) {
cout << ans - a[i][team[i]] + cou[team[i]] << ' ';
continue;
}
if (i == lims[team[i]]) {
cout << ans << ' ';
ans += a[i][team[i]^1];
cou[0] = 0;
cou[1] = 0;
continue;
}
cout << ans - a[i][team[i]^1] << ' ';
}
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,147 @@
/* Problem URL: https://codeforces.com/problemset/problem/2104/E */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
int n, k;
cin >> n >> k;
string a;
cin >> a;
vi dp(n, oo);
vvi ne(n, vi(k, -1));
vi pr(k, -1);
for (int i = n - 1; i >= 0; i--) {
ne[i] = pr;
pr[a[i] - 'a'] = i;
rep(j, k) {
if (ne[i][j] == -1) {
dp[i] = 0;
break;
}
rmin(dp[i], dp[ne[i][j]] + 1);
}
}
int q;
cin >> q;
while (q--) {
string s;
cin >> s;
int now = pr[s[0] - 'a'];
nrep(i, 1, s.size()) {
if (now == -1) {
break;
}
now = ne[now][s[i] - 'a'];
}
cout << (now == -1 ? 0 : 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,180 @@
/* Problem URL: https://codeforces.com/problemset/problem/2112/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);
}
V<pair<int, int>> edges;
pair<int, int> c1;
pair<int, int> c2;
int dc = -1;
function<bool(int, int, int)> dfs = [&](int i, int p, int d) {
bool valid = false;
repv(j, graph[i]) {
if (j == p) {
continue;
}
valid = valid || graph[i].size() >= 2;
if (d & 1) {
edges.emplace_back(j + 1, i + 1);
} else {
edges.emplace_back(i + 1, j + 1);
}
if (dfs(j, i, d + 1 - valid) && dc == -1) {
dc = d;
c1 = {i + 1, j + 1};
c2 = {j + 1, graph[j][0] == i ? graph[j][1] + 1 : graph[j][0] + 1};
// edges.pop_back();
edges.pop_back();
edges.pop_back();
}
}
return valid;
};
dfs(0, 0, 0);
if (dc == -1) {
edges.clear();
dfs(n - 1, n - 1, 0);
if (dc == -1) {
edges.clear();
dfs(n - 2, n - 2, 0);
if (dc == -1) {
edges.clear();
dfs(1, 1, 0);
if (dc == -1) {
cout << "NO\n";
return;
}
}
}
}
cout << "YES\n";
if (dc & 1) {
repv(i, edges) {
swap(i.first, i.second);
}
}
repv(i, edges) {
cout << i.first << ' ' << i.second << '\n';
}
cout << c1.first << ' ' << c1.second << '\n';
cout << c2.first << ' ' << c2.second << '\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,111 @@
/* Problem URL: https://codeforces.com/contest/2203/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()
{
int n, m, d;
cin >> n >> m >> d;
int f = d / m + 1;
cout << (n + f - 1) / f << '\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,119 @@
/* Problem URL: https://codeforces.com/contest/2203/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()
{
string a;
cin >> a;
ll tmp = accumulate(all(a), 0) - '0' * a.size();
a[0]--;
sortv(a);
int ans = 0;
while (tmp >= 10) {
tmp -= a.back() - '0';
a.pop_back();
ans++;
}
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,133 @@
/* Problem URL: https://codeforces.com/contest/2203/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;
void pre()
{
}
#define TEST 1
void solve()
{
ll s, m;
cin >> s >> m;
ll low = 1;
ll high = OO;
ll ans = -1;
while (low <= high) {
ll mid = (high - low) / 2 + low;
ll n = s;
for (int i = 62; i >= 0; i--) {
if (((m >> i) & 1) == 0) {
continue;
}
n -= min(n >> i, mid) << i;
}
if (n == 0) {
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,176 @@
/* Problem URL: https://codeforces.com/contest/2203/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;
// constexpr int MAXN = 2e6 + 10;
// vi divs[MAXN];
void pre()
{
// nrep(i, 1, MAXN) {
// for (int j = i; j < MAXN; j += i) {
// divs[j].push_back(i);
// }
// }
}
#define TEST 1
void solve()
{
int n, m;
cin >> n >> m;
vi a(n);
cin >> a;
sortv(a);
a.erase(unique(all(a)), a.end());
vi c(n + m + 1);
repv(i, a) {
for (int j = i; j <= n + m; j += i) {
c[j]++;
}
}
int p[3] = {0, 0, 0};
vi t(n + m + 1, -1);
rep(i, m) {
int now;
cin >> now;
if (t[now] != -1) {
p[t[now]]++;
continue;
}
int co = c[now];
if (co > 0 && a.size() - co > 0) {
p[2]++;
t[now] = 2;
continue;
}
if (co > 0) {
p[0]++;
t[now] = 0;
continue;
}
p[1]++;
t[now] = 1;
}
string win[] = {"Alice", "Bob"};
int now = 0;
while (true) {
if (p[2]) {
p[2]--;
now ^= 1;
continue;
}
if (p[now]) {
p[now]--;
now ^= 1;
continue;
}
cout << win[now^1] << '\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,256 @@
/* Problem URL: https://codeforces.com/problemset/problem/2029/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, m;
cin >> n >> m;
vvi graph(n);
while (m--) {
int a, b;
cin >> a >> b;
a--, b--;
graph[a].push_back(b);
graph[b].push_back(a);
}
V<bool> vis(n);
V<bool> frame(n);
vi cur(n);
set<pair<int, int>> rem;
vvi loops;
int end = -1;
function<bool(int, int)> dfs = [&](int i, int p) {
vis[i] = true;
frame[i] = true;
for (int &k = cur[i]; k < graph[i].size(); k++) {
int j = graph[i][k];
if (j == p) {
continue;
}
if (rem.count({i, j})) {
continue;
}
if (frame[j]) {
end = j;
loops.emplace_back();
loops.back().push_back(i);
frame[i] = false;
vis[i] = false;
rem.emplace(i, j);
rem.emplace(j, i);
return true;
}
if (vis[j]) {
continue;
}
if (dfs(j, i)) {
loops.back().push_back(i);
rem.emplace(i, j);
rem.emplace(j, i);
if (end != i) {
frame[i] = false;
vis[i] = false;
return true;
}
}
}
frame[i] = false;
return false;
};
rep(i, n) {
if (vis[i]) {
continue;
}
dfs(i, i);
}
int c = 0;
V<tuple<int, int, int>> ans;
repv(j, loops) {
nrep(i, 2, j.size()) {
ans.emplace_back(j[0] + 1, j[i - 1] + 1, j[i] + 1);
}
}
auto printans = [&]() {
cout << ans.size() << '\n';
for (auto [i, j, k] : ans) {
cout << i << ' ' << j << ' ' << k << '\n';
}
};
vvi act(n);
rep(i, n) {
repv(j, graph[i]) {
if (rem.count({i, j})) {
continue;
}
act[i].push_back(j);
c++;
}
}
if (c == 0) {
printans();
return;
}
fill(all(vis), false);
vi g(n);
int gg = 0;
set<int> ev;
function<void(int)> func = [&](int i) {
vis[i] = true;
g[i] = gg;
repv(j, act[i]) {
if (vis[j]) {
continue;
}
func(j);
}
};
rep(i, n) {
if (vis[i]) {
continue;
}
func(i);
gg++;
}
int a1 = -1;
int a2 = -1;
rep(i, n) {
if (act[i].size() > 0) {
a1 = i;
a2 = act[i][0];
ev.insert(g[i]);
break;
}
}
rep(i, n) {
if (!ev.count(g[i])) {
ans.emplace_back(a1 + 1, a2 + 1, i + 1);
ev.insert(g[i]);
a1 = i;
}
}
printans();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -44,6 +44,10 @@ Official divs from codeforces
- [ ] [D1. Removal of a Sequence (Easy Version)](https://codeforces.com/contest/2169/problem/D1)
A josephus problem, I'm gonna need to remember how it works to maka this.
- [ ] [D. Reachability and Tree](https://codeforces.com/problemset/problem/2112/D)
I know how to solve it, but I got bored in the middle, I'll get back to it eventually.
- Global

View File

@@ -28,7 +28,7 @@ A little collection of interesting problems that I randomly decided to do.
Another interesting sparse table problem, I'm not sure it needs one, but I solved with
one.
- [D. R2D2 and Droid Army](https://codeforces.com/problemset/problem/514/D)
- [D. R3D2 and Droid Army](https://codeforces.com/problemset/problem/514/D)
Another sparse table problem.
@@ -118,3 +118,9 @@ A little collection of interesting problems that I randomly decided to do.
- [https://codeforces.com/problemset/problem/1985/H1](https://codeforces.com/problemset/problem/1985/H1)
DSU with rollback problem, cool to think about and not really that difficult.
## Bridge
- [F. Non-academic Problem](https://codeforces.com/problemset/problem/1986/F)
Bridge to compress the graph into a tree.