Compare commits
15 Commits
97ebdbf890
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| e9cafa367b | |||
| 14deae4d42 | |||
| 07afa2b2a8 | |||
| ccd62bc6e9 | |||
| 23630c1356 | |||
| 2d314df46f | |||
| 41d9ca1dc8 | |||
| 43ced801b4 | |||
| 3174264022 | |||
| acabee731f | |||
| c2a0f3e0db | |||
| eccccddcc8 | |||
| 05414bf7f7 | |||
| 28bb38c1f8 | |||
| c4acd23d19 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,3 +2,5 @@
|
||||
!/*/
|
||||
!*.cpp
|
||||
!TODO.md
|
||||
!problemlist.md
|
||||
*sync-conflict*
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
/* Problem URL: https://codeforces.com/gym/102114/problem/G */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
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;
|
||||
unsigned x, y, z;
|
||||
cin >> n >> m >> x >> y >> z;
|
||||
|
||||
auto rng = [&]() {
|
||||
x = x ^ (x << 11);
|
||||
x = x ^ (x >> 4);
|
||||
x = x ^ (x << 5);
|
||||
x = x ^ (x >> 14);
|
||||
unsigned w = x ^ y ^ z;
|
||||
x = y;
|
||||
y = z;
|
||||
z = w;
|
||||
return z;
|
||||
};
|
||||
|
||||
vvl sparse(20, vl(n));
|
||||
|
||||
while (m--) {
|
||||
ll rng1 = rng();
|
||||
ll rng2 = rng();
|
||||
ll rng3 = rng();
|
||||
|
||||
int l = min(rng2 % n + 1, rng1 % n + 1);
|
||||
int r = max(rng2 % n + 1, rng1 % n + 1);
|
||||
ll val = rng3 & ((1LL << 30) - 1);
|
||||
l--, r--;
|
||||
|
||||
int log = 31 - __builtin_clz(r - l + 1);
|
||||
|
||||
rmax(sparse[log][l], val);
|
||||
rmax(sparse[log][r - (1 << log) + 1], val);
|
||||
}
|
||||
|
||||
for (int j = 19; j > 0; j--) {
|
||||
for (int i = 0; i + (1 << (j - 1)) < n; i++) {
|
||||
rmax(sparse[j - 1][i], sparse[j][i]);
|
||||
rmax(sparse[j - 1][i + (1 << (j - 1))], sparse[j][i]);
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
ans ^= (i + 1) * sparse[0][i];
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,302 @@
|
||||
/* 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;
|
||||
}
|
||||
|
||||
ll operator ^ (pt b) {
|
||||
return x * b.y - y * b.x;
|
||||
}
|
||||
|
||||
ll operator * (pt b) {
|
||||
return x * b.x + y * b.y;
|
||||
}
|
||||
|
||||
pt operator - (pt b) {
|
||||
return pt(x - b.x, y - b.y);
|
||||
}
|
||||
};
|
||||
|
||||
int ccw(pt a, pt b, pt c)
|
||||
{
|
||||
ll r = (b - a) ^ (c - a);
|
||||
return (r > 0) - (r < 0);
|
||||
}
|
||||
|
||||
ll cdot(pt a, pt b, pt c)
|
||||
{
|
||||
return (b - a) * (c - a);
|
||||
}
|
||||
|
||||
int quad(pt a)
|
||||
{
|
||||
if (a.x == 0 && a.y > 0) {
|
||||
return 0;
|
||||
}
|
||||
if (a.x > 0 && a.y == 0) {
|
||||
return 1;
|
||||
}
|
||||
if (a.x == 0 && a.y < 0) {
|
||||
return 2;
|
||||
}
|
||||
if (a.x < 0 && a.y == 0) {
|
||||
return 3;
|
||||
}
|
||||
static const 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;
|
||||
|
||||
vi p(n + q);
|
||||
iota(all(p), 0);
|
||||
|
||||
auto cmpr = [&](vi &p, int i) {
|
||||
if (p.empty()) {
|
||||
return vector<pair<int, ll>>();
|
||||
}
|
||||
sort(all(p), [&](int j, int k){return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);});
|
||||
V<pair<int, ll>> v;
|
||||
int c = 1;
|
||||
nrep(j, 1, p.size()) {
|
||||
if (ccw(pts[i], pts[p[j]], pts[p[j - 1]]) == 0) {
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
v.emplace_back(p[j - 1], c);
|
||||
c = 1;
|
||||
}
|
||||
v.emplace_back(p.back(), c);
|
||||
return v;
|
||||
};
|
||||
|
||||
vl ans(q);
|
||||
|
||||
rep(i, n) {
|
||||
vvi qp(4);
|
||||
nrep(j, n, n + q) {
|
||||
qp[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
vvi np(4);
|
||||
rep(j, n) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
np[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
V<V<pair<int, ll>>> cp = {cmpr(np[0], i), cmpr(np[1], i), cmpr(np[2], i), cmpr(np[3], i)};
|
||||
auto cmp = [&](int j, int k) {
|
||||
return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);
|
||||
};
|
||||
sort(all(qp[0]), cmp);
|
||||
sort(all(qp[1]), cmp);
|
||||
sort(all(qp[2]), cmp);
|
||||
sort(all(qp[3]), cmp);
|
||||
|
||||
auto getcw = [&](vi &p, V<pair<int, ll>> &c) {
|
||||
int j = 0;
|
||||
int k = 0;
|
||||
while (j < p.size() && k < c.size()) {
|
||||
ll dot = cdot(pts[i], pts[p[j]], pts[c[k].first]);
|
||||
if (dot == 0) {
|
||||
ans[p[j] - n] += c[k].second;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot > 0) {
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
};
|
||||
|
||||
auto getccw = [&](vi &p, V<pair<int, ll>> &c) {
|
||||
int j = (int)p.size() - 1;
|
||||
int k = (int)c.size() - 1;
|
||||
while (j >= 0 && k >= 0) {
|
||||
ll dot = cdot(pts[i], pts[p[j]], pts[c[k].first]);
|
||||
if (dot == 0) {
|
||||
ans[p[j] - n] += c[k].second;
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot > 0) {
|
||||
k--;
|
||||
continue;
|
||||
}
|
||||
|
||||
j--;
|
||||
}
|
||||
};
|
||||
|
||||
rep(j, 4) {
|
||||
getcw(qp[j], cp[(j + 1) % 4]);
|
||||
getccw(qp[j], cp[((j - 1) % 4 + 4) % 4]);
|
||||
}
|
||||
}
|
||||
|
||||
nrep(i, n, n + q) {
|
||||
vvi np(4);
|
||||
rep(j, n) {
|
||||
np[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
V<V<pair<int, ll>>> cp = {cmpr(np[0], i), cmpr(np[1], i), cmpr(np[2], i), cmpr(np[3], i)};
|
||||
|
||||
auto getcw = [&](V<pair<int, ll>> &p1, V<pair<int, ll>> &p2) {
|
||||
int j = 0;
|
||||
int k = 0;
|
||||
|
||||
while (j < p1.size() && k < p2.size()) {
|
||||
ll dot = cdot(pts[i], pts[p1[j].first], pts[p2[k].first]);
|
||||
|
||||
if (dot == 0) {
|
||||
ans[i - n] += p1[j].second * p2[k].second;
|
||||
j++;
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot < 0) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
k++;
|
||||
}
|
||||
};
|
||||
|
||||
rep(j, 4) {
|
||||
getcw(cp[j], cp[(j + 1) % 4]);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
/* Problem URL: https://codeforces.com/gym/104020/problem/L?mobile=false */
|
||||
|
||||
#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
|
||||
|
||||
struct point {
|
||||
ll x;
|
||||
ll y;
|
||||
ll z;
|
||||
|
||||
friend istream &operator>>(istream &is, point &a) {
|
||||
is >> a.x >> a.y >> a.z;
|
||||
return is;
|
||||
}
|
||||
|
||||
bool operator<(point b) const {
|
||||
if (x == b.x) {
|
||||
if (y == b.y) {
|
||||
return z < b.z;
|
||||
}
|
||||
|
||||
return y < b.y;
|
||||
}
|
||||
|
||||
return x < b.x;
|
||||
}
|
||||
};
|
||||
|
||||
ll sqr(ll x)
|
||||
{
|
||||
return x * x;
|
||||
}
|
||||
|
||||
long double dis(point a, point b)
|
||||
{
|
||||
ll x = a.x - b.x;
|
||||
ll y = a.y - b.y;
|
||||
ll z = a.z - b.z;
|
||||
return sqrtl(sqr(x) + sqr(y) + sqr(z));
|
||||
}
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<point> p(n);
|
||||
cin >> p;
|
||||
|
||||
sortv(p);
|
||||
|
||||
long double ans = 1e16;
|
||||
|
||||
struct compy {
|
||||
bool operator()(point a, point b) const {
|
||||
if (a.y == b.y) {
|
||||
if (a.z == b.z) {
|
||||
return a.x > b.x;
|
||||
}
|
||||
|
||||
return a.z > b.z;
|
||||
}
|
||||
|
||||
return a.y > b.y;
|
||||
}
|
||||
};
|
||||
|
||||
struct compz {
|
||||
bool operator()(point a, point b) const {
|
||||
if (a.z == b.z) {
|
||||
if (a.x == b.x) {
|
||||
return a.y > b.y;
|
||||
}
|
||||
|
||||
return a.x > b.x;
|
||||
}
|
||||
|
||||
return a.z > b.z;
|
||||
}
|
||||
};
|
||||
|
||||
set<point, compy> sy;
|
||||
set<point, compz> sz;
|
||||
|
||||
sy.emplace(p[0]);
|
||||
sz.emplace(p[0]);
|
||||
|
||||
int cur = 0;
|
||||
|
||||
nrep(i, 1, n) {
|
||||
while (p[cur].x < p[i].x - ans) {
|
||||
sy.erase(p[cur]);
|
||||
sz.erase(p[cur]);
|
||||
cur++;
|
||||
}
|
||||
|
||||
auto itr = sy.lower_bound(point(1e9, p[i].y + ans, 1e9));
|
||||
while (itr != sy.end() && abs(p[i].y - itr->y) <= ans) {
|
||||
rmin(ans, dis(p[i], *itr));
|
||||
itr++;
|
||||
}
|
||||
|
||||
itr = sz.lower_bound(point(1e9, 1e9, p[i].z + ans));
|
||||
while (itr != sz.end() && abs(p[i].z - itr->z) <= ans) {
|
||||
rmin(ans, dis(p[i], *itr));
|
||||
itr++;
|
||||
}
|
||||
|
||||
sy.emplace(p[i]);
|
||||
sz.emplace(p[i]);
|
||||
}
|
||||
|
||||
cout << fixed << setprecision(7) << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2045/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
string s, t;
|
||||
cin >> s >> t;
|
||||
|
||||
int in = -1;
|
||||
int jn = -1;
|
||||
int ans = oo;
|
||||
|
||||
vi pos(26, -1);
|
||||
rep(i, t.size() - 1) {
|
||||
pos[t[i] - 'a'] = i;
|
||||
}
|
||||
|
||||
nrep(i, 1, s.size()) {
|
||||
int cur = s[i] - 'a';
|
||||
|
||||
if (pos[cur] == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int c = i + t.size() - pos[cur] + 1;
|
||||
if (c < ans) {
|
||||
ans = c;
|
||||
in = i;
|
||||
jn = pos[cur];
|
||||
}
|
||||
}
|
||||
|
||||
if (ans == oo) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
rep(i, in) {
|
||||
cout << s[i];
|
||||
}
|
||||
|
||||
nrep(j, jn, t.size()) {
|
||||
cout << t[j];
|
||||
}
|
||||
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2045/M */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int r, c;
|
||||
cin >> r >> c;
|
||||
|
||||
V<string> a(r);
|
||||
cin >> a;
|
||||
|
||||
auto getpos = [&](int i, int j, int k) {
|
||||
return i * c * 4 + j * 4 + k;
|
||||
};
|
||||
|
||||
int tot = 0;
|
||||
|
||||
vvi graph(r * c * 4);
|
||||
vi ty(r * c * 4, -1);
|
||||
rep(i, r) {
|
||||
rep(j, c) {
|
||||
int n = getpos(i, j, 0);
|
||||
int e = getpos(i, j, 1);
|
||||
int s = getpos(i, j, 2);
|
||||
int w = getpos(i, j, 3);
|
||||
|
||||
if (i > 0) {
|
||||
int ot = getpos(i - 1, j, 2);
|
||||
graph[n].push_back(ot);
|
||||
}
|
||||
|
||||
if (j > 0) {
|
||||
int ot = getpos(i, j - 1, 1);
|
||||
graph[w].push_back(ot);
|
||||
}
|
||||
|
||||
if (i < r - 1) {
|
||||
int ot = getpos(i + 1, j, 0);
|
||||
graph[s].push_back(ot);
|
||||
}
|
||||
|
||||
if (j < c - 1) {
|
||||
int ot = getpos(i, j + 1, 3);
|
||||
graph[e].push_back(ot);
|
||||
}
|
||||
|
||||
if (a[i][j] == '.') {
|
||||
graph[e].push_back(w);
|
||||
graph[w].push_back(e);
|
||||
graph[s].push_back(n);
|
||||
graph[n].push_back(s);
|
||||
continue;
|
||||
}
|
||||
tot++;
|
||||
ty[n] = i * c + j;
|
||||
ty[e] = i * c + j;
|
||||
ty[s] = i * c + j;
|
||||
ty[w] = i * c + j;
|
||||
|
||||
if (a[i][j] == '/') {
|
||||
graph[n].push_back(w);
|
||||
graph[w].push_back(n);
|
||||
graph[s].push_back(e);
|
||||
graph[e].push_back(s);
|
||||
continue;
|
||||
}
|
||||
|
||||
graph[n].push_back(e);
|
||||
graph[e].push_back(n);
|
||||
graph[w].push_back(s);
|
||||
graph[s].push_back(w);
|
||||
}
|
||||
}
|
||||
|
||||
V<bool> vis(r * c * 4);
|
||||
vi sz(r * c * 4, -1);
|
||||
|
||||
set<int> s;
|
||||
function<void(int)> dfs = [&](int i) {
|
||||
vis[i] = true;
|
||||
if (ty[i] != -1) {
|
||||
s.insert(ty[i]);
|
||||
}
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j);
|
||||
}
|
||||
};
|
||||
|
||||
function<void(int)> dfs2 = [&](int i) {
|
||||
sz[i] = s.size();
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (sz[j] != -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs2(j);
|
||||
}
|
||||
};
|
||||
|
||||
rep(i, r) {
|
||||
rep(j, c) {
|
||||
rep(k, 4) {
|
||||
int ac = getpos(i, j, k);
|
||||
if (vis[ac]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(ac);
|
||||
dfs2(ac);
|
||||
s.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
V<string> ans;
|
||||
rep(i, r) {
|
||||
int w = getpos(i, 0, 3);
|
||||
if (sz[w] == tot) {
|
||||
ans.emplace_back("W" + to_string(i + 1));
|
||||
}
|
||||
int e = getpos(i, c - 1, 1);
|
||||
if (sz[e] == tot) {
|
||||
ans.emplace_back("E" + to_string(i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, c) {
|
||||
int n = getpos(0, i, 0);
|
||||
if (sz[n] == tot) {
|
||||
ans.emplace_back("N" + to_string(i + 1));
|
||||
}
|
||||
int s = getpos(r - 1, i, 2);
|
||||
if (sz[s] == tot) {
|
||||
ans.emplace_back("S" + to_string(i + 1));
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
repv(i, ans) {
|
||||
cout << i << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2172/M */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m, k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
vi ans(k);
|
||||
|
||||
vi type(n);
|
||||
|
||||
rep(i, n) {
|
||||
int t;
|
||||
cin >> t;
|
||||
type[i] = t - 1;
|
||||
}
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
queue<int> q;
|
||||
vi dis(n, oo);
|
||||
dis[0] = 0;
|
||||
q.push(0);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto i = q.front();
|
||||
q.pop();
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (dis[j] > dis[i] + 1) {
|
||||
dis[j] = dis[i] + 1;
|
||||
q.push(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
rmax(ans[type[i]], dis[i]);
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2181/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;
|
||||
cin >> n >> m;
|
||||
|
||||
multiset<ll, greater<>> a[2];
|
||||
|
||||
rep(i, n) {
|
||||
ll b;
|
||||
cin >> b;
|
||||
a[0].insert(b);
|
||||
}
|
||||
|
||||
rep(i, m) {
|
||||
ll b;
|
||||
cin >> b;
|
||||
a[1].insert(b);
|
||||
}
|
||||
|
||||
int now = 0;
|
||||
|
||||
while (!a[now].empty()) {
|
||||
ll ca = *a[now].begin();
|
||||
ll cb = *a[now^1].begin();
|
||||
a[now^1].erase(a[now^1].begin());
|
||||
now ^= 1;
|
||||
|
||||
cb -= ca;
|
||||
if (cb > 0) {
|
||||
a[now].insert(cb);
|
||||
}
|
||||
}
|
||||
|
||||
string win[] = {"Bob", "Alice"};
|
||||
cout << win[now] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2181/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
int c = 0;
|
||||
bool t = false;
|
||||
rep(i, n) {
|
||||
c += a[i] == 1;
|
||||
t = t || a[i] != 1;
|
||||
}
|
||||
|
||||
cout << (((c & 1) && !t) || ((~c & 1) && t) ? "Alice\n" : "Bob\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2127/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, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl a(n);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
|
||||
rep(i, n) {
|
||||
if (a[i] > b[i]) {
|
||||
swap(a[i], b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
vi perm(n);
|
||||
rep(i, n) {
|
||||
perm[i] = i;
|
||||
}
|
||||
|
||||
sort(all(perm), [&](int i, int j) {
|
||||
if (a[i] == a[j]) {
|
||||
return b[i] > b[j];
|
||||
}
|
||||
return a[i] < a[j];
|
||||
});
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
ans += abs(a[i] - b[i]);
|
||||
}
|
||||
|
||||
ll minimal = OO;
|
||||
|
||||
{
|
||||
ll r = b[perm[0]];
|
||||
nrep(i, 1, n) {
|
||||
if (a[perm[i]] <= r) {
|
||||
cout << ans << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
rmin(minimal, a[perm[i]] - r);
|
||||
rmax(r, b[perm[i]]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cout << ans + minimal * 2 << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
183
Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp
Normal file
183
Bayan 2015 Contest Warm Up/D. CGCDSSQ.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/475/D */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
// map<ll, ll> count;
|
||||
//
|
||||
// V<map<ll, ll>> g(n);
|
||||
// g[0][a[0]] = 1;
|
||||
// count[a[0]]++;
|
||||
//
|
||||
// nrep(i, 1, n) {
|
||||
// g[i][a[i]] = 1;
|
||||
// repv(j, g[i - 1]) {
|
||||
// g[i][gcd(a[i], j.first)] += j.second;
|
||||
// }
|
||||
//
|
||||
// repv(j, g[i]) {
|
||||
// count[j.first] += j.second;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// int q;
|
||||
// cin >> q;
|
||||
// while (q--) {
|
||||
// ll a;
|
||||
// cin >> a;
|
||||
// cout << count[a] << '\n';
|
||||
// }
|
||||
|
||||
vvl sparse(20, vl(n));
|
||||
rep(i, n) {
|
||||
sparse[0][i] = a[i];
|
||||
}
|
||||
|
||||
nrep(j, 1, 20) {
|
||||
for (int i = 0; i + (1 << (j - 1)) < n; i++) {
|
||||
sparse[j][i] = gcd(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
|
||||
}
|
||||
}
|
||||
|
||||
auto query = [&](ll l, ll r) {
|
||||
ll log = 31 - __builtin_clz(r - l + 1);
|
||||
return gcd(sparse[log][l], sparse[log][r - (1 << log) + 1]);
|
||||
};
|
||||
|
||||
map<ll, ll> count;
|
||||
|
||||
rep(i, n) {
|
||||
ll r = i;
|
||||
while (r < n) {
|
||||
ll low = r;
|
||||
ll high = n - 1;
|
||||
ll cur = r;
|
||||
while (low <= high) {
|
||||
ll mid = (low + high) >> 1;
|
||||
|
||||
if (query(i, r) == query(i, mid)) {
|
||||
cur = mid;
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
count[query(i, cur)] += cur - r + 1;
|
||||
r = cur + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
ll x;
|
||||
cin >> x;
|
||||
cout << count[x] << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
122
Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp
Normal file
122
Bubble Cup 9 - Finals [Online Mirror]/D. Dexterina’s Lab.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* Problem URL: https://codeforces.com/contest/717/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 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, x;
|
||||
cin >> n >> x;
|
||||
x++;
|
||||
|
||||
V<long double> p(x);
|
||||
cin >> p;
|
||||
|
||||
V<long double> act(4);
|
||||
rep(i, x) {
|
||||
rep(j, 4) {
|
||||
if ((i >> j) & 1) {
|
||||
act[j] += p[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << act;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1866/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 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi x1(n);
|
||||
vi x2(n);
|
||||
cin >> x1 >> x2;
|
||||
|
||||
int m;
|
||||
cin >> m;
|
||||
|
||||
vi y1(m);
|
||||
vi y2(m);
|
||||
|
||||
cin >> y1 >> y2;
|
||||
|
||||
vi x(2e6 + 1);
|
||||
vi y(2e6 + 1);
|
||||
|
||||
rep(i, n) {
|
||||
x[x1[i]] = x2[i];
|
||||
}
|
||||
|
||||
rep(i, m) {
|
||||
y[y1[i]] = y2[i];
|
||||
}
|
||||
|
||||
rep(i, 2e6 + 1) {
|
||||
if (x[i] < y[i]) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 1;
|
||||
const ll mod = 998244353;
|
||||
|
||||
rep(i, 2e6 + 1) {
|
||||
if (x[i] > y[i]) {
|
||||
ans = (ans << 1) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1942/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, x, y;
|
||||
cin >> n >> x >> y;
|
||||
|
||||
ll ini = y;
|
||||
|
||||
vi a(x);
|
||||
cin >> a;
|
||||
sortv(a);
|
||||
|
||||
ll ans = x - 2;
|
||||
|
||||
ll even = 0;
|
||||
vl odds;
|
||||
nrep(i, 1, x) {
|
||||
if (a[i] - a[i - 1] - 1 <= 1) {
|
||||
ans += a[i] - a[i - 1] - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
ll g = a[i] - a[i - 1] - 1;
|
||||
if (g & 1) {
|
||||
odds.push_back(g >> 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
even += g >> 1;
|
||||
}
|
||||
|
||||
ll g = n - (a.back() - a[0] + 1);
|
||||
|
||||
if (g <= 1) {
|
||||
ans += g;
|
||||
} else if (g & 1) {
|
||||
odds.push_back(g >> 1);
|
||||
} else {
|
||||
even += g >> 1;
|
||||
}
|
||||
|
||||
|
||||
sortv(odds);
|
||||
|
||||
repv(i, odds) {
|
||||
if (y >= i) {
|
||||
y -= i;
|
||||
ans += i + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
ans += y;
|
||||
y = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
ll ev = min(even, (ll)y);
|
||||
y -= ev;
|
||||
ans += ev;
|
||||
ll us = ini - y;
|
||||
ans += us;
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2039/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;
|
||||
|
||||
vi a(m);
|
||||
cin >> a;
|
||||
sort(all(a), greater<>());
|
||||
|
||||
if (n == 1) {
|
||||
cout << a[0] << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (m == 1) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
set<int> all;
|
||||
nrep(i, 1, n + 1) {
|
||||
all.insert(i);
|
||||
}
|
||||
|
||||
vi ans(n);
|
||||
rep(i, m) {
|
||||
if (all.empty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto itr = all.begin();
|
||||
int cur = *itr;
|
||||
ans[*itr - 1] = a[i];
|
||||
itr = all.erase(itr);
|
||||
|
||||
while (itr != all.end()) {
|
||||
if (*itr % cur == 0) {
|
||||
itr++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ans[*itr - 1] = a[i];
|
||||
itr = all.erase(itr);
|
||||
}
|
||||
}
|
||||
|
||||
if (!all.empty()) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
144
Codeforces Global Round 26/C2. Magnitude (Hard Version).cpp
Normal file
144
Codeforces Global Round 26/C2. Magnitude (Hard Version).cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
166
Codeforces Global Round 27/C. Alya and Permutation.cpp
Normal file
166
Codeforces Global Round 27/C. Alya and Permutation.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2035/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;
|
||||
|
||||
if (n == 5) {
|
||||
cout << "5\n";
|
||||
cout << "2 1 3 4 5\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int log = (1 << (32 - __builtin_clz(n))) - 1;
|
||||
|
||||
vi apt(__builtin_popcount(log) << 1);
|
||||
apt[0] = 1;
|
||||
apt[1] = 5;
|
||||
|
||||
for (int i = 2; i < apt.size(); i += 2) {
|
||||
apt[i] = 1 << (i >> 1);
|
||||
apt[i + 1] = (1 << ((i >> 1)) + 1) - 1;
|
||||
}
|
||||
|
||||
while (apt.back() > n) {
|
||||
apt.pop_back();
|
||||
}
|
||||
|
||||
set<int> all;
|
||||
rep(i, n) {
|
||||
all.insert(i + 1);
|
||||
}
|
||||
|
||||
vi perm(n);
|
||||
int ans = log;
|
||||
int start = n - apt.size();
|
||||
int lim = n;
|
||||
if (n & 1) {
|
||||
if (n != log) {
|
||||
start--;
|
||||
lim--;
|
||||
}
|
||||
|
||||
perm[n - 1] = n;
|
||||
all.erase(n);
|
||||
ans = n;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
|
||||
nrep(i, start, lim) {
|
||||
perm[i] = apt[i - start];
|
||||
all.erase(perm[i]);
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
if (all.empty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
perm[i] = *all.begin();
|
||||
all.erase(all.begin());
|
||||
}
|
||||
|
||||
cout << perm;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2035/D */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
vvi rem(n);
|
||||
vl count(n);
|
||||
|
||||
while (~a[n - 1] & 1) {
|
||||
count[n - 1]++;
|
||||
a[n - 1] >>= 1;
|
||||
}
|
||||
|
||||
stack<tuple<ll, ll, int>> s;
|
||||
s.emplace(a[n - 1], count[n - 1], n - 1);
|
||||
|
||||
auto comp = [&](tuple<ll, ll, int> al, tuple<ll, ll, int> bl) {
|
||||
auto [a, pa, _] = al;
|
||||
auto [b, pb, __] = bl;
|
||||
|
||||
if (pb >= 30) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pa == 0) {
|
||||
return a > (b << pb);
|
||||
}
|
||||
|
||||
return (a << pa) + (b << pb) > a + (b << (pb + pa));
|
||||
};
|
||||
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
while (~a[i] & 1) {
|
||||
count[i]++;
|
||||
a[i] >>= 1;
|
||||
}
|
||||
|
||||
tuple<ll, ll, int> tup = {a[i], count[i], i};
|
||||
auto [a, pa, _] = tup;
|
||||
|
||||
while (!s.empty() && comp(tup, s.top())) {
|
||||
auto [b, pb, _] = s.top();
|
||||
s.pop();
|
||||
if (!s.empty()) {
|
||||
auto &[tb, tpb, __] = s.top();
|
||||
tpb += pb;
|
||||
}
|
||||
}
|
||||
|
||||
if (s.empty()) {
|
||||
s.emplace(tup);
|
||||
continue;
|
||||
}
|
||||
|
||||
auto &[b, pb, j] = s.top();
|
||||
rem[j].push_back(i);
|
||||
s.emplace(tup);
|
||||
}
|
||||
|
||||
auto fpow = [&](ll a, ll p) {
|
||||
ll ans = 1;
|
||||
rep(i, 60) {
|
||||
if ((p >> i) & 1) {
|
||||
ans = (ans * a) % mod;
|
||||
}
|
||||
|
||||
a = (a * a) % mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
ll total = 0;
|
||||
rep(i, n) {
|
||||
repv(j, rem[i]) {
|
||||
total = ((total - a[j] * fpow(2, count[j])) % mod + mod) % mod;
|
||||
total = (total + a[j]) % mod;
|
||||
count[i] += count[j];
|
||||
}
|
||||
|
||||
total = (total + a[i] * fpow(2, count[i])) % mod;
|
||||
cout << total << " \n"[i == n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
181
Codeforces Global Round 28/D. Kevin and Competition Memories.cpp
Normal file
181
Codeforces Global Round 28/D. Kevin and Competition Memories.cpp
Normal file
@@ -0,0 +1,181 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2048/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;
|
||||
|
||||
vl a(n);
|
||||
vl b(m);
|
||||
cin >> a >> b;
|
||||
|
||||
int ke = a[0];
|
||||
sort(all(a), greater<>());
|
||||
|
||||
while (!a.empty() && a.back() <= ke) {
|
||||
a.pop_back();
|
||||
}
|
||||
|
||||
if (a.empty()) {
|
||||
nrep(i, 1, m + 1) {
|
||||
cout << (m / i) << " \n"[i == m];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
sortv(b);
|
||||
|
||||
vl act;
|
||||
int i = 0;
|
||||
while (i < m && b[i] <= ke) {
|
||||
act.push_back(b[i]);
|
||||
i++;
|
||||
}
|
||||
|
||||
int lim = i;
|
||||
|
||||
i = m - 1;
|
||||
while (i >= 0 && b[i] > ke) {
|
||||
act.push_back(b[i]);
|
||||
i--;
|
||||
}
|
||||
|
||||
swap(act, b);
|
||||
|
||||
vl pref(m + 2);
|
||||
|
||||
repv(j, a) {
|
||||
int maxi = lower_bound(b.begin() + lim, b.end(), j, greater<>()) - b.begin();
|
||||
pref[maxi]++;
|
||||
}
|
||||
|
||||
nrep(i, 1, m + 2) {
|
||||
pref[i] += pref[i - 1];
|
||||
}
|
||||
|
||||
vvi sparse(20, vi(m));
|
||||
rep(i, m) {
|
||||
sparse[0][i] = pref[i];
|
||||
}
|
||||
|
||||
nrep(j, 1, 20) {
|
||||
for (int i = 0; i + (1 << (j - 1)) < m; i++) {
|
||||
sparse[j][i] = max(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
|
||||
}
|
||||
}
|
||||
|
||||
auto query = [&](int l, int r) {
|
||||
int log = 31 - __builtin_clz(r - l + 1);
|
||||
return max(sparse[log][l], sparse[log][r - (1 << log) + 1]);
|
||||
};
|
||||
|
||||
nrep(i, 1, m + 1) {
|
||||
ll ans = 0;
|
||||
|
||||
for (int j = 0; j + i - 1 < m; j += i) {
|
||||
ans += query(j, j + i - 1) + 1;
|
||||
}
|
||||
|
||||
cout << ans << " \n"[i == m];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2164/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;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
ll x;
|
||||
cin >> x;
|
||||
|
||||
ll minimal = oo;
|
||||
ll maximal = -oo;
|
||||
|
||||
rep(i, n) {
|
||||
rmin(minimal, a[i]);
|
||||
rmax(maximal, a[i]);
|
||||
}
|
||||
|
||||
if (minimal <= x && maximal >= x) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "NO\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2164/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;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
rep(i, n - 1) {
|
||||
nrep(j, i + 1, n) {
|
||||
if ((a[j] % a[i] & 1) == 0) {
|
||||
cout << a[i] << ' ' << a[j] << '\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();
|
||||
}
|
||||
}
|
||||
185
Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp
Normal file
185
Codeforces Global Round 30 (Div. 1 + Div. 2)/C. Dungeon.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2164/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, m;
|
||||
cin >> n >> m;
|
||||
|
||||
priority_queue<ll, vl, greater<>> a;
|
||||
rep(i, n) {
|
||||
ll now;
|
||||
cin >> now;
|
||||
a.push(now);
|
||||
}
|
||||
V<pair<ll, ll>> c(m);
|
||||
|
||||
rep(i, m) {
|
||||
cin >> c[i].first;
|
||||
}
|
||||
|
||||
rep(i, m) {
|
||||
cin >> c[i].second;
|
||||
}
|
||||
sortv(c);
|
||||
|
||||
int ans = 0;
|
||||
priority_queue<ll, vl, greater<>> swo;
|
||||
priority_queue<ll, vl, greater<>> mom;
|
||||
priority_queue<ll> at;
|
||||
int cur = 0;
|
||||
while (cur < m && !a.empty()) {
|
||||
if (c[cur].second == 0) {
|
||||
mom.push(c[cur].first);
|
||||
cur++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a.top() < c[cur].first) {
|
||||
if (!at.empty()) {
|
||||
auto now = a.top();
|
||||
a.pop();
|
||||
a.push(max(now, at.top()));
|
||||
at.pop();
|
||||
ans++;
|
||||
continue;
|
||||
}
|
||||
|
||||
swo.push(a.top());
|
||||
a.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
at.push(c[cur].second);
|
||||
cur++;
|
||||
}
|
||||
|
||||
while (!at.empty() && !a.empty()) {
|
||||
auto now = a.top();
|
||||
a.pop();
|
||||
a.push(max(now, at.top()));
|
||||
at.pop();
|
||||
ans++;
|
||||
}
|
||||
|
||||
if (a.size() > swo.size()) {
|
||||
swap(a, swo);
|
||||
}
|
||||
|
||||
while (!a.empty()) {
|
||||
swo.push(a.top());
|
||||
a.pop();
|
||||
}
|
||||
|
||||
while (!swo.empty() && !mom.empty()) {
|
||||
if (swo.top() < mom.top()) {
|
||||
swo.pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
ans++;
|
||||
swo.pop();
|
||||
mom.pop();
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
157
Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp
Normal file
157
Codeforces Global Round 30 (Div. 1 + Div. 2)/D. Copy String.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2164/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, k;
|
||||
cin >> n >> k;
|
||||
|
||||
string s, t;
|
||||
cin >> s >> t;
|
||||
|
||||
if (s[0] != t[0]) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
vvi pos(26);
|
||||
rep(i, n) {
|
||||
pos[s[i] - 'a'].push_back(i);
|
||||
}
|
||||
|
||||
int big = 0;
|
||||
int minimal = n - 1;
|
||||
|
||||
vi choice(n);
|
||||
for (int i = n - 1; i >= 1; i--) {
|
||||
int cur = t[i] - 'a';
|
||||
auto itr = upper_bound(all(pos[cur]), min(i, minimal));
|
||||
|
||||
if (itr == pos[cur].begin()) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
itr--;
|
||||
|
||||
rmax(big, i - *itr);
|
||||
rmin(minimal, *itr);
|
||||
choice[i] = *itr;
|
||||
}
|
||||
|
||||
if (big > k) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << big << '\n';
|
||||
rep(j, big) {
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
if (choice[i] != i) {
|
||||
s[choice[i] + 1] = s[choice[i]];
|
||||
choice[i]++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << s << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
126
Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp
Normal file
126
Codeforces Global Round 30 (Div. 1 + Div. 2)/E. Journey.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2164/problem/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
vl cost(m);
|
||||
ll ans = 0;
|
||||
|
||||
V<tuple<ll, int, int>> edges(m);
|
||||
rep(i, m) {
|
||||
auto &[c, u, v] = edges[i];
|
||||
cin >> u >> v >> c;
|
||||
u--, v--;
|
||||
ans += c;
|
||||
cost[i] = c;
|
||||
}
|
||||
|
||||
vi dsu(n);
|
||||
rep(i, n) {
|
||||
dsu[i] = i;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
142
Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp
Normal file
142
Codeforces Round 1004 (Div. 1)/A. Object Identification.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2066/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;
|
||||
|
||||
set<int> all;
|
||||
rep(i, n) {
|
||||
all.insert(i);
|
||||
}
|
||||
|
||||
vi a(n);
|
||||
vi pos(n);
|
||||
rep(i, n) {
|
||||
cin >> a[i];
|
||||
all.erase(a[i] - 1);
|
||||
pos[a[i] - 1] = i;
|
||||
}
|
||||
|
||||
if (all.size() > 0) {
|
||||
cout << "? " << *all.begin() + 1 << ' ' << a[0] << endl;
|
||||
int ans;
|
||||
cin >> ans;
|
||||
|
||||
cout << (ans == 0 ? "! A" : "! B") << endl;
|
||||
return;
|
||||
}
|
||||
|
||||
int choice1 = pos[0] + 1;
|
||||
int choice2 = pos[n - 1] + 1;
|
||||
|
||||
cout << "? " << choice1 << ' ' << choice2 << endl;
|
||||
int ans1;
|
||||
cin >> ans1;
|
||||
|
||||
cout << "? " << choice2 << ' ' << choice1 << endl;
|
||||
int ans2;
|
||||
cin >> ans2;
|
||||
|
||||
cout << (ans1 == ans2 && ans1 >= n - 1 && ans2 >= n - 1 ? "! B" : "! A") << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
123
Codeforces Round 1004 (Div. 2)/C. Devyatkino.cpp
Normal file
123
Codeforces Round 1004 (Div. 2)/C. Devyatkino.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2067/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;
|
||||
cin >> n;
|
||||
|
||||
rep(k, 8) {
|
||||
string now = to_string(n - k);
|
||||
|
||||
int cur = 0;
|
||||
repv(i, now) {
|
||||
if (i <= '7') {
|
||||
rmax(cur, i - '0');
|
||||
}
|
||||
}
|
||||
|
||||
if (k >= 7 - cur) {
|
||||
cout << k << '\n';
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
172
Codeforces Round 1007 (Div. 2)/C. Trapmigiano Reggiano.cpp
Normal file
172
Codeforces Round 1007 (Div. 2)/C. Trapmigiano Reggiano.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
148
Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp
Normal file
148
Codeforces Round 1008 (Div. 1)/A. Breach of Faith.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2077/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;
|
||||
n <<= 1;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
sortv(a);
|
||||
|
||||
set<ll> elem;
|
||||
|
||||
ll total = 0;
|
||||
vl ans(n);
|
||||
rep(i, n >> 1) {
|
||||
ans[2 * i] = a[n - i - 1];
|
||||
ans[2 * i + 1] = a[i];
|
||||
total += ans[2 * i] - ans[2 * i + 1];
|
||||
elem.insert(ans[2 * i]);
|
||||
elem.insert(ans[2 * i + 1]);
|
||||
}
|
||||
|
||||
if (elem.count(total)) {
|
||||
ll t = a[n - 1];
|
||||
ans[1] = a[n - 2];
|
||||
total = -a[n - 2];
|
||||
nrep(i, 1, n >> 1) {
|
||||
ans[2 * i] = a[i - 1];
|
||||
ans[2 * i + 1] = a[n - i - 2];
|
||||
total += ans[2 * i] - ans[2 * i + 1];
|
||||
}
|
||||
cout << t;
|
||||
ans[0] = t - total;
|
||||
repv(i, ans) {
|
||||
cout << ' ' << i;
|
||||
}
|
||||
cout << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
cout << total;
|
||||
repv(i, ans) {
|
||||
cout << ' ' << i;
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
128
Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp
Normal file
128
Codeforces Round 1010 (Div. 2, Unrated)/B. Floor or Ceil.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2082/B */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll x, n, m;
|
||||
cin >> x >> n >> m;
|
||||
|
||||
n = min(n, 31LL);
|
||||
m = min(m, 31LL);
|
||||
|
||||
ll one = x;
|
||||
ll two = x;
|
||||
|
||||
rep(i, n) {
|
||||
one >>= 1;
|
||||
}
|
||||
|
||||
rep(j, m) {
|
||||
one = (one + 1) >> 1;
|
||||
two = (two + 1) >> 1;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
two >>= 1;
|
||||
}
|
||||
|
||||
cout << two << ' ' << one << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,22 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2091/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 (size_t i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (size_t i = s; i < (lim); i++)
|
||||
#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_; }
|
||||
@@ -69,14 +75,112 @@ auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
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 prev = d;
|
||||
d *= d;
|
||||
|
||||
V<string> a(n);
|
||||
cin >> a;
|
||||
|
||||
const ll mod = 998244353;
|
||||
|
||||
vvl dp(n, vl(m + 1));
|
||||
|
||||
dp[n - 1][0] = 1;
|
||||
|
||||
int now = n - 1;
|
||||
|
||||
auto dis = [&](ll x1, ll y1, ll x2, ll y2) {
|
||||
ll x = x1 - x2;
|
||||
ll y = y1 - y2;
|
||||
return x*x + y*y;
|
||||
};
|
||||
|
||||
while (true) {
|
||||
nrep(i, 1, m + 1) {
|
||||
dp[now][i] = (dp[now][i] + dp[now][i - 1]) % mod;
|
||||
}
|
||||
|
||||
dp[now][0] = 0;
|
||||
nrep(i, 1, m + 1) {
|
||||
if (a[now][i - 1] == '#') {
|
||||
dp[now][i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
nrep(i, 1, m + 1) {
|
||||
dp[now][i] = (dp[now][i] + dp[now][i - 1]) % mod;
|
||||
}
|
||||
|
||||
if (now == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
nrep(i, 1, m + 1) {
|
||||
if (a[now][i - 1] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
ll sum = ((dp[now][min(i + prev, m)] - dp[now][max(i - prev - 1, 0)]) % mod + mod) % mod;
|
||||
|
||||
int cur = now - 1;
|
||||
int act = i - prev;
|
||||
while (dis(cur, act, now, i) > d) {
|
||||
act++;
|
||||
}
|
||||
|
||||
int other = (i << 1) - act;
|
||||
|
||||
dp[cur][max(act, 0)] += sum;
|
||||
dp[cur][max(act, 0)] %= mod;
|
||||
if (other < m) {
|
||||
dp[cur][other + 1] -= sum;
|
||||
dp[cur][other + 1] += mod * (dp[cur][other + 1] < 0);
|
||||
}
|
||||
|
||||
cur--;
|
||||
}
|
||||
|
||||
now--;
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
nrep(i, 1, m + 1) {
|
||||
if (a[0][i - 1] == '#') {
|
||||
continue;
|
||||
}
|
||||
|
||||
ans = ((ans + dp[0][min(i + prev, m)] - dp[0][max(i - prev - 1, 0)]) % mod + mod) % mod;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int n, m, d;
|
||||
cin >> n >> m >> d;
|
||||
pre();
|
||||
|
||||
V<string> a;
|
||||
cin >> a;
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
180
Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp
Normal file
180
Codeforces Round 1019 (Div. 2)/C. Median Splits.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2103/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;
|
||||
|
||||
vl pref(n);
|
||||
vl suf(n);
|
||||
|
||||
pref[0] = (a[0] > k) - (a[0] <= k);
|
||||
suf[n - 1] = (a[n - 1] > k) - (a[n - 1] <= k);
|
||||
|
||||
nrep(i, 1, n) {
|
||||
pref[i] = pref[i - 1] + (a[i] > k) - (a[i] <= k);
|
||||
suf[n - i - 1] = suf[n - i] + (a[n - i - 1] > k) - (a[n - i - 1] <= k);
|
||||
}
|
||||
|
||||
set<int> sav;
|
||||
|
||||
nrep(i, 1, n - 1) {
|
||||
if (suf[i + 1] > 0) {
|
||||
sav.insert(pref[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (pref[i] <= 0) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (sav.lower_bound(pref[i]) != sav.end()) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
sav.insert(pref[i]);
|
||||
}
|
||||
|
||||
sav.clear();
|
||||
|
||||
for (int i = n - 2; i > 0; i--) {
|
||||
if (pref[i - 1] > 0) {
|
||||
sav.insert(suf[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (suf[i] <= 0) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (sav.lower_bound(suf[i]) != sav.end()) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
sav.insert(suf[i]);
|
||||
}
|
||||
|
||||
int sa1 = 0;
|
||||
while (sa1 < n && pref[sa1] > 0) {
|
||||
sa1++;
|
||||
}
|
||||
|
||||
int sa2 = n - 1;
|
||||
while (sa2 >= 0 && suf[sa2] > 0) {
|
||||
sa2--;
|
||||
}
|
||||
|
||||
if (sa1 < sa2 - 1) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "NO\n";
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
175
Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp
Normal file
175
Codeforces Round 1021 (Div. 1)/A. Sports Betting.cpp
Normal file
@@ -0,0 +1,175 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2097/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;
|
||||
|
||||
sortv(a);
|
||||
|
||||
V<pair<int, int>> v;
|
||||
int c = 1;
|
||||
|
||||
nrep(i, 1, n) {
|
||||
if (a[i] == a[i - 1]) {
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
|
||||
v.emplace_back(a[i - 1], c);
|
||||
c = 1;
|
||||
}
|
||||
v.emplace_back(a.back(), c);
|
||||
|
||||
int cur = -1;
|
||||
|
||||
rep(i, v.size()) {
|
||||
if (v[i].second >= 4) {
|
||||
cout << "Yes\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (cur == -1 && v[i].second >= 2) {
|
||||
cur = i;
|
||||
}
|
||||
}
|
||||
|
||||
if (cur == -1) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int tmp = v[cur].first + 1;
|
||||
int choice = cur + 1;
|
||||
while (choice < v.size()) {
|
||||
if (v[choice].first != tmp) {
|
||||
if (v[choice].second >= 2) {
|
||||
cur = choice;
|
||||
tmp = v[cur].first + 1;
|
||||
} else {
|
||||
cur = choice + 1;
|
||||
if (cur < v.size()) {
|
||||
tmp = v[cur].first + 1;
|
||||
}
|
||||
}
|
||||
choice++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (v[choice].second >= 2) {
|
||||
break;
|
||||
}
|
||||
|
||||
tmp++;
|
||||
choice++;
|
||||
}
|
||||
|
||||
if (choice >= v.size()) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2098/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, k;
|
||||
cin >> n >> k;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
sortv(a);
|
||||
|
||||
cout << a[((n + k) >> 1)] - a[((n - k - 1) >> 1)] + 1 << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2109/problem/C2 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
cin >> n;
|
||||
|
||||
int tmp;
|
||||
cout << "mul 9" << endl;
|
||||
cin >> tmp;
|
||||
cout << "digit" << endl;
|
||||
cin >> tmp;
|
||||
cout << "digit" << endl;
|
||||
cin >> tmp;
|
||||
cout << "add " << n - 9 << endl;
|
||||
cin >> tmp;
|
||||
cout << "!" << endl;
|
||||
cin >> tmp;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
151
Codeforces Round 1026 (Div. 2)/C. Racing.cpp
Normal file
151
Codeforces Round 1026 (Div. 2)/C. Racing.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2110/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 d(n);
|
||||
cin >> d;
|
||||
|
||||
V<pair<int, int>> v(n);
|
||||
repv(i, v) {
|
||||
cin >> i.first >> i.second;
|
||||
}
|
||||
|
||||
V<pair<int, int>> p(n + 1);
|
||||
nrep(i, 1, n + 1) {
|
||||
int a = d[i - 1];
|
||||
if (a == -1) {
|
||||
p[i] = {p[i - 1].first, p[i - 1].second + 1};
|
||||
rmin(p[i].second, v[i - 1].second);
|
||||
rmax(p[i].first, v[i - 1].first);
|
||||
if (p[i].second < p[i].first) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
p[i] = {p[i - 1].first + a, p[i - 1].second + a};
|
||||
rmin(p[i].second, v[i - 1].second);
|
||||
rmax(p[i].first, v[i - 1].first);
|
||||
if (p[i].second < p[i].first) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int cur = p.back().first;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
if (d[i] == -1) {
|
||||
d[i] = cur > p[i].first;
|
||||
cur -= cur > p[i].first;
|
||||
continue;
|
||||
}
|
||||
|
||||
cur -= d[i];
|
||||
}
|
||||
|
||||
cout << d;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
164
Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp
Normal file
164
Codeforces Round 1026 (Div. 2)/D. Fewer Batteries.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2110/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;
|
||||
|
||||
vl val(n);
|
||||
cin >> val;
|
||||
|
||||
V<V<pair<int, ll>>> tmp(n);
|
||||
while (m--) {
|
||||
int a, b, c;
|
||||
cin >> a >> b >> c;
|
||||
tmp[a - 1].emplace_back(b - 1, c);
|
||||
}
|
||||
|
||||
V<V<pair<int, ll>>> graph(n);
|
||||
rep(i, n) {
|
||||
if (tmp[i].empty()) {
|
||||
continue;
|
||||
}
|
||||
sortv(tmp[i]);
|
||||
graph[i].emplace_back(tmp[i][0]);
|
||||
nrep(j, 1, tmp[i].size()) {
|
||||
if (tmp[i][j].first == tmp[i][j - 1].first) {
|
||||
continue;
|
||||
}
|
||||
|
||||
graph[i].emplace_back(tmp[i][j]);
|
||||
}
|
||||
}
|
||||
|
||||
vl dp(n);
|
||||
|
||||
ll low = 0;
|
||||
ll high = 1e18;
|
||||
ll ans = -1;
|
||||
while (low <= high) {
|
||||
ll mid = (high - low) / 2 + low;
|
||||
|
||||
fill(all(dp), -OO);
|
||||
|
||||
dp[0] = min(mid, val[0]);
|
||||
rep(i, n) {
|
||||
repv(j, graph[i]) {
|
||||
if (j.second > dp[i]) {
|
||||
continue;
|
||||
}
|
||||
dp[j.first] = max(dp[j.first], min(dp[i] + val[j.first], mid));
|
||||
}
|
||||
}
|
||||
|
||||
if (dp[n - 1] >= 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2116/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 apos(n);
|
||||
vi bpos(n);
|
||||
|
||||
vi a(n);
|
||||
vi b(n);
|
||||
|
||||
rep(i, n) {
|
||||
int p;
|
||||
cin >> p;
|
||||
apos[p] = i;
|
||||
a[i] = p;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
int p;
|
||||
cin >> p;
|
||||
bpos[p] = i;
|
||||
b[i] = p;
|
||||
}
|
||||
|
||||
V<pair<int, int>> ans(n);
|
||||
|
||||
set<int> pos;
|
||||
rep(i, n) {
|
||||
pos.insert(i);
|
||||
}
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
set<int> rem;
|
||||
|
||||
auto getans = [&](int now, int p, vi &other) {
|
||||
auto itr = pos.lower_bound(p);
|
||||
|
||||
while (itr != pos.end()) {
|
||||
pair<int, int> act = {now, other[*itr - p]};
|
||||
rmax(ans[*itr], act);
|
||||
rem.insert(*itr);
|
||||
itr++;
|
||||
}
|
||||
};
|
||||
|
||||
getans(a[apos[i]], apos[i], b);
|
||||
getans(b[bpos[i]], bpos[i], a);
|
||||
|
||||
repv(i, rem) {
|
||||
pos.erase(i);
|
||||
}
|
||||
}
|
||||
|
||||
const ll mod = 998244353;
|
||||
auto fpow = [&](ll a, ll p) {
|
||||
ll ans = 1;
|
||||
|
||||
rep(i, 30) {
|
||||
if ((p >> i) & 1) {
|
||||
ans = (ans * a) % mod;
|
||||
}
|
||||
a = (a * a) % mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
cout << (fpow(2, ans[i].first) + fpow(2, ans[i].second)) % mod << " \n"[i == n - 1];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
146
Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp
Normal file
146
Codeforces Round 1030 (Div. 2)/C. Make It Beautiful.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2118/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
|
||||
|
||||
ll getcost(ll n)
|
||||
{
|
||||
if (~n & 1) {
|
||||
return 1;
|
||||
}
|
||||
return 1LL << __builtin_ctzll(~n);
|
||||
}
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
ll k;
|
||||
cin >> n >> k;
|
||||
|
||||
struct comp {
|
||||
bool operator()(ll a, ll b) {
|
||||
return getcost(a) > getcost(b);
|
||||
}
|
||||
};
|
||||
|
||||
priority_queue<ll, vl, comp> pq;
|
||||
rep(i, n) {
|
||||
int n;
|
||||
cin >> n;
|
||||
pq.push(n);
|
||||
}
|
||||
|
||||
while (getcost(pq.top()) <= k) {
|
||||
ll cur = pq.top();
|
||||
pq.pop();
|
||||
ll c = getcost(cur);
|
||||
cur += c;
|
||||
k -= c;
|
||||
pq.push(cur);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
while (!pq.empty()) {
|
||||
ans += __builtin_popcountll(pq.top());
|
||||
pq.pop();
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2118/D1 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
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 p(n);
|
||||
vl d(n);
|
||||
cin >> p >> d;
|
||||
|
||||
V<V<bool>> loop(n, V<bool>(2));
|
||||
V<V<bool>> vis(n, V<bool>(2));
|
||||
V<V<bool>> frame(n, V<bool>(2));
|
||||
|
||||
function<bool(int, int, ll)> func = [&](int i, int dir, ll cur) -> bool {
|
||||
if (cur % k != d[i]) {
|
||||
int ne = i + 1 - 2 * dir;
|
||||
if (ne < 0 || ne == n) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return func(ne, dir, (cur + abs(p[i] - p[ne])) % k);
|
||||
}
|
||||
|
||||
dir ^= 1;
|
||||
|
||||
if (frame[i][dir]) {
|
||||
frame[i][dir] = false;
|
||||
loop[i][dir] = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vis[i][dir]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
vis[i][dir] = true;
|
||||
frame[i][dir] = true;
|
||||
|
||||
int ne = i + 1 - 2 * dir;
|
||||
if (ne < 0 || ne == n) {
|
||||
frame[i][dir] = false;
|
||||
return false;
|
||||
}
|
||||
bool res = func(ne, dir, (cur + abs(p[i] - p[ne])) % k);
|
||||
|
||||
if (res == true) {
|
||||
loop[i][dir] = true;
|
||||
}
|
||||
|
||||
frame[i][dir] = false;
|
||||
return loop[i][dir];
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (!vis[i][0]) {
|
||||
func(i, 1, d[i]);
|
||||
}
|
||||
|
||||
if (!vis[i][1]) {
|
||||
func(i, 0, d[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
ll c;
|
||||
cin >> c;
|
||||
|
||||
int ind = lower_bound(all(p), c) - p.begin();
|
||||
bool pos = true;
|
||||
ll cur = 0;
|
||||
while (ind < n) {
|
||||
cur = (cur + abs(p[ind] - c)) % k;
|
||||
if (cur == d[ind]) {
|
||||
if (loop[ind][1]) {
|
||||
pos = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
c = p[ind];
|
||||
ind++;
|
||||
}
|
||||
|
||||
cout << (pos ? "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();
|
||||
}
|
||||
}
|
||||
127
Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp
Normal file
127
Codeforces Round 1032 (Div. 3)/G. Gangsta.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2121/G */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
vl pref(n + 1);
|
||||
|
||||
rep(i, n) {
|
||||
pref[i + 1] = pref[i] + (a[i] == '0') - (a[i] == '1');
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
ans += ((ll)i + 1) * (n - i);
|
||||
}
|
||||
|
||||
sortv(pref);
|
||||
rep(i, n + 1) {
|
||||
ans += pref[i] * (i - (n - i));
|
||||
}
|
||||
|
||||
cout << (ans >> 1) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2120/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 n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
ll high = n * (n + 1) / 2;
|
||||
|
||||
if (m < n || m > high) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
set<ll, greater<>> all;
|
||||
nrep(i, 1, n + 1) {
|
||||
all.insert(i);
|
||||
}
|
||||
|
||||
V<pair<int, int>> edges;
|
||||
int root = -1;
|
||||
int prev = -1;
|
||||
|
||||
auto itr = all.begin();
|
||||
while (n < m) {
|
||||
while (n - 1 > m - *itr) {
|
||||
itr++;
|
||||
}
|
||||
n--;
|
||||
m -= *itr;
|
||||
if (root == -1) {
|
||||
root = *itr;
|
||||
prev = root;
|
||||
itr = all.erase(itr);
|
||||
continue;
|
||||
}
|
||||
|
||||
edges.emplace_back(prev, *itr);
|
||||
prev = *itr;
|
||||
itr = all.erase(itr);
|
||||
}
|
||||
|
||||
if (prev == -1) {
|
||||
root = 1;
|
||||
} else {
|
||||
edges.emplace_back(prev, 1);
|
||||
}
|
||||
|
||||
all.erase(1);
|
||||
itr = all.begin();
|
||||
while (itr != all.end()) {
|
||||
edges.emplace_back(1, *itr);
|
||||
itr++;
|
||||
}
|
||||
|
||||
cout << root << '\n';
|
||||
repv(i, edges) {
|
||||
cout << i.first << ' ' << i.second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2120/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;
|
||||
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll a, b, k;
|
||||
cin >> a >> b >> k;
|
||||
|
||||
ll n = k * (a - 1) + 1;
|
||||
n %= mod;
|
||||
|
||||
auto fpow = [&](ll a, ll p) {
|
||||
ll ans = 1;
|
||||
rep(i, 60) {
|
||||
if ((p >> i) & 1) {
|
||||
ans = (ans * a) % mod;
|
||||
}
|
||||
a = (a * a) % mod;
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
|
||||
auto comb = [&](ll n, ll k) {
|
||||
ll ans = 1;
|
||||
ll div = 1;
|
||||
|
||||
rep(i, k) {
|
||||
ans = (ans * ((n - i) % mod + mod) % mod) % mod;
|
||||
div = (div * (i + 1)) % mod;
|
||||
}
|
||||
|
||||
return ans * fpow(div, mod - 2) % mod;
|
||||
};
|
||||
|
||||
cout << n << ' ' << ((b - 1) * k % mod * comb(n, a) % mod + 1) % mod << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,190 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2129/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;
|
||||
|
||||
string ans(n, ' ');
|
||||
|
||||
vi pos(n);
|
||||
rep(i, n) {
|
||||
pos[i] = i + 1;
|
||||
}
|
||||
bool rev = false;
|
||||
|
||||
int total;
|
||||
cout << "? " << n;
|
||||
nrep(i, 1, n + 1) {
|
||||
cout << ' ' << i;
|
||||
}
|
||||
cout << endl;
|
||||
cin >> total;
|
||||
|
||||
if (total == 0) {
|
||||
rev = true;
|
||||
reverse(all(pos));
|
||||
cout << "? " << n;
|
||||
rep(i, n) {
|
||||
cout << ' ' << pos[i];
|
||||
}
|
||||
cout << endl;
|
||||
cin >> total;
|
||||
}
|
||||
|
||||
auto sfirstclose = [&]() {
|
||||
int low = 2;
|
||||
int high = n;
|
||||
int ans = 2;
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >> 1;
|
||||
|
||||
cout << "? " << mid;
|
||||
rep(i, mid) {
|
||||
cout << ' ' << pos[i];
|
||||
}
|
||||
cout << endl;
|
||||
int cur;
|
||||
cin >> cur;
|
||||
|
||||
if (cur == total) {
|
||||
ans = mid;
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
int c = sfirstclose();
|
||||
|
||||
int v = pos[c - 1];
|
||||
for (int i = 0; i + 1 < n; i += 2) {
|
||||
cout << "? 9 " << i + 1 << ' ' << v << ' ' << v << ' ' << i + 2 << ' ' << v << ' ' << i + 1 << ' ' << i + 1 << ' ' << i + 2 << ' ' << i + 2 << endl;
|
||||
int cur;
|
||||
cin >> cur;
|
||||
|
||||
char a1[] = {')', ')', '(', '('};
|
||||
char a2[] = {')', '(', '(', ')'};
|
||||
|
||||
ans[i] = a1[cur];
|
||||
ans[i + 1] = a2[cur];
|
||||
}
|
||||
|
||||
if (n & 1) {
|
||||
cout << "? 2 " << n << ' ' << pos[c - 1] << endl;
|
||||
int cur;
|
||||
cin >> cur;
|
||||
|
||||
if (cur == 1) {
|
||||
ans.back() = '(';
|
||||
} else {
|
||||
ans.back() = ')';
|
||||
}
|
||||
}
|
||||
|
||||
cout << "! " << ans << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -91,46 +91,60 @@ void solve()
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vi depth(n);
|
||||
vi sums(n);
|
||||
V<bool> leaf(n, true);
|
||||
|
||||
sums[0] = 1;
|
||||
|
||||
vvi graph(n);
|
||||
nrep(i, 1, n) {
|
||||
int a;
|
||||
cin >> a;
|
||||
a--;
|
||||
|
||||
depth[i] = depth[a] + 1;
|
||||
sums[depth[i]]++;
|
||||
leaf[a] = false;
|
||||
int j;
|
||||
cin >> j;
|
||||
graph[j - 1].push_back(i);
|
||||
}
|
||||
|
||||
int maxdepth = oo;
|
||||
rep(i, n) {
|
||||
if (leaf[i]) {
|
||||
rmin(maxdepth, depth[i]);
|
||||
vi sums;
|
||||
int minimal = oo;
|
||||
|
||||
function<void(int, int)> calc = [&](int i, int d) {
|
||||
if (d >= sums.size()) {
|
||||
sums.emplace_back();
|
||||
}
|
||||
}
|
||||
|
||||
vi dp(n + 1);
|
||||
dp[0] = 1;
|
||||
sums[d]++;
|
||||
d++;
|
||||
|
||||
rep(i, maxdepth) {
|
||||
for (int j = n; j >= sums[i]; j--) {
|
||||
dp[j] |= dp[j - sums[i]];
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, n + 1) {
|
||||
if (dp[i] && i <= k && n - i <= n - k) {
|
||||
cout << maxdepth + 1 << '\n';
|
||||
if (graph[i].empty()) {
|
||||
rmin(minimal, d);
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
calc(j, d);
|
||||
}
|
||||
};
|
||||
|
||||
calc(0, 0);
|
||||
|
||||
V<V<int>> dp(sums.size() + 1, V<int>(k + 1, -1));
|
||||
dp[0][k] = n - k;
|
||||
|
||||
int maximal = 0;
|
||||
|
||||
nrep(i, 1, minimal + 1) {
|
||||
int cost = sums[i - 1];
|
||||
|
||||
rep(j, k + 1) {
|
||||
if (j + cost <= k) {
|
||||
rmax(dp[i][j], dp[i - 1][j + cost]);
|
||||
}
|
||||
|
||||
if (dp[i - 1][j] >= cost) {
|
||||
rmax(dp[i][j], dp[i - 1][j] - cost);
|
||||
}
|
||||
|
||||
if (dp[i][j] >= 0) {
|
||||
maximal = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << maxdepth << '\n';
|
||||
cout << maximal << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
|
||||
@@ -75,53 +75,67 @@ auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
int n;
|
||||
cin >> n;
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
vvvl dp(n + 1, vvl(n + 1, vl(n + 1)));
|
||||
dp[0][0][0] = 1;
|
||||
|
||||
nrep(i, 1, n + 1) {
|
||||
int cur = a[i - 1];
|
||||
dp[i] = dp[i - 1];
|
||||
|
||||
rep(j, n + 1) {
|
||||
rep(k, min(cur, j) + 1) {
|
||||
if (cur >= j) {
|
||||
dp[i][cur][k] += dp[i - 1][j][k];
|
||||
dp[i][cur][k] %= mod;
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[i][j][cur] += dp[i - 1][j][k];
|
||||
dp[i][j][cur] %= mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n + 1) {
|
||||
rep(j, n + 1) {
|
||||
ans = (ans + dp[n][i][j]) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
vvvl memo(n, vvl(n + 1, vl(n + 1, -1)));
|
||||
|
||||
function<ll(int, int, int)> dp = [&](int i, int maximal, int mp) {
|
||||
if (i >= n) {
|
||||
return 1LL;
|
||||
}
|
||||
|
||||
ll &ans = memo[i][maximal][mp];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = dp(i + 1, maximal, mp);
|
||||
|
||||
if (a[i] < maximal && a[i] < mp) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
if (a[i] >= maximal) {
|
||||
ans += dp(i + 1, a[i], mp);
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
if (a[i] < maximal) {
|
||||
ans += dp(i + 1, maximal, max(mp, a[i]));
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << dp(0, 0, 0) << '\n';
|
||||
solve();
|
||||
}
|
||||
}
|
||||
|
||||
118
Codeforces Round 1056 (Div. 2)/D. Batteries.cpp
Normal file
118
Codeforces Round 1056 (Div. 2)/D. Batteries.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2155/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;
|
||||
|
||||
nrep(c, 1, n + 1) {
|
||||
rep(i, n - c) {
|
||||
cout << i + 1 << ' ' << i + 1 + c << endl;
|
||||
int x;
|
||||
cin >> x;
|
||||
if (x) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
143
Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp
Normal file
143
Codeforces Round 1059 (Div. 3)/E. Beautiful Palindromes.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2162/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
set<int> miss;
|
||||
rep(i, n) {
|
||||
miss.insert(i + 1);
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
miss.erase(a[i]);
|
||||
}
|
||||
|
||||
if (miss.empty()) {
|
||||
rep(i, k) {
|
||||
cout << a[i] << " \n"[i == k - 1];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int choice = -1;
|
||||
rep(i, n) {
|
||||
if (a[i] != a[n - 1]) {
|
||||
choice = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
vi at;
|
||||
if (choice == -1) {
|
||||
at = {*miss.begin(), *next(miss.begin()), a[0]};
|
||||
} else {
|
||||
at = {*miss.begin(), choice, a[n - 1]};
|
||||
}
|
||||
|
||||
rep(i, k) {
|
||||
cout << at[i % 3] << " \n"[i == k - 1];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
119
Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp
Normal file
119
Codeforces Round 1063 (Div. 2)/A. Souvlaki VS. Kalamaki.cpp
Normal file
@@ -0,0 +1,119 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2163/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;
|
||||
sortv(a);
|
||||
|
||||
rep(i, n - 1) {
|
||||
if ((i & 1) && a[i] != a[i + 1]) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
133
Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp
Normal file
133
Codeforces Round 1063 (Div. 2)/B. Siga ta Kymata.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2163/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 p(n);
|
||||
cin >> p;
|
||||
string x;
|
||||
cin >> x;
|
||||
|
||||
if (x[0] == '1' || x[n - 1] == '1') {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
vi pos(n);
|
||||
rep(i, n) {
|
||||
pos[p[i] - 1] = i + 1;
|
||||
}
|
||||
|
||||
if (x[pos[0] - 1] == '1' || x[pos[n - 1] - 1] == '1') {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "5\n";
|
||||
cout << "1 " << pos[0] << '\n';
|
||||
cout << "1 " << pos[n - 1] << '\n';
|
||||
cout << pos[0] << ' ' << n << '\n';
|
||||
cout << pos[n - 1] << ' ' << n << '\n';
|
||||
cout << min(pos[0], pos[n - 1]) << ' ' << max(pos[0], pos[n - 1]) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
140
Codeforces Round 1063 (Div. 2)/C. Monopati.cpp
Normal file
140
Codeforces Round 1063 (Div. 2)/C. Monopati.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2163/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 a(2, vi(n));
|
||||
cin >> a;
|
||||
|
||||
vi maximal(n);
|
||||
vi minimal(n);
|
||||
|
||||
maximal[n - 1] = a[1][n - 1];
|
||||
minimal[n - 1] = a[1][n - 1];
|
||||
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
maximal[i] = max(a[1][i], maximal[i + 1]);
|
||||
minimal[i] = min(a[1][i], minimal[i + 1]);
|
||||
}
|
||||
|
||||
int ma = 0;
|
||||
int mi = oo;
|
||||
vi act(n << 1, (n << 1) + 1);
|
||||
|
||||
rep(i, n) {
|
||||
rmax(ma, a[0][i]);
|
||||
rmin(mi, a[0][i]);
|
||||
|
||||
rmin(act[min(mi, minimal[i]) - 1], max(ma, maximal[i]));
|
||||
}
|
||||
|
||||
ll ans = (n << 1) - act.back() + 1;
|
||||
for (int i = (n << 1) - 2; i >= 0; i--) {
|
||||
rmin(act[i], act[i + 1]);
|
||||
ans += (n << 1) - act[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();
|
||||
}
|
||||
}
|
||||
145
Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp
Normal file
145
Codeforces Round 1063 (Div. 2)/D1. Diadrash (Easy Version).cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2163/problem/D1 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
V<pair<int, int>> seg(q);
|
||||
repv(i, seg) {
|
||||
cin >> i.first >> i.second;
|
||||
}
|
||||
|
||||
sortv(seg);
|
||||
vi rang(n, -1);
|
||||
|
||||
repv(i, seg) {
|
||||
rmax(rang[i.first - 1], i.second - 1);
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
|
||||
int maximal = -1;
|
||||
int lim = (n >> 1) + (n & 1);
|
||||
|
||||
cout << "? 1 " << (n >> 1) + (n & 1) << endl;
|
||||
int tmp;
|
||||
cin >> tmp;
|
||||
if (!tmp) {
|
||||
maximal = (n >> 1) + (n & 1) - 1;
|
||||
lim = n;
|
||||
}
|
||||
|
||||
rep(i, lim) {
|
||||
if (rang[i] <= maximal) {
|
||||
continue;
|
||||
}
|
||||
cout << "? " << i + 1 << ' ' << rang[i] + 1 << endl;
|
||||
int act;
|
||||
cin >> act;
|
||||
rmax(ans, act);
|
||||
maximal = rang[i];
|
||||
}
|
||||
|
||||
cout << "! " << ans << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
138
Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp
Normal file
138
Codeforces Round 1064 (Div. 1)/B. Marble Council.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2165/B */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
ll mod = 998244353;
|
||||
|
||||
vl c(n);
|
||||
ll ma = 0;
|
||||
rep(i, n) {
|
||||
int a;
|
||||
cin >> a;
|
||||
c[a - 1]++;
|
||||
rmax(ma, c[a - 1]);
|
||||
}
|
||||
|
||||
vl dp(n + 1);
|
||||
dp[0] = 1;
|
||||
|
||||
rep(i, n) {
|
||||
if (c[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = n; j >= c[i]; j--) {
|
||||
(dp[j] += dp[j - c[i]] * c[i]) %= mod;
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
nrep(i, ma, n + 1) {
|
||||
(ans += dp[i]) %= mod;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2171/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 suf(n + 1);
|
||||
vi pos(n);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
suf[i] = max(suf[i + 1], p[i]);
|
||||
pos[p[i] - 1] = i;
|
||||
}
|
||||
|
||||
int now = 1;
|
||||
int ma = 1;
|
||||
int lim = n;
|
||||
while (now <= n) {
|
||||
int po = pos[now - 1];
|
||||
if (po >= lim) {
|
||||
now++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (now > ma) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
rmax(ma, suf[po]);
|
||||
rmin(lim, po);
|
||||
now++;
|
||||
}
|
||||
|
||||
if (lim != 0) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2171/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vi p(n);
|
||||
cin >> p;
|
||||
|
||||
vi suf(n + 1);
|
||||
vi pos(n);
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
suf[i] = max(suf[i + 1], p[i]);
|
||||
pos[p[i] - 1] = i;
|
||||
}
|
||||
|
||||
V<pair<int, int>> ans;
|
||||
|
||||
int now = 1;
|
||||
int lim = n;
|
||||
int ma = 1;
|
||||
while (now < n) {
|
||||
int ps = pos[now - 1];
|
||||
if (now > ma) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (ps >= lim) {
|
||||
now++;
|
||||
continue;
|
||||
}
|
||||
|
||||
nrep(i, ps + 1, n) {
|
||||
if (p[i] < now) {
|
||||
break;
|
||||
}
|
||||
|
||||
ans.emplace_back(p[i], now);
|
||||
}
|
||||
if (now != ma) {
|
||||
ans.emplace_back(ma, now);
|
||||
}
|
||||
|
||||
lim = ps;
|
||||
rmax(ma, suf[ps]);
|
||||
}
|
||||
|
||||
if (ans.size() < n - 1) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Yes\n";
|
||||
repv(i, ans) {
|
||||
cout << i.first << ' ' << i.second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2157/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 count(n + 1);
|
||||
rep(i, n) {
|
||||
int num;
|
||||
cin >> num;
|
||||
count[num]++;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
rep(i, n + 1) {
|
||||
if (count[i] < i) {
|
||||
ans += count[i];
|
||||
continue;
|
||||
}
|
||||
|
||||
ans += count[i] - i;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
154
Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp
Normal file
154
Codeforces Round 1066 (Div. 1 + Div. 2)/B. Expansion Plan 2.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2157/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;
|
||||
ll x, y;
|
||||
cin >> n >> x >> y;
|
||||
x = abs(x);
|
||||
y = abs(y);
|
||||
|
||||
ll ort = min(x, y);
|
||||
ll right = x - ort;
|
||||
ll left = y - ort;
|
||||
|
||||
rep(i, n) {
|
||||
char op;
|
||||
cin >> op;
|
||||
|
||||
if (op == '4') {
|
||||
if (right) {
|
||||
right--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (left) {
|
||||
left--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ort) {
|
||||
ort--;
|
||||
right++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ort) {
|
||||
ort--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (right) {
|
||||
right--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (left) {
|
||||
left--;
|
||||
}
|
||||
}
|
||||
|
||||
cout << (ort == 0 && left == 0 && right == 0 ? "YES\n" : "NO\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
158
Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp
Normal file
158
Codeforces Round 1066 (Div. 1 + Div. 2)/C. Meximum Array 2.cpp
Normal file
@@ -0,0 +1,158 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2157/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, k, q;
|
||||
cin >> n >> k >> q;
|
||||
|
||||
V<bool> valid(n);
|
||||
V<bool> inv(n);
|
||||
V<pair<int, int>> adj;
|
||||
while (q--) {
|
||||
int c, l, r;
|
||||
cin >> c >> l >> r;
|
||||
l--, r--;
|
||||
|
||||
if (c == 1) {
|
||||
nrep(i, l, r + 1) {
|
||||
inv[i] = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
adj.emplace_back(l, r);
|
||||
|
||||
nrep(i, l, r + 1) {
|
||||
valid[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
sortv(adj);
|
||||
|
||||
vi ans(n);
|
||||
rep(i, n) {
|
||||
if (!valid[i]) {
|
||||
ans[i] = k;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (valid[i] && inv[i]) {
|
||||
ans[i] = n + 1;
|
||||
}
|
||||
}
|
||||
|
||||
repv(i, adj) {
|
||||
vi us;
|
||||
nrep(j, i.first, i.second + 1) {
|
||||
if (!inv[j]) {
|
||||
us.push_back(j);
|
||||
}
|
||||
}
|
||||
|
||||
nrep(j, 1, k) {
|
||||
ans[us[j]] = (ans[us[j - 1]] + 1) % k;
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
111
Codeforces Round 1067 (Div. 2)/A. Suspension.cpp
Normal file
111
Codeforces Round 1067 (Div. 2)/A. Suspension.cpp
Normal file
@@ -0,0 +1,111 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2158/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;
|
||||
int y, r;
|
||||
cin >> y >> r;
|
||||
|
||||
cout << min(y / 2 + r, n) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
150
Codeforces Round 1067 (Div. 2)/B. Split.cpp
Normal file
150
Codeforces Round 1067 (Div. 2)/B. Split.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2158/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;
|
||||
n <<= 1;
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
sortv(a);
|
||||
vi count = {1};
|
||||
nrep(i, 1, n) {
|
||||
if (a[i] != a[i - 1]) {
|
||||
count.emplace_back();
|
||||
}
|
||||
|
||||
count.back()++;
|
||||
}
|
||||
|
||||
if (count.size() == 1) {
|
||||
cout << (((n >> 1) & 1) << 1) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
int c = 0;
|
||||
rep(i, count.size()) {
|
||||
if (count[i] & 1) {
|
||||
ans++;
|
||||
continue;
|
||||
}
|
||||
|
||||
c++;
|
||||
}
|
||||
|
||||
if (ans > 0) {
|
||||
c <<= 1;
|
||||
} else {
|
||||
if (((n >> 1) & 1)) {
|
||||
c -= ~c & 1;
|
||||
c <<= 1;
|
||||
} else {
|
||||
c -= c & 1;
|
||||
c <<= 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();
|
||||
}
|
||||
}
|
||||
147
Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp
Normal file
147
Codeforces Round 1067 (Div. 2)/C. Annoying Game.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2158/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, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl a(n);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
|
||||
if (n == 1) {
|
||||
cout << a[0] + b[0] * (k & 1) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
vl pref(n);
|
||||
vl suf(n);
|
||||
|
||||
ll kanp = 0;
|
||||
ll kans = 0;
|
||||
|
||||
ll ans = -OO;
|
||||
|
||||
rep(i, n) {
|
||||
kanp += a[i];
|
||||
kans += a[n - i - 1];
|
||||
|
||||
ans = max({ans, kanp, kans});
|
||||
|
||||
rmax(kanp, 0LL);
|
||||
rmax(kans, 0LL);
|
||||
|
||||
pref[i] = kanp;
|
||||
suf[n - i - 1] = kans;
|
||||
}
|
||||
|
||||
if (k & 1) {
|
||||
rmax(ans, suf[1] + b[0] + a[0]);
|
||||
rmax(ans, pref[n - 2] + b[n - 1] + a[n - 1]);
|
||||
nrep(i, 1, n - 1) {
|
||||
rmax(ans, pref[i - 1] + suf[i + 1] + a[i] + b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
186
Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp
Normal file
186
Codeforces Round 1067 (Div. 2)/D. Palindrome Flipping.cpp
Normal file
@@ -0,0 +1,186 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2158/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;
|
||||
|
||||
|
||||
map<string, pair<V<pair<int, int>>, char>> ops;
|
||||
map<string, V<pair<int, int>>> getlast[2];
|
||||
void pre()
|
||||
{
|
||||
ops["0000"] = {{}, '0'};
|
||||
ops["0001"] = {{{1, 3}}, '1'};
|
||||
ops["0010"] = {{{1, 2}, {1, 3}}, '0'};
|
||||
ops["0011"] = {{{1, 2}}, '1'};
|
||||
ops["0100"] = {{{3, 4}, {2, 4}}, '0'};
|
||||
ops["0101"] = {{{1, 3}, {3, 4}, {2, 4}}, '1'};
|
||||
ops["0110"] = {{{2, 3}}, '0'};
|
||||
ops["0111"] = {{{2, 4}}, '0'};
|
||||
ops["1000"] = {{{2, 4}}, '1'};
|
||||
ops["1001"] = {{{2, 3}}, '1'};
|
||||
ops["1010"] = {{{1, 3}, {3, 4}, {2, 4}}, '0'};
|
||||
ops["1011"] = {{{3, 4}, {2, 4}}, '1'};
|
||||
ops["1100"] = {{{1, 2}}, '0'};
|
||||
ops["1101"] = {{{1, 2}, {1, 3}}, '1'};
|
||||
ops["1110"] = {{{1, 3}}, '0'};
|
||||
ops["1111"] = {{}, '1'};
|
||||
|
||||
getlast[0]["0000"] = {};
|
||||
getlast[0]["0001"] = {{1, 4}, {1, 3}};
|
||||
getlast[0]["0010"] = {{1, 3}, {1, 2}};
|
||||
getlast[0]["0011"] = {{3, 4}};
|
||||
getlast[0]["0100"] = {{2, 4}, {3, 4}};
|
||||
getlast[0]["0101"] = {{1, 3}, {1, 2}, {2, 4}};
|
||||
getlast[0]["0110"] = {{2, 3}};
|
||||
getlast[0]["0111"] = {{2, 4}};
|
||||
getlast[0]["1000"] = {{1, 4}, {2, 4}};
|
||||
getlast[0]["1001"] = {{2, 3}, {1, 4}};
|
||||
getlast[0]["1010"] = {{2, 4}, {3, 4}, {1, 3}};
|
||||
getlast[0]["1011"] = {{1, 4}, {2, 4}, {3, 4}};
|
||||
getlast[0]["1100"] = {{1, 2}};
|
||||
getlast[0]["1101"] = {{1, 4}, {1, 3}, {1, 2}};
|
||||
getlast[0]["1110"] = {{1, 3}};
|
||||
getlast[0]["1111"] = {{1, 4}};
|
||||
|
||||
getlast[1]["1111"] = {};
|
||||
getlast[1]["1110"] = {{1, 4}, {1, 3}};
|
||||
getlast[1]["1101"] = {{1, 3}, {1, 2}};
|
||||
getlast[1]["1100"] = {{3, 4}};
|
||||
getlast[1]["1011"] = {{2, 4}, {3, 4}};
|
||||
getlast[1]["1010"] = {{1, 3}, {1, 2}, {2, 4}};
|
||||
getlast[1]["1001"] = {{2, 3}};
|
||||
getlast[1]["1000"] = {{2, 4}};
|
||||
getlast[1]["0111"] = {{1, 4}, {2, 4}};
|
||||
getlast[1]["0110"] = {{2, 3}, {1, 4}};
|
||||
getlast[1]["0101"] = {{2, 4}, {3, 4}, {1, 3}};
|
||||
getlast[1]["0100"] = {{1, 4}, {2, 4}, {3, 4}};
|
||||
getlast[1]["0011"] = {{1, 2}};
|
||||
getlast[1]["0010"] = {{1, 4}, {1, 3}, {1, 2}};
|
||||
getlast[1]["0001"] = {{1, 3}};
|
||||
getlast[1]["0000"] = {{1, 4}};
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
auto [ans, cur] = ops[a.substr(0, 4)];
|
||||
|
||||
nrep(i, 4, n) {
|
||||
if (a[i] != cur) {
|
||||
ans.emplace_back(1, i);
|
||||
cur = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = n - 1; i > 3; i--) {
|
||||
if (b[i] != cur) {
|
||||
ans.emplace_back(1, i + 1);
|
||||
cur = b[i];
|
||||
}
|
||||
}
|
||||
|
||||
auto bf = getlast[cur - '0'][b.substr(0, 4)];
|
||||
repv(i, bf) {
|
||||
ans.emplace_back(i);
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
repv(i, ans) {
|
||||
cout << i.first << ' ' << i.second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
128
Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp
Normal file
128
Codeforces Round 1068 (Div. 2)/A. Sleeping Through Classes.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2173/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, k;
|
||||
cin >> n >> k;
|
||||
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
int c = 0;
|
||||
int ans = 0;
|
||||
rep(i, n) {
|
||||
if (c == 0 && a[i] == '0') {
|
||||
ans++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a[i] == '1') {
|
||||
c = k;
|
||||
continue;
|
||||
}
|
||||
|
||||
c--;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
122
Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp
Normal file
122
Codeforces Round 1068 (Div. 2)/B. Niko's Tactical Cards.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2173/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;
|
||||
|
||||
vl a(n);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
|
||||
vvl dp(n, vl(2));
|
||||
dp[0][0] = max(-a[0], b[0]);
|
||||
dp[0][1] = min(-a[0], b[0]);
|
||||
|
||||
nrep(i, 1, n) {
|
||||
dp[i][0] = max(b[i] - dp[i - 1][1], dp[i - 1][0] - a[i]);
|
||||
dp[i][1] = min(b[i] - dp[i - 1][0], dp[i - 1][1] - a[i]);
|
||||
}
|
||||
|
||||
cout << dp[n - 1][0] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
174
Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp
Normal file
174
Codeforces Round 1068 (Div. 2)/C. Kanade's Perfect Multiples.cpp
Normal file
@@ -0,0 +1,174 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2173/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;
|
||||
ll k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
sortv(a);
|
||||
a.erase(unique(all(a)), a.end());
|
||||
n = a.size();
|
||||
|
||||
ll lim = (k + n - 1) / n / 2;
|
||||
if (a[0] < lim) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
set<ll> all;
|
||||
rep(i, n) {
|
||||
all.insert(a[i]);
|
||||
}
|
||||
|
||||
set<ll> ans;
|
||||
rep(i, n) {
|
||||
auto test = [&]() {
|
||||
for (ll j = a[i]; j <= k; j += a[i]) {
|
||||
if (!all.count(j)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
if (test()) {
|
||||
ans.insert(a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
auto itr = ans.begin();
|
||||
while (itr != ans.end()) {
|
||||
bool valid = false;
|
||||
|
||||
for (ll j = *itr; j <= k; j += *itr) {
|
||||
auto tmp = all.find(j);
|
||||
if (tmp != all.end()) {
|
||||
all.erase(tmp);
|
||||
valid = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!valid) {
|
||||
itr = ans.erase(itr);
|
||||
continue;
|
||||
}
|
||||
|
||||
itr++;
|
||||
}
|
||||
|
||||
if (!all.empty()) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
repv(i, ans) {
|
||||
cout << i << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
215
Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp
Normal file
215
Codeforces Round 1068 (Div. 2)/D. Taiga's Carry Chains.cpp
Normal file
@@ -0,0 +1,215 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2173/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()
|
||||
{
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
if (k == 0) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
// vi count(n);
|
||||
// int c = 0;
|
||||
// nrep(i, 1, 30) {
|
||||
// if (~(n >> i) & 1) {
|
||||
// count.push_back(c);
|
||||
// c = 0;
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// c++;
|
||||
// }
|
||||
//
|
||||
// count.push_back(c);
|
||||
// sort(all(count), greater<>());
|
||||
//
|
||||
// ll ans = 0;
|
||||
//
|
||||
|
||||
int act = min(k, 30LL);
|
||||
|
||||
// vvvl memo(31, vvl(act + 1, vl(2, -1)));
|
||||
//
|
||||
// function<ll(int, int, int)> dp = [&](int i, int k, int c) {
|
||||
// if (i >= 31) {
|
||||
// return 0LL;
|
||||
// }
|
||||
//
|
||||
// ll &ans = memo[i][k][c];
|
||||
// if (ans != -1) {
|
||||
// return ans;
|
||||
// }
|
||||
//
|
||||
// if (c == 1 && ((n >> i) & 1)) {
|
||||
// return ans = dp(i + 1, k, 1) + 1;
|
||||
// }
|
||||
//
|
||||
// if (k == 0) {
|
||||
// return ans = 0LL;
|
||||
// }
|
||||
//
|
||||
// if (((n >> i) & 1) || c == 1) {
|
||||
// return ans = max(dp(i + 1, k, 0), dp(i + 1, k - 1, 1) + 1);
|
||||
// }
|
||||
//
|
||||
// return ans = dp(i + 1, k, 0);
|
||||
// };
|
||||
|
||||
// dp(0, act, 0);
|
||||
|
||||
vvvl dp(31, vvl(act + 1, vl(2, -OO)));
|
||||
dp[0][0][0] = 0;
|
||||
|
||||
if (n & 1) {
|
||||
dp[0][1][1] = 1;
|
||||
}
|
||||
|
||||
nrep(i, 1, 31) {
|
||||
dp[i][0][0] = dp[i - 1][0][0];
|
||||
if ((n >> i) & 1) {
|
||||
dp[i][1][1] = max(dp[i - 1][1][1], dp[i - 1][0][0]) + 1;
|
||||
} else {
|
||||
dp[i][1][0] = max(dp[i - 1][1][1], dp[i - 1][0][0]);
|
||||
}
|
||||
|
||||
nrep(j, 1, act + 1) {
|
||||
dp[i][j][0] = max(dp[i - 1][j][0], dp[i - 1][j][1]);
|
||||
|
||||
if ((n >> i) & 1) {
|
||||
dp[i][j][1] = max(dp[i - 1][j - 1][0], dp[i - 1][j][1]) + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[i][j][1] = dp[i - 1][j - 1][1] + 1;
|
||||
if (j > 1) {
|
||||
rmax(dp[i][j][1], dp[i - 1][j - 2][0] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, 31) {
|
||||
rep(j, act + 1) {
|
||||
rmax(ans, dp[i][j][0] + k - j);
|
||||
rmax(ans, dp[i][j][1] + k - j);
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
|
||||
// rep(i, 30) {
|
||||
// rep(j, act + 1) {
|
||||
// if (memo[i][j][0] != -1) {
|
||||
// rmax(ans, memo[i][j][0] + k - j);
|
||||
// }
|
||||
//
|
||||
// if (memo[i][j][1] != -1) {
|
||||
// rmax(ans, memo[i][j][1] + k - j);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
147
Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp
Normal file
147
Codeforces Round 1069 (Div. 1)/B. Wishing Cards.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2174/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, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
V<pair<int, int>> act = {{0, 0}, {1, a[0]}};
|
||||
int cur = a[0];
|
||||
nrep(i, 1, n) {
|
||||
if (cur >= a[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
act.emplace_back(i + 1, a[i]);
|
||||
cur = a[i];
|
||||
}
|
||||
|
||||
act.emplace_back(n + 1, 0);
|
||||
|
||||
vvvl dp(act.size(), vvl(k + 1, vl(k + 1)));
|
||||
ll ans = 0;
|
||||
|
||||
vvl maximal(act.size(), vl(k + 1));
|
||||
|
||||
int c = 1;
|
||||
|
||||
nrep(i, 1, act.size() - 1) {
|
||||
int lim = act[i].second;
|
||||
int dis = act[i].first - act[i - 1].first;
|
||||
|
||||
rep(j, k + 1) {
|
||||
rep(l, min(act[i].second, j) + 1) {
|
||||
dp[i][j][l] = max(maximal[i - 1][j - l] + l, dp[i - 1][j][l] + l * (dp[i - 1][j][l] != 0) * dis);
|
||||
ll next = 0;
|
||||
next = act[i + 1].first - act[i].first - 1;
|
||||
rmax(maximal[i][j], dp[i][j][l] + l * next);
|
||||
rmax(ans, maximal[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
148
Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp
Normal file
148
Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2175/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()
|
||||
{
|
||||
string a, b;
|
||||
cin >> a >> b;
|
||||
|
||||
vi count(26);
|
||||
vi count2(26);
|
||||
|
||||
repv(i, a) {
|
||||
count2[i - 'a']++;
|
||||
}
|
||||
|
||||
repv(i, b) {
|
||||
count[i - 'a']++;
|
||||
}
|
||||
|
||||
rep(i, 26) {
|
||||
if (count[i] < count2[i]) {
|
||||
cout << "Impossible\n";
|
||||
return;
|
||||
}
|
||||
count[i] -= count2[i];
|
||||
}
|
||||
|
||||
int cur = 0;
|
||||
string ans;
|
||||
|
||||
rep(i, 26) {
|
||||
while (cur < a.size() && a[cur] <= i + 'a') {
|
||||
ans.push_back(a[cur]);
|
||||
cur++;
|
||||
}
|
||||
|
||||
while (count[i]) {
|
||||
count[i]--;
|
||||
ans.push_back(i + 'a');
|
||||
}
|
||||
}
|
||||
|
||||
while (cur < a.size()) {
|
||||
ans.push_back(a[cur]);
|
||||
cur++;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
125
Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp
Normal file
125
Codeforces Round 1070 (Div. 2)/A. Operations with Inversions.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2176/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 now = n;
|
||||
int ans = 0;
|
||||
while (now > 1) {
|
||||
rep(i, a.size()) {
|
||||
if (a[i] == now) {
|
||||
while (i < a.size() - 1 && a[i + 1] < a[i]) {
|
||||
a.erase(a.begin() + i + 1);
|
||||
ans++;
|
||||
}
|
||||
}
|
||||
}
|
||||
now--;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
137
Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp
Normal file
137
Codeforces Round 1070 (Div. 2)/B. Optimal Shifts.cpp
Normal file
@@ -0,0 +1,137 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2176/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;
|
||||
string a;
|
||||
cin >> n >> a;
|
||||
|
||||
int maximal = 0;
|
||||
|
||||
int first = a.find('1');
|
||||
int prev = first;
|
||||
|
||||
nrep(i, 1, n) {
|
||||
if (a[(first + i) % n] == '1') {
|
||||
prev = (first + i) % n;
|
||||
continue;
|
||||
}
|
||||
|
||||
rmax(maximal, (((first + i) % n - prev) % n + n) % n);
|
||||
}
|
||||
|
||||
if (maximal == 0) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
int cur = 1;
|
||||
while (cur > maximal) {
|
||||
maximal -= cur;
|
||||
ans += cur;
|
||||
cur <<= 1;
|
||||
}
|
||||
|
||||
cout << ans + maximal << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
157
Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp
Normal file
157
Codeforces Round 1070 (Div. 2)/C. Odd Process.cpp
Normal file
@@ -0,0 +1,157 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2176/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;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
vl even;
|
||||
vl odd;
|
||||
|
||||
rep(i, n) {
|
||||
if (a[i] & 1) {
|
||||
odd.push_back(a[i]);
|
||||
} else {
|
||||
even.push_back(a[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (odd.empty()) {
|
||||
rep(i, n) {
|
||||
cout << "0 ";
|
||||
}
|
||||
cout << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
sortv(even);
|
||||
sortv(odd);
|
||||
|
||||
ll ans = odd.back();
|
||||
ll cur = 0;
|
||||
cout << ans;
|
||||
rep(i, even.size()) {
|
||||
cur += even[even.size() - i - 1];
|
||||
cout << ' ' << ans + cur;
|
||||
}
|
||||
|
||||
int tmp = n - 1 - even.size();
|
||||
|
||||
for (int i = 1; tmp > 1; tmp -= 2, i++) {
|
||||
if (even.size() >= 1) {
|
||||
cout << ' ' << odd.back() + cur - even[0] << ' ';
|
||||
} else {
|
||||
cout << " 0 ";
|
||||
}
|
||||
cout << odd.back() + cur;
|
||||
}
|
||||
|
||||
if (tmp == 1) {
|
||||
cout << " 0";
|
||||
}
|
||||
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
164
Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp
Normal file
164
Codeforces Round 1070 (Div. 2)/D. Fibonacci Paths.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2176/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, m;
|
||||
cin >> n >> m;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
const ll mod = 998244353;
|
||||
|
||||
V<V<pair<ll, int>>> graph(n);
|
||||
vvl inv(n);
|
||||
|
||||
while (m--) {
|
||||
int u, v;
|
||||
cin >> u >> v;
|
||||
u--, v--;
|
||||
graph[u].emplace_back(a[v], v);
|
||||
inv[v].push_back(a[u]);
|
||||
}
|
||||
|
||||
V<map<ll, vi>> act(n);
|
||||
|
||||
rep(now, n) {
|
||||
sortv(graph[now]);
|
||||
sortv(inv[now]);
|
||||
inv[now].erase(unique(all(inv[now])), inv[now].end());
|
||||
|
||||
repv(j, inv[now]) {
|
||||
auto itr = lower_bound(all(graph[now]), make_pair(j + a[now], -1));
|
||||
|
||||
while (itr != graph[now].end() && itr->first == j + a[now]) {
|
||||
act[now][j].push_back(itr->second);
|
||||
itr++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
V<map<ll, ll>> memo(n);
|
||||
|
||||
function<ll(int, ll)> dp = [&](int i, ll prev) {
|
||||
auto itr = memo[i].find(prev);
|
||||
if (itr != memo[i].end()) {
|
||||
return itr->second;
|
||||
}
|
||||
|
||||
ll &ans = memo[i][prev];
|
||||
|
||||
repv(j, act[i][prev]) {
|
||||
ans = (ans + dp(j, a[i]) + 1) % mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
repv(j, graph[i]) {
|
||||
ans = (ans + dp(j.second, a[i]) + 1) % mod;
|
||||
}
|
||||
}
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
126
Codeforces Round 1073 (Div. 1)/B1. Sub-RBS (Easy Version).cpp
Normal file
126
Codeforces Round 1073 (Div. 1)/B1. Sub-RBS (Easy Version).cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
110
Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp
Normal file
110
Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2185/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;
|
||||
nrep(i, 1, n + 1) {
|
||||
cout << i * i << " \n"[i == n];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
155
Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp
Normal file
155
Codeforces Round 1074 (Div. 4)/F. BattleCows.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2185/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
int sz = 1 << n;
|
||||
|
||||
vl a(sz);
|
||||
cin >> a;
|
||||
|
||||
vl an(sz * 2);
|
||||
|
||||
function<ll(int)> calc = [&](int i) {
|
||||
if (i >= sz) {
|
||||
an[i] = a[i - sz];
|
||||
return an[i];
|
||||
}
|
||||
|
||||
ll a1 = calc(i * 2);
|
||||
ll a2 = calc(i * 2 + 1);
|
||||
|
||||
an[i] = a1 ^ a2;
|
||||
return an[i];
|
||||
};
|
||||
|
||||
calc(1);
|
||||
|
||||
while (q--) {
|
||||
int b, c;
|
||||
cin >> b >> c;
|
||||
|
||||
ll ans = 0;
|
||||
ll cur = a[b - 1];
|
||||
bool add = false;
|
||||
int st = 0;
|
||||
b += sz;
|
||||
b--;
|
||||
|
||||
while (b > 1) {
|
||||
int now = b & 1;
|
||||
b >>= 1;
|
||||
|
||||
ll cur = an[(b << 1) + (now ^ 1)];
|
||||
if (cur > c || (cur == c && now)) {
|
||||
ans += 1 << st;
|
||||
}
|
||||
|
||||
st++;
|
||||
c ^= cur;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
121
Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp
Normal file
121
Codeforces Round 1076 (Div. 3)/A. DBMB and the Array.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2193/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, s, x;
|
||||
cin >> n >> s >> x;
|
||||
|
||||
int cur = 0;
|
||||
while (n--) {
|
||||
int x;
|
||||
cin >> x;
|
||||
cur += x;
|
||||
}
|
||||
|
||||
if (cur > s || (cur - s) % x) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
130
Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp
Normal file
130
Codeforces Round 1076 (Div. 3)/B. Reverse a Permutation.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2193/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 p(n);
|
||||
cin >> p;
|
||||
int cur = n;
|
||||
int inv = n;
|
||||
rep(i, n) {
|
||||
if (p[i] == cur) {
|
||||
cur--;
|
||||
continue;
|
||||
}
|
||||
|
||||
inv = i;
|
||||
break;
|
||||
}
|
||||
|
||||
int l = inv;
|
||||
while (l < n && p[l] != cur) {
|
||||
l++;
|
||||
}
|
||||
|
||||
reverse(p.begin() + inv, p.begin() + l + 1);
|
||||
|
||||
cout << p;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
127
Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp
Normal file
127
Codeforces Round 1076 (Div. 3)/C. Replace and Sum.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2193/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, q;
|
||||
cin >> n >> q;
|
||||
vl a(n);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
rmax(a[n - 1], b[n - 1]);
|
||||
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
a[i] = max({a[i], a[i + 1], b[i]});
|
||||
}
|
||||
|
||||
vl pref(n + 1);
|
||||
nrep(i, 1, n + 1) {
|
||||
pref[i] = pref[i - 1] + a[i - 1];
|
||||
}
|
||||
|
||||
while (q--) {
|
||||
int l, r;
|
||||
cin >> l >> r;
|
||||
cout << pref[r] - pref[l - 1] << ' ';
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
138
Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp
Normal file
138
Codeforces Round 1076 (Div. 3)/D. Monster Game.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2193/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;
|
||||
vl a(n);
|
||||
vi b(n);
|
||||
cin >> a >> b;
|
||||
|
||||
priority_queue<ll> s;
|
||||
repv(i, a) {
|
||||
s.push(i);
|
||||
}
|
||||
|
||||
ll best = OO;
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
while (b[i]) {
|
||||
if (s.empty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
rmin(best, s.top());
|
||||
s.pop();
|
||||
b[i]--;
|
||||
}
|
||||
|
||||
if (b[i]) {
|
||||
break;
|
||||
}
|
||||
|
||||
rmax(ans, (i + 1) * best);
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
127
Codeforces Round 1079 (Div. 2)/A. Friendly Numbers.cpp
Normal file
127
Codeforces Round 1079 (Div. 2)/A. Friendly Numbers.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
132
Codeforces Round 1079 (Div. 2)/B. Array and Permutation.cpp
Normal file
132
Codeforces Round 1079 (Div. 2)/B. Array and Permutation.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
120
Codeforces Round 1079 (Div. 2)/C. Game with a Fraction.cpp
Normal file
120
Codeforces Round 1079 (Div. 2)/C. Game with a Fraction.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
112
Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp
Normal file
112
Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2195/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;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
cout << (count(all(a), 67) ? "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();
|
||||
}
|
||||
}
|
||||
152
Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp
Normal file
152
Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp
Normal file
@@ -0,0 +1,152 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2195/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;
|
||||
|
||||
vvi s;
|
||||
vi st;
|
||||
V<bool> vis(n);
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
s.emplace_back();
|
||||
st.push_back(i);
|
||||
for (int j = i; j < n; j = (j + 1) * 2 - 1) {
|
||||
vis[j] = true;
|
||||
s.back().push_back(j + 1);
|
||||
}
|
||||
}
|
||||
|
||||
repv(i, s) {
|
||||
sort(all(i), [&](int i, int j){
|
||||
return a[i - 1] < a[j - 1];
|
||||
});
|
||||
}
|
||||
|
||||
vi ans(n);
|
||||
|
||||
rep(i, s.size()) {
|
||||
int start = st[i];
|
||||
auto &v = s[i];
|
||||
|
||||
for (int j = 0; j < v.size(); j++, start = (start + 1) * 2 - 1) {
|
||||
ans[start] = a[v[j] - 1];
|
||||
}
|
||||
}
|
||||
|
||||
nrep(i, 1, n) {
|
||||
if (ans[i] < ans[i - 1]) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
128
Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp
Normal file
128
Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2195/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;
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
vvl dp(n + 1, vl(6, OO));
|
||||
fill(all(dp[0]), 0);
|
||||
|
||||
nrep(i, 1, n + 1) {
|
||||
int cur = a[i - 1] - 1;
|
||||
|
||||
rep(j, 6) {
|
||||
int add = cur != j;
|
||||
rep(k, 6) {
|
||||
if (k == j || k == 6 - j - 1) {
|
||||
continue;
|
||||
}
|
||||
rmin(dp[i][j], add + dp[i - 1][k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << *min_element(all(dp.back())) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
123
Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp
Normal file
123
Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2195/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;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
ll sum = a[0] + a.back();
|
||||
for (int i = n - 1; i > 0; i--) {
|
||||
a[i] = a[i - 1] - a[i];
|
||||
}
|
||||
a[0] = sum / (n - 1);
|
||||
|
||||
vl ans(n);
|
||||
ans.back() = (a[0] + a.back()) / 2;
|
||||
rep(i, n - 1) {
|
||||
ans[i] = (a[i] - a[i + 1]) / 2;
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
147
Codeforces Round 1080 (Div. 3)/E. Idiot First Search.cpp
Normal file
147
Codeforces Round 1080 (Div. 3)/E. Idiot First Search.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2195/problem/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<array<int, 2>> graph(n);
|
||||
rep(i, n) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
graph[i][0] = a - 1;
|
||||
graph[i][1] = b - 1;
|
||||
}
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
vl ans(n);
|
||||
|
||||
function<ll(int)> dfs = [&](int i) {
|
||||
if (graph[i][0] == -1) {
|
||||
return ans[i] = 1;
|
||||
}
|
||||
|
||||
return ans[i] = (dfs(graph[i][0]) + dfs(graph[i][1]) + 3) % mod;
|
||||
};
|
||||
|
||||
dfs(0);
|
||||
|
||||
function<void(int, int)> dfs2 = [&](int i, int p) {
|
||||
ans[i] += ans[p];
|
||||
ans[i] %= mod;
|
||||
|
||||
if (graph[i][0] == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
dfs2(graph[i][0], i);
|
||||
dfs2(graph[i][1], i);
|
||||
};
|
||||
|
||||
if (graph[0][0] != -1) {
|
||||
dfs2(graph[0][0], 0);
|
||||
dfs2(graph[0][1], 0);
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
123
Codeforces Round 1082 (Div. 2)/A. Parkour Design.cpp
Normal file
123
Codeforces Round 1082 (Div. 2)/A. Parkour Design.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
143
Codeforces Round 1082 (Div. 2)/B. ABAB Construction.cpp
Normal file
143
Codeforces Round 1082 (Div. 2)/B. ABAB Construction.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
197
Codeforces Round 1083 (Div. 2)/C. Simons and Posting Blogs.cpp
Normal file
197
Codeforces Round 1083 (Div. 2)/C. Simons and Posting Blogs.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
172
Codeforces Round 1083 (Div. 2)/D. Simons and Beating Peaks.cpp
Normal file
172
Codeforces Round 1083 (Div. 2)/D. Simons and Beating Peaks.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
111
Codeforces Round 1084 (Div. 3)/A. Eating Game.cpp
Normal file
111
Codeforces Round 1084 (Div. 3)/A. Eating Game.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
118
Codeforces Round 1084 (Div. 3)/B. Deletion Sort.cpp
Normal file
118
Codeforces Round 1084 (Div. 3)/B. Deletion Sort.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
131
Codeforces Round 1084 (Div. 3)/C. Specialty String.cpp
Normal file
131
Codeforces Round 1084 (Div. 3)/C. Specialty String.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
150
Codeforces Round 1084 (Div. 3)/D. Portal.cpp
Normal file
150
Codeforces Round 1084 (Div. 3)/D. Portal.cpp
Normal 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();
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user