Merge branch 'master' of https://git.puffypony.party/segcolt/codeforces-solutions
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
||||
!*.cpp
|
||||
!TODO.md
|
||||
!problemlist.md
|
||||
*sync-conflict*
|
||||
|
||||
@@ -80,33 +80,31 @@ const ll OO = INT64_MAX >> 1;
|
||||
|
||||
struct pt {
|
||||
ll x, y;
|
||||
int c;
|
||||
|
||||
pt() = default;
|
||||
pt(ll x, ll y): x(x), y(y) {}
|
||||
pt(pt a, int c): x(a.x), y(a.y), c(c) {}
|
||||
|
||||
friend istream &operator >> (istream &is, pt &a) {
|
||||
is >> a.x >> a.y;
|
||||
return is;
|
||||
}
|
||||
|
||||
pt operator - (pt b) {
|
||||
return pt(x - b.x, y - b.y);
|
||||
ll operator ^ (pt b) {
|
||||
return x * b.y - y * b.x;
|
||||
}
|
||||
|
||||
ll operator * (pt b) {
|
||||
return x * b.x + y * b.y;
|
||||
}
|
||||
|
||||
ll operator ^ (pt b) {
|
||||
return x * b.y - y * b.x;
|
||||
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 - b);
|
||||
ll r = (b - a) ^ (c - a);
|
||||
return (r > 0) - (r < 0);
|
||||
}
|
||||
|
||||
@@ -117,14 +115,26 @@ ll cdot(pt a, pt b, pt c)
|
||||
|
||||
int quad(pt a)
|
||||
{
|
||||
static int q[][2] = {{0, 1}, {3, 2}};
|
||||
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 quad(a) != quad(b);
|
||||
}
|
||||
return ccw(pt(0, 0), a, b) < 0;
|
||||
}
|
||||
@@ -140,202 +150,135 @@ void solve()
|
||||
{
|
||||
int n, q;
|
||||
cin >> n >> q;
|
||||
|
||||
V<pt> pts(n + q);
|
||||
cin >> pts;
|
||||
|
||||
vl ans(q);
|
||||
|
||||
vi inv(n);
|
||||
vi va(q);
|
||||
vi ev(n + q);
|
||||
iota(all(inv), 0);
|
||||
iota(all(va), n);
|
||||
iota(all(ev), 0);
|
||||
|
||||
rep(i, n) {
|
||||
auto cmp = [&](int j, int k) {
|
||||
if (j == i) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (k == i) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);
|
||||
};
|
||||
|
||||
sort(all(va), cmp);
|
||||
sort(all(inv), cmp);
|
||||
|
||||
int sz = inv.size() - 1;
|
||||
V<pair<int, ll>> tmp;
|
||||
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, sz) {
|
||||
if (ccw(pts[i], pts[inv[j - 1]], pts[inv[j]]) == 0) {
|
||||
nrep(j, 1, p.size()) {
|
||||
if (ccw(pts[i], pts[p[j]], pts[p[j - 1]]) == 0) {
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
|
||||
tmp.emplace_back(inv[j - 1], c);
|
||||
v.emplace_back(p[j - 1], c);
|
||||
c = 1;
|
||||
}
|
||||
v.emplace_back(p.back(), c);
|
||||
return v;
|
||||
};
|
||||
|
||||
tmp.emplace_back(inv.end()[-2], c);
|
||||
vl ans(q);
|
||||
|
||||
int j = 0;
|
||||
int k = 0;
|
||||
rep(i, n) {
|
||||
vvi qp(4);
|
||||
nrep(j, n, n + q) {
|
||||
qp[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
auto valid1 = [&]() {
|
||||
auto p1 = pts[va[j]];
|
||||
if (ccw(pts[i], p1, pts[inv[0]]) > 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) > 0) {
|
||||
return false;
|
||||
vvi np(4);
|
||||
rep(j, n) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
np[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
if (ccw(pts[i], p1, pts[inv[0]]) < 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) < 0 &&
|
||||
cdot(pts[i], p1, pts[inv[0]]) > 0 && cdot(pts[i], p1, pts[inv.end()[-2]]) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
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);
|
||||
|
||||
while (j < va.size()) {
|
||||
auto p1 = pts[va[j]];
|
||||
auto p2 = pts[tmp[k % tmp.size()].first];
|
||||
if (!valid1()) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ccw(pts[i], p1, p2) > 0) {
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ll dot = cdot(pts[i], p1, p2);
|
||||
|
||||
if (dot < 0) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot > 0) {
|
||||
auto p3 = pts[tmp[(k + 1) % tmp.size()].first];
|
||||
if (ccw(pts[i], p1, p3) > 0 || cdot(pts[i], p1, p3) < 0) {
|
||||
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;
|
||||
}
|
||||
k++;
|
||||
continue;
|
||||
|
||||
if (dot > 0) {
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
|
||||
j++;
|
||||
}
|
||||
|
||||
ans[va[j] - n] += tmp[k % tmp.size()].second;
|
||||
j++;
|
||||
}
|
||||
|
||||
auto valid2 = [&]() {
|
||||
auto p1 = pts[va[j]];
|
||||
if (ccw(pts[i], p1, pts[inv[0]]) < 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ccw(pts[i], p1, pts[inv[0]]) > 0 && ccw(pts[i], p1, pts[inv.end()[-2]]) > 0 &&
|
||||
cdot(pts[i], p1, pts[inv[0]]) > 0 && cdot(pts[i], p1, pts[inv.end()[-2]]) > 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
j = va.size() - 1;
|
||||
k = tmp.size() - 1;
|
||||
|
||||
auto kpos = [&]() {
|
||||
return (k % tmp.size() + tmp.size()) % tmp.size();
|
||||
};
|
||||
|
||||
while (j >= 0) {
|
||||
auto p1 = pts[va[j]];
|
||||
auto p2 = pts[tmp[kpos()].first];
|
||||
|
||||
if (!valid2()) {
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ccw(pts[i], p1, p2) < 0) {
|
||||
k--;
|
||||
continue;
|
||||
}
|
||||
|
||||
ll dot = cdot(pts[i], p1, p2);
|
||||
|
||||
if (dot < 0) {
|
||||
j--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot > 0) {
|
||||
auto p3 = pts[tmp[((k - 1) % tmp.size() + tmp.size()) % tmp.size()].first];
|
||||
if (ccw(pts[i], p1, p3) > 0 || cdot(pts[i], p1, p3) < 0) {
|
||||
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;
|
||||
}
|
||||
k--;
|
||||
continue;
|
||||
|
||||
if (dot > 0) {
|
||||
k--;
|
||||
continue;
|
||||
}
|
||||
|
||||
j--;
|
||||
}
|
||||
};
|
||||
|
||||
ans[va[j] - n] += tmp[kpos()].second;
|
||||
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) {
|
||||
auto cmp = [&](int j, int k) {
|
||||
return polar_cmp(pts[j] - pts[i], pts[k] - pts[i]);
|
||||
};
|
||||
|
||||
sort(all(inv), cmp);
|
||||
|
||||
V<pair<int, ll>> tmp;
|
||||
int c = 1;
|
||||
nrep(j, 1, n) {
|
||||
if (ccw(pts[i], pts[inv[j - 1]], pts[inv[j]]) == 0) {
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
|
||||
tmp.emplace_back(inv[j - 1], c);
|
||||
c = 1;
|
||||
vvi np(4);
|
||||
rep(j, n) {
|
||||
np[quad(pts[j] - pts[i])].push_back(j);
|
||||
}
|
||||
|
||||
tmp.emplace_back(inv.end()[-1], c);
|
||||
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;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
int k = 0;
|
||||
while (j < tmp.size()) {
|
||||
while (k <= j) {
|
||||
k++;
|
||||
}
|
||||
};
|
||||
|
||||
auto p1 = pts[tmp[j].first];
|
||||
auto p2 = pts[tmp[k % tmp.size()].first];
|
||||
ll dot = (p1 - pts[i]) * (p2 - pts[i]);
|
||||
if (ccw(pts[i], p1, p2) >= 0 || dot < 0) {
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dot > 0) {
|
||||
k++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ans[i - n] += tmp[j].second * tmp[k % tmp.size()].second;
|
||||
j++;
|
||||
rep(j, 4) {
|
||||
getcw(cp[j], cp[(j + 1) % 4]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -93,42 +93,52 @@ void solve()
|
||||
vi p(n);
|
||||
cin >> p;
|
||||
|
||||
vi suf(n + 1);
|
||||
vi pos(n);
|
||||
rep(i, n) {
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
suf[i] = max(suf[i + 1], p[i]);
|
||||
pos[p[i] - 1] = i;
|
||||
}
|
||||
|
||||
stack<pair<int, int>> s;
|
||||
V<pair<int, int>> ans;
|
||||
|
||||
pair<int, int> big = {0, 0};
|
||||
|
||||
V<pair<int, int>> edges;
|
||||
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
while (!s.empty() && s.top().first > pos[i]) {
|
||||
edges.emplace_back(s.top().second + 1, i + 1);
|
||||
s.pop();
|
||||
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 (big.first > pos[i]) {
|
||||
edges.emplace_back(big.second + 1, i + 1);
|
||||
if (ps >= lim) {
|
||||
now++;
|
||||
continue;
|
||||
}
|
||||
|
||||
big = {pos[i], i};
|
||||
s.emplace(pos[i], i);
|
||||
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]);
|
||||
}
|
||||
|
||||
sortv(edges);
|
||||
edges.erase(unique(all(edges)), edges.end());
|
||||
|
||||
if (edges.size() != n - 1) {
|
||||
if (ans.size() < n - 1) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Yes\n";
|
||||
repv(i, edges) {
|
||||
repv(i, ans) {
|
||||
cout << i.first << ' ' << i.second << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1799/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;
|
||||
cin >> a;
|
||||
|
||||
map<char, int> c;
|
||||
repv(i, a) {
|
||||
c[i]++;
|
||||
}
|
||||
|
||||
int l = 0;
|
||||
int r = a.size() - 1;
|
||||
|
||||
auto itr = c.begin();
|
||||
while (itr != c.end()) {
|
||||
auto [ch, co] = *itr;
|
||||
itr = c.erase(itr);
|
||||
|
||||
while (co > 1) {
|
||||
a[l] = ch;
|
||||
a[r] = ch;
|
||||
l++;
|
||||
r--;
|
||||
co -= 2;
|
||||
}
|
||||
|
||||
if (co == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c.size() >= 2) {
|
||||
a[r] = ch;
|
||||
r--;
|
||||
break;
|
||||
}
|
||||
|
||||
if (c.empty()) {
|
||||
a[l] = ch;
|
||||
break;
|
||||
}
|
||||
|
||||
auto [ch2, co2] = *itr;
|
||||
itr = c.erase(itr);
|
||||
|
||||
int now = 1;
|
||||
while (co2--) {
|
||||
if (now) {
|
||||
a[l] = ch2;
|
||||
l++;
|
||||
} else {
|
||||
a[r] = ch2;
|
||||
r--;
|
||||
}
|
||||
now ^= 1;
|
||||
}
|
||||
|
||||
a[r] = ch;
|
||||
}
|
||||
|
||||
while (itr != c.end()) {
|
||||
auto [ch, co] = *itr;
|
||||
itr++;
|
||||
|
||||
while (co--) {
|
||||
a[l] = ch;
|
||||
l++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << a << '\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/1800/E1 */
|
||||
|
||||
#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, b;
|
||||
cin >> a >> b;
|
||||
|
||||
vi c1(26);
|
||||
vi c2(26);
|
||||
rep(i, n) {
|
||||
c1[a[i] - 'a']++;
|
||||
c2[b[i] - 'a']++;
|
||||
}
|
||||
|
||||
if (c1 != c2) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (n <= 3 && a != b) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (n >= 3 && n <= 5 && a[2] != b[2]) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (n >= 3 && n <= 4 && a[1] != b[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();
|
||||
}
|
||||
}
|
||||
142
Codeforces Round 855 (Div. 3)/F. Dasha and Nightmares.cpp
Normal file
142
Codeforces Round 855 (Div. 3)/F. Dasha and Nightmares.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1800/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 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<pair<int, int>> a(n);
|
||||
rep(i, n) {
|
||||
string s;
|
||||
cin >> s;
|
||||
vi c(26);
|
||||
repv(i, s) {
|
||||
c[i - 'a']++;
|
||||
}
|
||||
|
||||
rep(j, 26) {
|
||||
a[i].first |= (c[j] != 0) << j;
|
||||
a[i].second |= (c[j] & 1) << j;
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, 26) {
|
||||
vi all;
|
||||
repv(j, a) {
|
||||
if ((j.first >> i) & 1) {
|
||||
continue;
|
||||
}
|
||||
all.push_back(j.second ^ ((1 << 26) - 1) ^ (1 << i));
|
||||
}
|
||||
sortv(all);
|
||||
repv(j, a) {
|
||||
if ((j.first >> i) & 1) {
|
||||
continue;
|
||||
}
|
||||
ans += upper_bound(all(all), j.second) - lower_bound(all(all), j.second);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -78,20 +78,17 @@ auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
const ll mod = 998244353;
|
||||
|
||||
constexpr MAXN = 1e6 + 10;
|
||||
constexpr int MAXN = 1e6 + 10;
|
||||
bool prime[MAXN];
|
||||
bool vis[MAXN];
|
||||
|
||||
vl fact[MAXN];
|
||||
vl inv[MAXN];
|
||||
ll fact[MAXN];
|
||||
ll inv[MAXN];
|
||||
ll finv[MAXN];
|
||||
|
||||
ll mod = 998244353;
|
||||
|
||||
void pre()
|
||||
{
|
||||
fill(prime, prime + MAXN, true);
|
||||
prime[0] = false;
|
||||
prime[1] = false;
|
||||
|
||||
nrep(i, 2, MAXN) {
|
||||
@@ -105,50 +102,38 @@ void pre()
|
||||
}
|
||||
|
||||
fact[0] = 1;
|
||||
fact[1] = 1;
|
||||
nrep(i, 2, MAXN) {
|
||||
nrep(i, 1, MAXN) {
|
||||
fact[i] = fact[i - 1] * i % mod;
|
||||
}
|
||||
|
||||
inv[0] = 1;
|
||||
inv[1] = 1;
|
||||
nrep(i, 2, MAXN) {
|
||||
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
|
||||
}
|
||||
|
||||
nrep(i, 2, MAXN) {
|
||||
inv[i] = inv[i - 1] * inv[i] % mod;
|
||||
finv[0] = 1;
|
||||
nrep(i, 1, MAXN) {
|
||||
finv[i] = finv[i - 1] * inv[i] % mod;
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vi a(2 * n);
|
||||
cin >> a;
|
||||
|
||||
vi act(n << 1);
|
||||
cin >> act;
|
||||
|
||||
set<int> up;
|
||||
|
||||
vi ot;
|
||||
vi primes;
|
||||
|
||||
rep(i, n << 1) {
|
||||
if (prime[act[i]]) {
|
||||
if (vis[act[i]]) {
|
||||
ot.push_back(act[i]);
|
||||
} else {
|
||||
up.insert(act[i]);
|
||||
vis[act[i]] = true;
|
||||
primes.push_back(act[i]);
|
||||
}
|
||||
continue;
|
||||
map<int, ll> c;
|
||||
rep(i, 2 * n) {
|
||||
if (prime[a[i]] && !c.count(a[i])) {
|
||||
primes.push_back(a[i]);
|
||||
}
|
||||
|
||||
ot.push_back(act[i]);
|
||||
c[a[i]]++;
|
||||
}
|
||||
|
||||
if (primes.size() < n) {
|
||||
@@ -156,38 +141,27 @@ void solve()
|
||||
return;
|
||||
}
|
||||
|
||||
auto comb = [&](int n, int k) {
|
||||
return fact[n] * inv[k] % mod * inv[n - k] % mod;
|
||||
};
|
||||
|
||||
ll ans = comb(primes.size(), n);
|
||||
|
||||
if (ot.empty()) {
|
||||
cout << ans << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
sortv(ot);
|
||||
|
||||
int c = 1;
|
||||
vi tmp;
|
||||
|
||||
nrep(i, 1, ot.size()) {
|
||||
if (ot[i] == ot[i - 1]) {
|
||||
c++;
|
||||
continue;
|
||||
ll non = 1;
|
||||
repv(i, c) {
|
||||
if (!prime[i.first]) {
|
||||
non = (non * finv[i.second]) % mod;
|
||||
}
|
||||
|
||||
tmp.push_back(c);
|
||||
c = 1;
|
||||
}
|
||||
tmp.push_back(c);
|
||||
|
||||
vvl dp(primes.size() + 1, vl(n + 1));
|
||||
dp[0][0] = fact[n] * non % mod;
|
||||
|
||||
nrep(i, 1, primes.size() + 1) {
|
||||
int cur = primes[i - 1];
|
||||
ll co = c[cur];
|
||||
dp[i][0] = dp[i - 1][0] * finv[co] % mod;
|
||||
|
||||
repv(i, up) {
|
||||
vis[i] = false;
|
||||
nrep(j, 1, n + 1) {
|
||||
dp[i][j] = (dp[i - 1][j] * finv[co] % mod + dp[i - 1][j - 1] * finv[co - 1] % mod) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[primes.size()][n] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
|
||||
160
Codeforces Round 904 (Div. 2)/C. Medium Design.cpp
Normal file
160
Codeforces Round 904 (Div. 2)/C. Medium Design.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1884/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;
|
||||
|
||||
V<pair<int, int>> a(n);
|
||||
vector<int> c;
|
||||
repv(i, a) {
|
||||
cin >> i.first >> i.second;
|
||||
c.push_back(i.first);
|
||||
c.push_back(i.second);
|
||||
}
|
||||
auto itr = find(all(a), make_pair(1, m));
|
||||
if (itr != a.end()) {
|
||||
a.erase(itr);
|
||||
}
|
||||
|
||||
sortv(c);
|
||||
c.erase(unique(all(c)), c.end());
|
||||
repv(i, a) {
|
||||
i.first = lower_bound(all(c), i.first) - c.begin();
|
||||
i.second = lower_bound(all(c), i.second) - c.begin();
|
||||
}
|
||||
|
||||
int sz = c.size() + 1;
|
||||
|
||||
vi t(sz);
|
||||
vi pref(sz);
|
||||
vi suf(sz);
|
||||
repv(i, a) {
|
||||
t[i.first]++;
|
||||
t[i.second + 1]--;
|
||||
if (c[i.first] == 1) {
|
||||
pref[0]++;
|
||||
pref[i.second + 1]--;
|
||||
}
|
||||
|
||||
if (c[i.second] == m) {
|
||||
suf[sz - 1]++;
|
||||
if (i.first > 0) {
|
||||
suf[i.first - 1]--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nrep(i, 1, sz) {
|
||||
pref[i] += pref[i - 1];
|
||||
suf[sz - i - 1] += suf[sz - i];
|
||||
t[i] += t[i - 1];
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
rep(i, sz) {
|
||||
rmax(ans, t[i] - min(pref[i], suf[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,171 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1924/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, m;
|
||||
cin >> n >> k >> m;
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
vi pos(k, -1);
|
||||
vvi ne(m + 1, vi(k, -1));
|
||||
|
||||
for (int i = m - 1; i >= 0; i--) {
|
||||
ne[i + 1] = pos;
|
||||
pos[a[i] - 'a'] = i + 1;
|
||||
}
|
||||
ne[0] = pos;
|
||||
|
||||
vi memo(m + 1, -1);
|
||||
|
||||
function<int(int)> dp = [&](int i) {
|
||||
if (i == -1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int &ans = memo[i];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = oo;
|
||||
rep(j, k) {
|
||||
rmin(ans, dp(ne[i][j]) + 1);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
int c = dp(0) - 1;
|
||||
if (c >= n) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "NO\n";
|
||||
|
||||
int now = 0;
|
||||
string ans;
|
||||
while (ans.size() < n) {
|
||||
int j = 0;
|
||||
while (1) {
|
||||
if (ne[now][j] == -1) {
|
||||
while (ans.size() < n) {
|
||||
ans += j + 'a';
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (memo[ne[now][j]] == c) {
|
||||
c--;
|
||||
ans += j + 'a';
|
||||
now = ne[now][j];
|
||||
break;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
143
Codeforces Round 928 (Div. 4)/G. Vlad and Trouble at MIT.cpp
Normal file
143
Codeforces Round 928 (Div. 4)/G. Vlad and Trouble at MIT.cpp
Normal file
@@ -0,0 +1,143 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1926/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;
|
||||
cin >> n;
|
||||
|
||||
vvi graph(n);
|
||||
nrep(i, 1, n) {
|
||||
int p;
|
||||
cin >> p;
|
||||
graph[p - 1].push_back(i);
|
||||
}
|
||||
|
||||
string a;
|
||||
cin >> a;
|
||||
|
||||
vvl dp(n, vl(3));
|
||||
|
||||
function<void(int)> dfs = [&](int i) {
|
||||
if (a[i] == 'S') {
|
||||
dp[i][0] = oo;
|
||||
dp[i][2] = oo;
|
||||
}
|
||||
|
||||
if (a[i] == 'P') {
|
||||
dp[i][1] = oo;
|
||||
dp[i][2] = oo;
|
||||
}
|
||||
|
||||
repv(j, graph[i]) {
|
||||
dfs(j);
|
||||
|
||||
dp[i][0] += min({dp[j][0], dp[j][1] + 1, dp[j][2]});
|
||||
dp[i][1] += min({dp[j][0] + 1, dp[j][1], dp[j][2]});
|
||||
dp[i][2] += min({dp[j][0] + 1, dp[j][1] + 1, dp[j][2]});
|
||||
}
|
||||
};
|
||||
|
||||
dfs(0);
|
||||
|
||||
cout << *min_element(all(dp[0])) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
168
Codeforces Round 954 (Div. 3)/E. Beautiful Array.cpp
Normal file
168
Codeforces Round 954 (Div. 3)/E. Beautiful Array.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1986/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;
|
||||
ll k;
|
||||
cin >> n >> k;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
map<ll, vl> c;
|
||||
rep(i, n) {
|
||||
c[a[i] % k].push_back(a[i]);
|
||||
}
|
||||
|
||||
int oc = 0;
|
||||
repv(i, c) {
|
||||
oc += i.second.size() & 1;
|
||||
}
|
||||
|
||||
if (oc > 1) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
auto func = [&](vl &v) -> int {
|
||||
if (v.size() <= 1) {
|
||||
return 0;
|
||||
}
|
||||
int n = v.size();
|
||||
vl suf(n + 1);
|
||||
suf[n - 2] = (v.end()[-1] - v.end()[-2]) / k;
|
||||
for (int i = n - 4; i >= 0; i -= 2) {
|
||||
suf[i] = suf[i + 2] + (v[i + 1] - v[i]) / k;
|
||||
}
|
||||
|
||||
ll ans = OO;
|
||||
ll sum = 0;
|
||||
rep(i, n) {
|
||||
if (i & 1) {
|
||||
rmin(ans, (v[i + 1] - v[i - 1]) / k + suf[i + 2] + sum);
|
||||
sum += (v[i] - v[i - 1]) / k;
|
||||
continue;
|
||||
}
|
||||
|
||||
rmin(ans, sum + suf[i + 1]);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
ll ans = 0;
|
||||
repv(i, c) {
|
||||
auto &v = i.second;
|
||||
sortv(v);
|
||||
if (v.size() & 1) {
|
||||
ans += func(v);
|
||||
continue;
|
||||
}
|
||||
|
||||
nrep(j, 1, v.size()) {
|
||||
ans += (v[j] - v[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();
|
||||
}
|
||||
}
|
||||
130
Codeforces Round 969 (Div. 2)/C. Dora and C++.cpp
Normal file
130
Codeforces Round 969 (Div. 2)/C. Dora and C++.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2007/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 a, b;
|
||||
cin >> n >> a >> b;
|
||||
ll g = gcd(a, b);
|
||||
|
||||
vl v(n);
|
||||
repv(i, v) {
|
||||
cin >> i;
|
||||
i %= g;
|
||||
}
|
||||
sortv(v);
|
||||
v.erase(unique(all(v)), v.end());
|
||||
|
||||
if (v.size() == 1) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
ll ans = v.back() - v[0];
|
||||
|
||||
nrep(i, 1, v.size()) {
|
||||
rmin(ans, v[i - 1] + g - v[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,142 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1997/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 val(n);
|
||||
cin >> val;
|
||||
|
||||
vl an(n);
|
||||
|
||||
vvi graph(n);
|
||||
nrep(i, 1, n) {
|
||||
int p;
|
||||
cin >> p;
|
||||
graph[p - 1].push_back(i);
|
||||
}
|
||||
|
||||
function<ll(int)> dfs = [&](int i) {
|
||||
if (graph[i].empty()) {
|
||||
return val[i];
|
||||
}
|
||||
|
||||
ll mi = OO;
|
||||
repv(j, graph[i]) {
|
||||
rmin(mi, dfs(j));
|
||||
}
|
||||
|
||||
an[i] = mi;
|
||||
if (val[i] > mi) {
|
||||
return mi;
|
||||
}
|
||||
|
||||
ll diff = mi - val[i];
|
||||
return mi - ((diff + 1) >> 1);
|
||||
};
|
||||
|
||||
dfs(0);
|
||||
|
||||
cout << val[0] + an[0] << '\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,151 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/2086/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;
|
||||
|
||||
|
||||
const ll mod = 998244353;
|
||||
|
||||
constexpr int MAXN = 1e6;
|
||||
ll fact[MAXN];
|
||||
|
||||
void pre()
|
||||
{
|
||||
fact[0] = 1;
|
||||
nrep(i, 1, MAXN) {
|
||||
fact[i] = (fact[i - 1] * i) % mod;
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
vi c(26);
|
||||
int s = 0;
|
||||
ll mu = 1;
|
||||
repv(i, c) {
|
||||
cin >> i;
|
||||
s += i;
|
||||
mu *= fact[i];
|
||||
mu %= mod;
|
||||
}
|
||||
|
||||
int o = (s + 1) >> 1;
|
||||
|
||||
vi dp(o + 1);
|
||||
dp[0] = 1;
|
||||
rep(i, 26) {
|
||||
if (c[i] == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = o; j >= c[i]; j--) {
|
||||
dp[j] += dp[j - c[i]];
|
||||
dp[j] %= mod;
|
||||
}
|
||||
}
|
||||
|
||||
auto fpow = [&](ll a, ll p) {
|
||||
ll ans = 1;
|
||||
rep(i, 61) {
|
||||
if ((p >> i) & 1) {
|
||||
ans = (ans * a) % mod;
|
||||
}
|
||||
a = (a * a) % mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << fact[o] * fact[s >> 1] % mod * fpow(mu, mod - 2) % mod * dp[o] % mod << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
135
Pinely Round 3 (Div. 1 + Div. 2)/C. Heavy Intervals.cpp
Normal file
135
Pinely Round 3 (Div. 1 + Div. 2)/C. Heavy Intervals.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1909/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 l(n);
|
||||
vi r(n);
|
||||
vl c(n);
|
||||
cin >> l >> r >> c;
|
||||
|
||||
sortv(l);
|
||||
set<int> q;
|
||||
|
||||
rep(i, n) {
|
||||
q.insert(r[i]);
|
||||
}
|
||||
|
||||
priority_queue<ll> pq;
|
||||
for (int i = n - 1; i >= 0; i--) {
|
||||
auto itr = q.lower_bound(l[i]);
|
||||
pq.push(*itr - l[i]);
|
||||
q.erase(itr);
|
||||
}
|
||||
|
||||
sortv(c);
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
ans += c[i] * 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();
|
||||
}
|
||||
}
|
||||
4
TODO.md
4
TODO.md
@@ -48,6 +48,10 @@ Official divs from codeforces
|
||||
- [ ] [D. Reachability and Tree](https://codeforces.com/problemset/problem/2112/D)
|
||||
|
||||
I know how to solve it, but I got bored in the middle, I'll get back to it eventually.
|
||||
|
||||
- [X] [D. Even String](https://codeforces.com/problemset/problem/2086/D)
|
||||
|
||||
I think I got close, but it's harder than I thought.
|
||||
|
||||
- Global
|
||||
|
||||
|
||||
Reference in New Issue
Block a user