Compare commits
2 Commits
97ebdbf890
...
28bb38c1f8
| Author | SHA1 | Date | |
|---|---|---|---|
| 28bb38c1f8 | |||
| c4acd23d19 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@
|
||||
!/*/
|
||||
!*.cpp
|
||||
!TODO.md
|
||||
!problemlist.md
|
||||
|
||||
@@ -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,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,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,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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
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,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();
|
||||
}
|
||||
}
|
||||
@@ -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]);
|
||||
}
|
||||
|
||||
cout << maxdepth << '\n';
|
||||
if (dp[i - 1][j] >= cost) {
|
||||
rmax(dp[i][j], dp[i - 1][j] - cost);
|
||||
}
|
||||
|
||||
if (dp[i][j] >= 0) {
|
||||
maximal = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/* 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 pos(n);
|
||||
rep(i, n) {
|
||||
pos[p[i] - 1] = i;
|
||||
}
|
||||
|
||||
stack<pair<int, int>> s;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
if (big.first > pos[i]) {
|
||||
edges.emplace_back(big.second + 1, i + 1);
|
||||
continue;
|
||||
}
|
||||
|
||||
big = {pos[i], i};
|
||||
s.emplace(pos[i], i);
|
||||
}
|
||||
|
||||
sortv(edges);
|
||||
edges.erase(unique(all(edges)), edges.end());
|
||||
|
||||
if (edges.size() != n - 1) {
|
||||
cout << "No\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << "Yes\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,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();
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
145
Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp
Normal file
145
Codeforces Round 113 (Div. 2)/E. Tetrahedron.cpp
Normal file
@@ -0,0 +1,145 @@
|
||||
/* Problem URL: https://codeforces.com/contest/166/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 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
cin >> n;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size()));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[0].size()) {
|
||||
rep(k, b.size()) {
|
||||
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
auto fpow = [&]() {
|
||||
vvl ans(4, vl(4));
|
||||
vvl mu(4, vl(4, 1));
|
||||
|
||||
rep(i, 4) {
|
||||
ans[i][i] = 1;
|
||||
mu[i][i] = 0;
|
||||
}
|
||||
|
||||
rep(i, 30) {
|
||||
if ((n >> i) & 1) {
|
||||
ans = mul(ans, mu);
|
||||
}
|
||||
|
||||
mu = mul(mu, mu);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << fpow()[0][0] << '\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 185 (Div. 1)/A. The Closest Pair.cpp
Normal file
118
Codeforces Round 185 (Div. 1)/A. The Closest Pair.cpp
Normal file
@@ -0,0 +1,118 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/311/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()
|
||||
{
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
ll full = n * (n - 1) / 2;
|
||||
|
||||
if (full <= k) {
|
||||
cout << "no solution\n";
|
||||
return;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
cout << "0 " << i << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
156
Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp
Normal file
156
Codeforces Round 257 (Div. 2)/B. Jzzhu and Sequences.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/* Problem URL: https://codeforces.com/contest/450/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()
|
||||
{
|
||||
ll x, y, n;
|
||||
cin >> x >> y >> n;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
if (n == 1) {
|
||||
cout << x + mod * (x < 0) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (n == 2) {
|
||||
cout << y + mod * (y < 0) << '\n';
|
||||
return;
|
||||
}
|
||||
n -= 2;
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size()));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[0].size()) {
|
||||
rep(k, b.size()) {
|
||||
c[i][j] = ((c[i][j] + a[i][k] * b[k][j] % mod) % mod + mod) % mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
auto fpow = [&]() {
|
||||
vvl ans = {{x}, {y}};
|
||||
vvl mu(2, vl(2));
|
||||
mu[0][1] = 1;
|
||||
mu[1][0] = mod - 1;
|
||||
mu[1][1] = 1;
|
||||
|
||||
rep(i, 31) {
|
||||
if ((n >> i) & 1) {
|
||||
ans = mul(mu, ans);
|
||||
}
|
||||
|
||||
mu = mul(mu, mu);
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
auto ans = fpow();
|
||||
|
||||
cout << ans[1][0] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
160
Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp
Normal file
160
Codeforces Round 291 (Div. 2)/D. R2D2 and Droid Army.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/514/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, m;
|
||||
ll k;
|
||||
cin >> n >> m >> k;
|
||||
|
||||
vvl droid(n, vl(m));
|
||||
cin >> droid;
|
||||
|
||||
vvvl sparse(m, vvl(20, vl(n)));
|
||||
|
||||
rep(i, n) {
|
||||
rep(j, m) {
|
||||
sparse[j][0][i] = droid[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
rep(i, m) {
|
||||
nrep(j, 1, 20) {
|
||||
for (int k = 0; k + (1 << (j - 1)) < n; k++) {
|
||||
sparse[i][j][k] = max(sparse[i][j - 1][k], sparse[i][j - 1][k + (1 << (j - 1))]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto query = [&](int i, int l, int r) {
|
||||
int log = 31 - __builtin_clz(r - l + 1);
|
||||
return max(sparse[i][log][l], sparse[i][log][r - (1 << log) + 1]);
|
||||
};
|
||||
|
||||
auto calc = [&](int l, int r) {
|
||||
int ans = 0;
|
||||
rep(j, m) {
|
||||
ans += query(j, l, r);
|
||||
}
|
||||
return ans;
|
||||
};
|
||||
|
||||
int ans = 0;
|
||||
vi act(m);
|
||||
|
||||
int l = 0;
|
||||
rep(i, n) {
|
||||
while (l <= i && calc(l, i) > k) {
|
||||
l++;
|
||||
}
|
||||
|
||||
if (i - l + 1 > ans) {
|
||||
ans = i - l + 1;
|
||||
|
||||
rep(j, m) {
|
||||
act[j] = query(j, l, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << act;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
150
Codeforces Round 302 (Div. 1)/A. Writing Code.cpp
Normal file
150
Codeforces Round 302 (Div. 1)/A. Writing Code.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://codeforces.com/contest/543/problem/A */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m, b;
|
||||
ll mod;
|
||||
cin >> n >> m >> b >> mod;
|
||||
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
// vvvl dp(n + 1, vvl(m + 1, vl(b + 1)));
|
||||
// dp[0][0][0] = 1;
|
||||
//
|
||||
// nrep(i, 1, n + 1) {
|
||||
// int now = a[i - 1];
|
||||
//
|
||||
// rep(j, m + 1) {
|
||||
// nrep(k, j, m + 1) {
|
||||
// nrep(l, now * j, b + 1) {
|
||||
// dp[i][k][l] += dp[i - 1][k - j][l - now * j];
|
||||
// dp[i][k][l] %= mod;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
vvl dp(m + 1, vl(b + 1));
|
||||
dp[0][0] = 1;
|
||||
|
||||
nrep(i, 1, n + 1) {
|
||||
int now = a[i - 1];
|
||||
|
||||
rep(j, m) {
|
||||
rep(l, b + 1 - now) {
|
||||
dp[j + 1][l + now] += dp[j][l];
|
||||
dp[j + 1][l + now] %= mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
rep(i, b + 1) {
|
||||
ans += dp[m][i];
|
||||
ans %= 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();
|
||||
}
|
||||
}
|
||||
156
Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp
Normal file
156
Codeforces Round 341 (Div. 2)/E. Wet Shark and Blocks.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/* Problem URL: https://codeforces.com/contest/621/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 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, k, x;
|
||||
ll b;
|
||||
cin >> n >> b >> k >> x;
|
||||
|
||||
vi dig(n);
|
||||
cin >> dig;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
|
||||
vvl ans(x, vl(x));
|
||||
repv(j, dig) {
|
||||
ans[j%x][j%x]++;
|
||||
}
|
||||
|
||||
vvl adj(x, vl(x));
|
||||
rep(i, x) {
|
||||
repv(j, dig) {
|
||||
adj[i][(10 * i + j) % x]++;
|
||||
}
|
||||
}
|
||||
|
||||
b--;
|
||||
|
||||
auto mul = [&](vvl &a, vvl &b) {
|
||||
vvl c(a.size(), vl(b[0].size()));
|
||||
|
||||
rep(i, c.size()) {
|
||||
rep(j, c[0].size()) {
|
||||
rep(k, b.size()) {
|
||||
c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
};
|
||||
|
||||
rep(i, 30) {
|
||||
if ((b >> i) & 1) {
|
||||
ans = mul(ans, adj);
|
||||
}
|
||||
|
||||
adj = mul(adj, adj);
|
||||
}
|
||||
|
||||
ll sum = 0;
|
||||
rep(i, x) {
|
||||
sum = (sum + ans[i][k]) % mod;
|
||||
}
|
||||
|
||||
cout << sum << '\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,121 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/872/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, k;
|
||||
cin >> n >> k;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
if (k == 1) {
|
||||
cout << *min_element(all(a)) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (k == 2) {
|
||||
cout << max(a[0], a[n - 1]) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
cout << *max_element(all(a)) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
160
Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp
Normal file
160
Codeforces Round 527 (Div. 3)/F. Tree with Maximum Cost.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1092/problem/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vl cost(n);
|
||||
cin >> cost;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
vl c(n);
|
||||
vl dp(n);
|
||||
|
||||
function<void(int, int)> dfs = [&](int i, int p) {
|
||||
dp[i] = 0;
|
||||
c[i] = cost[i];
|
||||
repv(j, graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j, i);
|
||||
|
||||
c[i] += c[j];
|
||||
dp[i] += dp[j] + c[j];
|
||||
}
|
||||
};
|
||||
|
||||
dfs(0, 0);
|
||||
|
||||
ll ans = 0;
|
||||
function<void(int, int)> reroot = [&](int i, int p) {
|
||||
rmax(ans, dp[i]);
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[j] = dp[i] + c[i] - c[j] * 2;
|
||||
c[j] = c[i];
|
||||
|
||||
reroot(j, i);
|
||||
}
|
||||
};
|
||||
|
||||
reroot(0, 0);
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1190/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;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
const string win[] = {
|
||||
"cslnb\n",
|
||||
"sjfnb\n"
|
||||
};
|
||||
|
||||
if (n == 1) {
|
||||
cout << win[a[0] & 1];
|
||||
return;
|
||||
}
|
||||
|
||||
sortv(a);
|
||||
if (a[0] == 0 && a[1] == 0) {
|
||||
cout << win[0];
|
||||
return;
|
||||
}
|
||||
|
||||
map<ll, int> c;
|
||||
|
||||
rep(i, n) {
|
||||
c[a[i]]++;
|
||||
}
|
||||
|
||||
auto itr = c.begin();
|
||||
int count = 0;
|
||||
while (itr != c.end()) {
|
||||
if (itr->second >= 3 || (itr != c.begin() && itr->second == 2 && prev(itr)->first == itr->first - 1)) {
|
||||
cout << win[0];
|
||||
return;
|
||||
}
|
||||
|
||||
if (itr->second == 2) {
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count > 1) {
|
||||
cout << win[0];
|
||||
return;
|
||||
}
|
||||
|
||||
itr++;
|
||||
}
|
||||
|
||||
ll diff = 0;
|
||||
rep(i, n) {
|
||||
diff += a[i] - i;
|
||||
}
|
||||
|
||||
cout << win[diff & 1];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
139
Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp
Normal file
139
Codeforces Round 683 (Div. 1, by Meet IT)/A. Knapsack.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1446/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;
|
||||
ll w;
|
||||
cin >> n >> w;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
ll total = 0;
|
||||
vi ans;
|
||||
rep(i, n) {
|
||||
if (a[i] >= ((w + 1) >> 1) && a[i] <= w) {
|
||||
cout << "1\n";
|
||||
cout << i + 1 << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (a[i] > w) {
|
||||
continue;
|
||||
}
|
||||
|
||||
total += a[i];
|
||||
ans.push_back(i + 1);
|
||||
if (total >= ((w + 1) >> 1)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (total < ((w + 1) >> 1)) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
122
Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp
Normal file
122
Codeforces Round 831 (Div. 1 + Div. 2)/C. Bricks and Bags.cpp
Normal file
@@ -0,0 +1,122 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1740/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
sortv(a);
|
||||
ll ans = 0;
|
||||
|
||||
nrep(i, 2, n) {
|
||||
rmax(ans, a[i] - a[i - 1] + a[i] - a[0]);
|
||||
}
|
||||
|
||||
rep(i, n - 2) {
|
||||
rmax(ans, a[i + 1] - a[i] + a[n - 1] - a[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();
|
||||
}
|
||||
}
|
||||
128
Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp
Normal file
128
Codeforces Round 836 (Div. 2)/C. Almost All Multiples.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1758/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, x;
|
||||
cin >> n >> x;
|
||||
|
||||
if (n % x) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
vi ans(n);
|
||||
iota(all(ans), 1);
|
||||
ans[0] = x;
|
||||
ans.back() = 1;
|
||||
|
||||
while (x < n) {
|
||||
for (int i = x * 2; i <= n; i += x) {
|
||||
if (n % i == 0) {
|
||||
ans[x - 1] = i;
|
||||
x = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
135
Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp
Normal file
135
Codeforces Round 837 (Div. 2)/B. Hossam and Friends.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1771/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;
|
||||
|
||||
V<set<int>> nfriend(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
if (a < b) {
|
||||
swap(a, b);
|
||||
}
|
||||
nfriend[a].insert(b);
|
||||
}
|
||||
|
||||
int l = 0;
|
||||
ll ans = 0;
|
||||
rep(i, n) {
|
||||
auto inv = [&]() {
|
||||
auto itr = nfriend[i].lower_bound(l);
|
||||
return itr == nfriend[i].end();
|
||||
};
|
||||
|
||||
while (!inv()) {
|
||||
l++;
|
||||
}
|
||||
|
||||
ans += i - l + 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();
|
||||
}
|
||||
}
|
||||
232
Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp
Normal file
232
Codeforces Round 843 (Div. 2)/D. Friendly Spiders.cpp
Normal file
@@ -0,0 +1,232 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1775/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 int MAXN = 3e5 + 1;
|
||||
|
||||
vi primes;
|
||||
int di[MAXN];
|
||||
|
||||
void pre()
|
||||
{
|
||||
fill(di, di + MAXN, 1);
|
||||
|
||||
nrep(i, 2, MAXN) {
|
||||
if (di[i] != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = i; j < MAXN; j += i) {
|
||||
di[j] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 0
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
int s, t;
|
||||
cin >> s >> t;
|
||||
s--, t--;
|
||||
|
||||
if (s == t) {
|
||||
cout << "1\n";
|
||||
cout << s + 1 << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
if (a[s] == 1 || a[t] == 1) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
if (a[s] == a[t]) {
|
||||
cout << "2\n";
|
||||
cout << s + 1 << ' ' << t + 1 << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
V<bool> used(3e5 + 1);
|
||||
used[a[s]] = true;
|
||||
used[a[t]] = true;
|
||||
|
||||
vvi graph(MAXN + n);
|
||||
auto getgraph = [&](int i) {
|
||||
int now = a[i];
|
||||
while (now > 1) {
|
||||
int prev = di[now];
|
||||
while (di[now] == prev) {
|
||||
now /= di[now];
|
||||
}
|
||||
graph[prev].push_back(i + MAXN);
|
||||
graph[i + MAXN].push_back(prev);
|
||||
}
|
||||
};
|
||||
|
||||
getgraph(s);
|
||||
getgraph(t);
|
||||
|
||||
rep(i, n) {
|
||||
// if ((i != s && a[i] == a[s]) || (i != t && a[i] == a[t])) {
|
||||
// continue;
|
||||
// }
|
||||
// pos[a[i]] = i;
|
||||
|
||||
if (used[a[i]]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
used[a[i]] = true;
|
||||
getgraph(i);
|
||||
}
|
||||
|
||||
// nrep(i, 2, 3e5 + 1) {
|
||||
// if (pos[i] == -1) {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// for (int j = i * 2; j <= 3e5; j += i) {
|
||||
// if (pos[j] == -1) {
|
||||
// continue;
|
||||
// }
|
||||
//
|
||||
// graph[pos[i]].push_back(pos[j]);
|
||||
// graph[pos[j]].push_back(pos[i]);
|
||||
// }
|
||||
// }
|
||||
|
||||
vi dis(MAXN + n, oo);
|
||||
vi parent(MAXN + n, -1);
|
||||
queue<int> q;
|
||||
dis[s + MAXN] = 0;
|
||||
q.push(s + MAXN);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto i = q.front();
|
||||
q.pop();
|
||||
|
||||
for (auto j : graph[i]) {
|
||||
if (dis[j] > dis[i] + 1) {
|
||||
dis[j] = dis[i] + 1;
|
||||
parent[j] = i;
|
||||
q.push(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (dis[t + MAXN] == oo) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
vi ans;
|
||||
int now = t + MAXN;
|
||||
while (parent[now] != -1) {
|
||||
if (now >= MAXN) {
|
||||
ans.push_back(now - MAXN + 1);
|
||||
}
|
||||
now = parent[now];
|
||||
}
|
||||
|
||||
ans.push_back(s + 1);
|
||||
reverse(all(ans));
|
||||
cout << ans.size() << '\n';
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
142
Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp
Normal file
142
Codeforces Round 846 (Div. 2)/D. Bit Guessing Game.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1780/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 cnt;
|
||||
cin >> cnt;
|
||||
|
||||
int c = 0;
|
||||
int t = cnt;
|
||||
|
||||
int ans = 0;
|
||||
rep(i, 30) {
|
||||
if (c == t) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (cnt == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
cout << "- " << (1 << i) << endl;
|
||||
int tmp;
|
||||
cin >> tmp;
|
||||
|
||||
if (tmp >= cnt) {
|
||||
int diff = tmp - cnt + 1;
|
||||
while (diff--) {
|
||||
i++;
|
||||
}
|
||||
ans |= 1 << i;
|
||||
cnt = tmp;
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
|
||||
ans |= 1 << i;
|
||||
cnt = tmp;
|
||||
c++;
|
||||
}
|
||||
|
||||
cout << "! " << ans << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
132
Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp
Normal file
132
Codeforces Round 847 (Div. 3)/E. Vlad and a Pair of Numbers.cpp
Normal file
@@ -0,0 +1,132 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1790/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll x;
|
||||
cin >> x;
|
||||
ll prev = x;
|
||||
|
||||
ll a = x;
|
||||
ll b = 0;
|
||||
|
||||
int log = 31 - __builtin_clz(x);
|
||||
|
||||
for (int i = log; i >= 0; i--) {
|
||||
int bit1 = (a >> i) & 1;
|
||||
|
||||
if (bit1 || (2LL << i) > x) {
|
||||
continue;
|
||||
}
|
||||
|
||||
a |= 1LL << i;
|
||||
b |= 1LL << i;
|
||||
x -= 2LL << i;
|
||||
}
|
||||
|
||||
if (((a + b) >> 1) != prev || (a^b) != prev) {
|
||||
cout << "-1\n";
|
||||
return;
|
||||
}
|
||||
|
||||
cout << a << ' ' << b << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
135
Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp
Normal file
135
Codeforces Round 857 (Div. 1)/A. The Very Beautiful Blanket.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1801/A */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
cout << n * m << '\n';
|
||||
|
||||
vvl ans(n, vl(m));
|
||||
ll cur = 0;
|
||||
for (int i = 0; i < n; i += 2) {
|
||||
int now = 0;
|
||||
for (int j = 0; j < m; j += 2) {
|
||||
ans[i][j] = now + cur;
|
||||
if (j < m - 1) {
|
||||
ans[i][j + 1] = now + cur + 1;
|
||||
}
|
||||
|
||||
if (i < n - 1) {
|
||||
ans[i + 1][j] = now + cur + 2;
|
||||
}
|
||||
|
||||
if (i < n - 1 && j < m - 1) {
|
||||
ans[i + 1][j + 1] = now + cur + 3;
|
||||
}
|
||||
|
||||
now += 4;
|
||||
}
|
||||
|
||||
cur += 1 << 30;
|
||||
}
|
||||
|
||||
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 Round 858 (Div. 2)/C. Sequence Master.cpp
Normal file
144
Codeforces Round 858 (Div. 2)/C. Sequence Master.cpp
Normal file
@@ -0,0 +1,144 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1806/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;
|
||||
n <<= 1;
|
||||
vl p(n);
|
||||
cin >> p;
|
||||
|
||||
if (n == 2) {
|
||||
cout << abs(p[0] - p[1]) << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
ll ans = OO;
|
||||
|
||||
if (n == 4) {
|
||||
ans = 0;
|
||||
rep(i, n) {
|
||||
ans += abs(p[i] - 2);
|
||||
}
|
||||
}
|
||||
|
||||
ll total = 0;
|
||||
rep(i, n) {
|
||||
total += abs(p[i]);
|
||||
}
|
||||
|
||||
rmin(ans, total);
|
||||
if (~n & 2) {
|
||||
ll act = 0;
|
||||
rep(i, n) {
|
||||
act += abs(p[i] + 1);
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
ll tmp = act - abs(p[i] + 1);
|
||||
rmin(ans, tmp + abs(p[i] - (n >> 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();
|
||||
}
|
||||
}
|
||||
170
Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp
Normal file
170
Codeforces Round 859 (Div. 4)/F. Bouncy Ball.cpp
Normal file
@@ -0,0 +1,170 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1807/F */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m, i1, j1, i2, j2;
|
||||
string d;
|
||||
cin >> n >> m >> i1 >> j1 >> i2 >> j2 >> d;
|
||||
i1--, j1--, i2--, j2--;
|
||||
|
||||
int mask = ((d[0] == 'D') << 1) | (d[1] == 'R');
|
||||
|
||||
vvvi dis(n, vvi(m, vi(4, oo)));
|
||||
dis[i1][j1][mask] = 0;
|
||||
|
||||
queue<tuple<int, int, int>> q;
|
||||
q.emplace(i1, j1, mask);
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [i, j, cur] = q.front();
|
||||
q.pop();
|
||||
|
||||
int bounce = 0;
|
||||
|
||||
int prev = dis[i][j][cur];
|
||||
|
||||
if (cur & 2) {
|
||||
i++;
|
||||
} else {
|
||||
i--;
|
||||
}
|
||||
|
||||
if (cur & 1) {
|
||||
j++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
|
||||
if (i < 0) {
|
||||
i = 1;
|
||||
cur ^= 2;
|
||||
bounce |= 1;
|
||||
}
|
||||
|
||||
if (i >= n) {
|
||||
i = n - 2;
|
||||
cur ^= 2;
|
||||
bounce |= 1;
|
||||
}
|
||||
|
||||
if (j < 0) {
|
||||
j = 1;
|
||||
cur ^= 1;
|
||||
bounce |= 1;
|
||||
}
|
||||
|
||||
if (j >= m) {
|
||||
j = m - 2;
|
||||
cur ^= 1;
|
||||
bounce |= 1;
|
||||
}
|
||||
|
||||
if (dis[i][j][cur] > prev + bounce) {
|
||||
dis[i][j][cur] = prev + bounce;
|
||||
q.emplace(i, j, cur);
|
||||
}
|
||||
}
|
||||
|
||||
int ans = *min_element(all(dis[i2][j2]));
|
||||
cout << (ans == oo ? -1 : ans) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
146
Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp
Normal file
146
Codeforces Round 868 (Div. 2)/C. Strongly Composite.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1823/problem/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
const int MAXN = 1e7 + 1;
|
||||
int divs[MAXN];
|
||||
|
||||
void pre()
|
||||
{
|
||||
fill(divs, divs + MAXN, 1);
|
||||
|
||||
nrep(i, 1, MAXN) {
|
||||
if (divs[i] != 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = i; j < MAXN; j += i) {
|
||||
divs[j] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
map<int, int> var;
|
||||
int maximal = 0;
|
||||
rep(i, n) {
|
||||
int no;
|
||||
cin >> no;
|
||||
|
||||
while (divs[no] != 1) {
|
||||
var[divs[no]]++;
|
||||
rmax(maximal, var[divs[no]]);
|
||||
no /= divs[no];
|
||||
}
|
||||
}
|
||||
|
||||
if (var.size() <= 2 && maximal < 2) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int k = 0;
|
||||
int c = 0;
|
||||
repv(i, var) {
|
||||
k += i.second / 2;
|
||||
c += i.second & 1;
|
||||
}
|
||||
|
||||
cout << k + c / 3 << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
245
Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp
Normal file
245
Codeforces Round 869 (Div. 1)/B. Fish Graph.cpp
Normal file
@@ -0,0 +1,245 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1817/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;
|
||||
|
||||
vvi graph(n);
|
||||
while (m--) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
|
||||
function<bool(int, int, int)> find = [&](int i, int t, int p) {
|
||||
vis[i] = true;
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (j == t) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (find(j, t, i)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (graph[i].size() < 4) {
|
||||
continue;
|
||||
}
|
||||
|
||||
fill(all(vis), false);
|
||||
|
||||
if (!find(i, i, i)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
V<pair<int, int>> ans;
|
||||
V<bool> loop(n);
|
||||
|
||||
function<pair<int, int>()> get = [&]() -> pair<int, int> {
|
||||
rep(l1, graph[i].size() - 1) {
|
||||
nrep(l2, l1 + 1, graph[i].size()) {
|
||||
fill(all(vis), false);
|
||||
int one = graph[i][l1];
|
||||
int two = graph[i][l2];
|
||||
function<bool(int, int)> test = [&](int j, int p) {
|
||||
vis[j] = true;
|
||||
repv(k, graph[j]) {
|
||||
if (k == one || k == two) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (k == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (k == i) {
|
||||
loop[j] = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (vis[k]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (test(k, j)) {
|
||||
loop[j] = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
if (test(i, i)) {
|
||||
return {one, two};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {-1, -1};
|
||||
};
|
||||
|
||||
auto [one, two] = get();
|
||||
|
||||
ans.emplace_back(i + 1, one + 1);
|
||||
ans.emplace_back(i + 1, two + 1);
|
||||
|
||||
fill(all(vis), false);
|
||||
int now = -1;
|
||||
int p = -1;
|
||||
int next = i;
|
||||
while (next != -1) {
|
||||
now = next;
|
||||
vis[now] = true;
|
||||
next = -1;
|
||||
repv(j, graph[now]) {
|
||||
if (j == p || !loop[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (j == i) {
|
||||
ans.emplace_back(i + 1, now + 1);
|
||||
break;
|
||||
}
|
||||
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
p = now;
|
||||
next = j;
|
||||
ans.emplace_back(now + 1, j + 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "YES\n";
|
||||
cout << ans.size() << '\n';
|
||||
repv(i, ans) {
|
||||
cout << i.first << ' ' << i.second << '\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();
|
||||
}
|
||||
}
|
||||
131
Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp
Normal file
131
Codeforces Round 870 (Div. 2)/C. Dreaming of Freedom.cpp
Normal file
@@ -0,0 +1,131 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1826/problem/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
const int MAXN = 1e6 + 1;
|
||||
vi divs[MAXN];
|
||||
|
||||
void pre()
|
||||
{
|
||||
V<bool> prime(MAXN, true);
|
||||
prime[1] = false;
|
||||
|
||||
nrep(i, 2, MAXN) {
|
||||
if (!prime[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int j = i; j < MAXN; j += i) {
|
||||
prime[j] = false;
|
||||
divs[j].push_back(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
repv(i, divs[n]) {
|
||||
if (i <= m) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
139
Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp
Normal file
139
Codeforces Round 875 (Div. 1)/A. Copil Copac Draws Trees.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1830/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;
|
||||
|
||||
V<V<pair<int, int>>> graph(n);
|
||||
rep(i, n - 1) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].emplace_back(b, i);
|
||||
graph[b].emplace_back(a, i);
|
||||
}
|
||||
|
||||
function<int(int, int, int)> dfs = [&](int i, int p, int t) {
|
||||
int ans = 0;
|
||||
|
||||
repv(no, graph[i]) {
|
||||
auto [j, c] = no;
|
||||
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (t > c) {
|
||||
rmax(ans, dfs(j, i, c) + 1);
|
||||
} else {
|
||||
rmax(ans, dfs(j, i, c));
|
||||
}
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << dfs(0, 0, -1) + 1 << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
124
Codeforces Round 881 (Div. 3)/B. Long Long.cpp
Normal file
124
Codeforces Round 881 (Div. 3)/B. Long Long.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1843/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;
|
||||
|
||||
ll sum = 0;
|
||||
int ans = 0;
|
||||
int neg = 0;
|
||||
rep(i, n) {
|
||||
if (a[i] > 0) {
|
||||
ans += neg;
|
||||
neg = 0;
|
||||
} else {
|
||||
neg = neg | (a[i] < 0);
|
||||
}
|
||||
sum += abs(a[i]);
|
||||
}
|
||||
|
||||
cout << sum << ' ' << ans + neg << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
203
Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp
Normal file
203
Codeforces Round 881 (Div. 3)/E. Tracking Segments.cpp
Normal file
@@ -0,0 +1,203 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1843/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;
|
||||
|
||||
const int MAX = 1e5+10, UPD = 1e5+10, LOG = 18;
|
||||
const int MAXS = 2*MAX+UPD*LOG;
|
||||
|
||||
namespace perseg {
|
||||
ll seg[MAXS];
|
||||
int rt[UPD], L[MAXS], R[MAXS], cnt, t;
|
||||
int n, *v;
|
||||
|
||||
ll build(int p, int l, int r) {
|
||||
if (l == r) return seg[p] = v[l];
|
||||
L[p] = cnt++, R[p] = cnt++;
|
||||
int m = (l+r)/2;
|
||||
return seg[p] = build(L[p], l, m) + build(R[p], m+1, r);
|
||||
}
|
||||
void build(int n2, int* v2) {
|
||||
n = n2, v = v2;
|
||||
rt[0] = cnt++;
|
||||
build(0, 0, n-1);
|
||||
}
|
||||
ll query(int a, int b, int p, int l, int r) {
|
||||
if (b < l or r < a) return 0;
|
||||
if (a <= l and r <= b) return seg[p];
|
||||
int m = (l+r)/2;
|
||||
return query(a, b, L[p], l, m) + query(a, b, R[p], m+1, r);
|
||||
}
|
||||
ll query(int a, int b, int tt) {
|
||||
return query(a, b, rt[tt], 0, n-1);
|
||||
}
|
||||
ll update(int a, int x, int lp, int p, int l, int r) {
|
||||
if (l == r) return seg[p] = seg[lp]+x;
|
||||
int m = (l+r)/2;
|
||||
if (a <= m)
|
||||
return seg[p] = update(a, x, L[lp], L[p]=cnt++, l, m) + seg[R[p]=R[lp]];
|
||||
return seg[p] = seg[L[p]=L[lp]] + update(a, x, R[lp], R[p]=cnt++, m+1, r);
|
||||
}
|
||||
int update(int a, int x, int tt=t) {
|
||||
update(a, x, rt[tt], rt[++t]=cnt++, 0, n-1);
|
||||
return t;
|
||||
}
|
||||
};
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
perseg::t = 0;
|
||||
perseg::cnt = 0;
|
||||
|
||||
int n, m;
|
||||
cin >> n >> m;
|
||||
|
||||
V<pair<int, int>> p(m);
|
||||
repv(i, p) {
|
||||
cin >> i.first >> i.second;
|
||||
i.first--, i.second--;
|
||||
}
|
||||
|
||||
vi v2(n);
|
||||
perseg::build(n, v2.data());
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
vi que(q);
|
||||
vi pref(n + 1);
|
||||
repv(i, que) {
|
||||
cin >> i;
|
||||
i--;
|
||||
perseg::update(i, 1);
|
||||
pref[i + 1]++;
|
||||
}
|
||||
|
||||
rep(i, n) {
|
||||
pref[i + 1] += pref[i];
|
||||
}
|
||||
|
||||
int ans = oo;
|
||||
repv(seg, p) {
|
||||
int count = pref[seg.second + 1] - pref[seg.first];
|
||||
int lim = ((seg.second - seg.first + 1) >> 1) + 1;
|
||||
if (count < lim) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int low = 1;
|
||||
int high = q;
|
||||
int act = q;
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >> 1;
|
||||
|
||||
int cur = perseg::query(seg.first, seg.second, mid);
|
||||
if (cur >= lim) {
|
||||
act = mid;
|
||||
high = mid - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
low = mid + 1;
|
||||
}
|
||||
|
||||
rmin(ans, act);
|
||||
}
|
||||
|
||||
cout << (ans == oo ? -1 : ans) << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
156
Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp
Normal file
156
Codeforces Round 882 (Div. 2)/C. Vampiric Powers, anyone?.cpp
Normal file
@@ -0,0 +1,156 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1847/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;
|
||||
|
||||
vvi trie(1, vi(2));
|
||||
|
||||
auto add = [&](int i) {
|
||||
int now = 0;
|
||||
|
||||
for (int j = 7; j >= 0; j--) {
|
||||
int bit = (i >> j) & 1;
|
||||
|
||||
if (trie[now][bit] == 0) {
|
||||
trie[now][bit] = trie.size();
|
||||
trie.emplace_back(2);
|
||||
}
|
||||
|
||||
now = trie[now][bit];
|
||||
}
|
||||
};
|
||||
|
||||
auto get = [&](int i) {
|
||||
int ans = 0;
|
||||
int now = 0;
|
||||
|
||||
for (int j = 7; j >=0; j--) {
|
||||
int bit = (i >> j) & 1;
|
||||
|
||||
if (trie[now][bit^1] == 0) {
|
||||
now = trie[now][bit];
|
||||
continue;
|
||||
}
|
||||
|
||||
ans |= 1 << j;
|
||||
now = trie[now][bit^1];
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
int ans = 0;
|
||||
int xo = 0;
|
||||
|
||||
rep(i, n) {
|
||||
add(xo);
|
||||
xo ^= a[i];
|
||||
rmax(ans, get(xo));
|
||||
}
|
||||
|
||||
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/1846/E2 */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n'; return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
const int sz = 1e6 + 1;
|
||||
vl p[sz];
|
||||
|
||||
void pre()
|
||||
{
|
||||
for (__int128 i = 2; i < sz; i++) {
|
||||
__int128 cur = i * i;
|
||||
__int128 sum = i + i * i;
|
||||
|
||||
while (sum <= (ll)1e18) {
|
||||
p[i].push_back(sum);
|
||||
cur *= i;
|
||||
sum += cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
cin >> n;
|
||||
|
||||
if (n == 1) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
|
||||
n--;
|
||||
|
||||
__int128 at = sqrtl((n * 4 + 1)) / 2.0L - 0.5L;
|
||||
|
||||
if (at > 1 && at + at * at == n) {
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
rep(i, 63) {
|
||||
int low = 2;
|
||||
int high = 1e6;
|
||||
int ans = 2;
|
||||
while (low <= high) {
|
||||
int mid = (low + high) >> 1;
|
||||
|
||||
if (p[mid].size() > i && p[mid][i] <= n) {
|
||||
ans = mid;
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
if (p[ans].size() > i && p[ans][i] == n) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
147
Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp
Normal file
147
Codeforces Round 885 (Div. 2)/C. Vika and Price Tags.cpp
Normal file
@@ -0,0 +1,147 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1848/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);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
|
||||
V<array<bool, 3>> mo(n);
|
||||
|
||||
rep(i, n) {
|
||||
if (a[i] == 0 && b[i] == 0) {
|
||||
mo[i][0] = true;
|
||||
mo[i][1] = true;
|
||||
mo[i][2] = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (a[i] == 0) {
|
||||
mo[i][0] = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (b[i] == 0) {
|
||||
mo[i][2] = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
ll d = gcd(a[i], b[i]);
|
||||
int x = (a[i] / d) & 1;
|
||||
int y = (b[i] / d) & 1;
|
||||
|
||||
mo[i][x + !y] = true;
|
||||
}
|
||||
|
||||
array<bool, 3> ans = {true, true, true};
|
||||
rep(i, n) {
|
||||
ans[0] = ans[0] && mo[i][0];
|
||||
ans[1] = ans[1] && mo[i][1];
|
||||
ans[2] = ans[2] && mo[i][2];
|
||||
}
|
||||
|
||||
cout << (ans[0] || ans[1] || ans[2] ? "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();
|
||||
}
|
||||
}
|
||||
183
Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp
Normal file
183
Codeforces Round 894 (Div. 3)/G. The Great Equalizer.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1862/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;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
map<ll, int> act;
|
||||
multiset<ll> diff;
|
||||
diff.insert(0);
|
||||
|
||||
repv(i, a) {
|
||||
act[i]++;
|
||||
}
|
||||
|
||||
{
|
||||
auto itr = next(act.begin());
|
||||
auto prev = act.begin();
|
||||
while (itr != act.end()) {
|
||||
diff.insert(itr->first - prev->first);
|
||||
itr++;
|
||||
prev++;
|
||||
}
|
||||
}
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
int i;
|
||||
ll x;
|
||||
cin >> i >> x;
|
||||
i--;
|
||||
|
||||
ll p = a[i];
|
||||
a[i] = x;
|
||||
|
||||
auto itr = act.find(p);
|
||||
itr->second--;
|
||||
|
||||
if (itr->second == 0) {
|
||||
auto ne = next(itr);
|
||||
|
||||
if (itr != act.begin() && ne != act.end()) {
|
||||
diff.insert(ne->first - prev(itr)->first);
|
||||
}
|
||||
|
||||
if (itr != act.begin()) {
|
||||
diff.erase(diff.find(itr->first - prev(itr)->first));
|
||||
}
|
||||
|
||||
if (ne != act.end()) {
|
||||
diff.erase(diff.find(ne->first - itr->first));
|
||||
}
|
||||
|
||||
act.erase(itr);
|
||||
}
|
||||
|
||||
act[x]++;
|
||||
|
||||
itr = act.find(x);
|
||||
|
||||
if (itr->second == 1) {
|
||||
auto ne = next(itr);
|
||||
|
||||
if (ne != act.end() && itr != act.begin()) {
|
||||
diff.erase(diff.find(ne->first - prev(itr)->first));
|
||||
}
|
||||
|
||||
if (itr != act.begin()) {
|
||||
diff.insert(itr->first - prev(itr)->first);
|
||||
}
|
||||
|
||||
if (ne != act.end()) {
|
||||
diff.insert(ne->first - itr->first);
|
||||
}
|
||||
}
|
||||
|
||||
cout << prev(act.end())->first + *prev(diff.end()) << " \n"[q == 0];
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
173
Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp
Normal file
173
Codeforces Round 897 (Div. 2)/D. Cyclic Operations.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1867/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;
|
||||
|
||||
vi a(n);
|
||||
repv(i, a) {
|
||||
cin >> i;
|
||||
i--;
|
||||
}
|
||||
|
||||
if (k == 1) {
|
||||
rep(i, n) {
|
||||
if (a[i] != i) {
|
||||
cout << "NO\n";
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "YES\n";
|
||||
return;
|
||||
}
|
||||
|
||||
V<bool> vis(n);
|
||||
V<bool> frame(n);
|
||||
|
||||
auto findloop = [&](int i) {
|
||||
int hare = a[a[i]];
|
||||
int tortoise = a[i];
|
||||
|
||||
while (tortoise != hare && !vis[tortoise]) {
|
||||
hare = a[a[hare]];
|
||||
tortoise = a[tortoise];
|
||||
}
|
||||
|
||||
if (vis[tortoise]) {
|
||||
while (!vis[i]) {
|
||||
vis[i] = true;
|
||||
i = a[i];
|
||||
}
|
||||
return k;
|
||||
}
|
||||
|
||||
int count = 1;
|
||||
hare = a[hare];
|
||||
while (hare != tortoise) {
|
||||
count++;
|
||||
hare = a[hare];
|
||||
}
|
||||
|
||||
while (!vis[i]) {
|
||||
vis[i] = true;
|
||||
i = a[i];
|
||||
}
|
||||
|
||||
return count;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (findloop(i) != k) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
172
Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp
Normal file
172
Codeforces Round 899 (Div. 2)/D. Tree XOR.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1882/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 a(n);
|
||||
cin >> a;
|
||||
|
||||
vvi graph(n);
|
||||
rep(i, n - 1) {
|
||||
int a, b;
|
||||
cin >> a >> b;
|
||||
a--, b--;
|
||||
|
||||
graph[a].push_back(b);
|
||||
graph[b].push_back(a);
|
||||
}
|
||||
|
||||
V<array<ll, 20>> dp(n);
|
||||
vl count(n, 1);
|
||||
function<void(int, int)> calc = [&](int i, int p) {
|
||||
repv(j, graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
calc(j, i);
|
||||
count[i] += count[j];
|
||||
|
||||
rep(k, 20) {
|
||||
dp[i][k] += dp[j][k];
|
||||
if (((a[i] >> k) & 1) != ((a[j] >> k) & 1)) {
|
||||
dp[i][k] += (1LL << k) * count[j];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
calc(0, 0);
|
||||
|
||||
vl ans(n);
|
||||
|
||||
function<void(int, int)> reroot = [&](int i, int p) {
|
||||
ans[i] = accumulate(all(dp[i]), 0LL);
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
count[i] = n - count[j];
|
||||
|
||||
rep(k, 20) {
|
||||
if (((a[i] >> k) & 1) != ((a[j] >> k) & 1)) {
|
||||
dp[j][k] = dp[i][k] + count[i] * (1LL << k) - count[j] * (1LL << k);
|
||||
continue;
|
||||
}
|
||||
|
||||
dp[j][k] = dp[i][k];
|
||||
}
|
||||
|
||||
reroot(j, i);
|
||||
}
|
||||
};
|
||||
|
||||
reroot(0, 0);
|
||||
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
162
Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp
Normal file
162
Codeforces Round 900 (Div. 3)/D. Reverse Madness.cpp
Normal file
@@ -0,0 +1,162 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1878/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;
|
||||
cin >> s;
|
||||
|
||||
vi l(k);
|
||||
vi r(k);
|
||||
cin >> l >> r;
|
||||
|
||||
vi other(n);
|
||||
V<bool> start(n);
|
||||
int cur = 0;
|
||||
rep(i, n) {
|
||||
while (r[cur] < i + 1) {
|
||||
cur++;
|
||||
}
|
||||
|
||||
start[i] = i == l[cur] - 1;
|
||||
other[i] = r[cur] - i + l[cur] - 2;
|
||||
other[r[cur] - i + l[cur] - 2] = i;
|
||||
}
|
||||
|
||||
vi act(n + 1);
|
||||
|
||||
int q;
|
||||
cin >> q;
|
||||
while (q--) {
|
||||
int x;
|
||||
cin >> x;
|
||||
|
||||
int pos = upper_bound(all(l), x) - l.begin() - 1;
|
||||
int a = min(x, l[pos] + r[pos] - x) - 1;
|
||||
int b = max(x, l[pos] + r[pos] - x) - 1;
|
||||
|
||||
if (a == b) {
|
||||
continue;
|
||||
}
|
||||
|
||||
act[a] ^= 1;
|
||||
if (b != r[pos] - 1) {
|
||||
act[b + 1] ^= 1;
|
||||
}
|
||||
}
|
||||
|
||||
cur = 0;
|
||||
rep(i, n) {
|
||||
if (start[i]) {
|
||||
cur = 0;
|
||||
}
|
||||
cur ^= act[i];
|
||||
if (cur) {
|
||||
cout << s[other[i]];
|
||||
} else {
|
||||
cout << s[i];
|
||||
}
|
||||
}
|
||||
cout << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
139
Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp
Normal file
139
Codeforces Round 908 (Div. 1)/A. Anonymous Informant.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1893/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;
|
||||
vi b(n);
|
||||
cin >> b;
|
||||
|
||||
vi pos(n, -1);
|
||||
set<int, greater<>> t;
|
||||
rep(i, n) {
|
||||
t.insert(b[i]);
|
||||
if (b[i] > n) {
|
||||
continue;
|
||||
}
|
||||
b[i]--;
|
||||
// pos[i - b[i] + n * ((i - b[i]) < 0)].push_back(b[i] + 1);
|
||||
int p = b[i] - i + n * ((b[i] - i) < 0);
|
||||
b[i]++;
|
||||
pos[p - b[i] + n * (p - b[i] < 0)] = b[i];
|
||||
}
|
||||
|
||||
if ((t.size() == 1 && *t.begin() <= n) || *t.begin() == n) {
|
||||
cout << "Yes\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int cur = 0;
|
||||
int c = 0;
|
||||
int lim = min(k, n);
|
||||
while (pos[cur] != -1 && c < lim) {
|
||||
cur += pos[cur];
|
||||
cur %= n;
|
||||
c++;
|
||||
}
|
||||
|
||||
cout << (c < lim ? "No\n" : "Yes\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
106
Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp
Normal file
106
Codeforces Round 908 (Div. 1)/C. Freedom of Choice.cpp
Normal file
@@ -0,0 +1,106 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1893/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
126
Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp
Normal file
126
Codeforces Round 909 (Div. 3)/F. Alex's whims.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1899/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;
|
||||
|
||||
rep(i, n - 1) {
|
||||
cout << i + 1 << ' ' << i + 2 << '\n';
|
||||
}
|
||||
|
||||
int prev = n - 1;
|
||||
|
||||
while (q--) {
|
||||
int d;
|
||||
cin >> d;
|
||||
|
||||
if (d == prev) {
|
||||
cout << "-1 -1 -1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
cout << n << ' ' << prev << ' ' << d << '\n';
|
||||
prev = d;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
123
Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp
Normal file
123
Codeforces Round 910 (Div. 2)/B. Milena and Admirer.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1898/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;
|
||||
|
||||
if (n == 1) {
|
||||
cout << "0\n";
|
||||
return;
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
for (int i = n - 2; i >= 0; i--) {
|
||||
ll k = (a[i] + a[i + 1] - 1) / a[i + 1];
|
||||
ans += k - 1;
|
||||
a[i] = a[i] / k;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
127
Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp
Normal file
127
Codeforces Round 910 (Div. 2)/D. Absolute Beauty.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1898/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);
|
||||
vl b(n);
|
||||
cin >> a >> b;
|
||||
rep(i, n) {
|
||||
if (a[i] > b[i]) {
|
||||
swap(a[i], b[i]);
|
||||
}
|
||||
}
|
||||
|
||||
ll total = 0;
|
||||
ll mi = OO;
|
||||
ll ma = 0;
|
||||
rep(i, n) {
|
||||
total += b[i] - a[i];
|
||||
rmax(ma, a[i]);
|
||||
rmin(mi, b[i]);
|
||||
}
|
||||
|
||||
cout << total + max(ma - mi, 0LL) << '\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 916 (Div. 3)/F. Programming Competition.cpp
Normal file
150
Codeforces Round 916 (Div. 3)/F. Programming Competition.cpp
Normal file
@@ -0,0 +1,150 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1914/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;
|
||||
|
||||
vvi graph(n);
|
||||
rep(i, n - 1) {
|
||||
int now;
|
||||
cin >> now;
|
||||
graph[now - 1].push_back(i + 1);
|
||||
}
|
||||
|
||||
vi size(n + 1);
|
||||
|
||||
function<int(int)> getsize = [&](int i) {
|
||||
size[i] = 1;
|
||||
repv(j, graph[i]) {
|
||||
size[i] += getsize(j);
|
||||
}
|
||||
|
||||
return size[i];
|
||||
};
|
||||
|
||||
getsize(0);
|
||||
|
||||
function<int(int, int)> getans = [&](int i, int rem) {
|
||||
if (size[i] == 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int choice = n;
|
||||
int total = size[i] - 1;
|
||||
|
||||
repv(j, graph[i]) {
|
||||
if (size[j] > size[choice]) {
|
||||
choice = j;
|
||||
}
|
||||
}
|
||||
|
||||
if (size[choice] * 2 - rem <= total) {
|
||||
return (total - rem) >> 1;
|
||||
}
|
||||
|
||||
return total - size[choice] + getans(choice, max(0, rem + total - size[choice] - 1));
|
||||
};
|
||||
|
||||
cout << getans(0, 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 919 (Div. 2)/C. Partitioning the Array.cpp
Normal file
168
Codeforces Round 919 (Div. 2)/C. Partitioning the Array.cpp
Normal file
@@ -0,0 +1,168 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1920/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
const int MAXN = 2e5 + 1;
|
||||
vvi divs(MAXN);
|
||||
|
||||
void pre()
|
||||
{
|
||||
nrep(i, 2, MAXN) {
|
||||
for (int j = i; j < MAXN; j += i) {
|
||||
divs[j].push_back(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n;
|
||||
cin >> n;
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
vi mods(n);
|
||||
|
||||
int ans = 1;
|
||||
repv(i, divs[n]) {
|
||||
int size = n / i;
|
||||
|
||||
rep(j, size) {
|
||||
int cur = abs(a[j] - a[j + size]);
|
||||
if (cur == 0) {
|
||||
cur = -1;
|
||||
}
|
||||
|
||||
for (int k = j + size; k + size < n; k += size) {
|
||||
int now = abs(a[k] - a[k + size]);
|
||||
if (now == 0) {
|
||||
now = -1;
|
||||
}
|
||||
|
||||
if (cur == -1 && now != -1) {
|
||||
cur = now;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (cur != -1 && now != -1) {
|
||||
cur = gcd(cur, now);
|
||||
}
|
||||
}
|
||||
|
||||
mods[j] = cur;
|
||||
}
|
||||
|
||||
int prev = -1;
|
||||
rep(j, size) {
|
||||
if (prev == -1) {
|
||||
prev = mods[j];
|
||||
continue;
|
||||
}
|
||||
|
||||
if (mods[j] == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
prev = gcd(prev, mods[j]);
|
||||
}
|
||||
|
||||
if (prev != 1) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
169
Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp
Normal file
169
Codeforces Round 920 (Div. 3)/E. Eat the Chip.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1921/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll h, w, xa, ya, xb, yb;
|
||||
cin >> h >> w >> xa >> ya >> xb >> yb;
|
||||
|
||||
if (xb <= xa) {
|
||||
cout << "Draw\n";
|
||||
return;
|
||||
}
|
||||
|
||||
ll diff = xb - xa;
|
||||
|
||||
if (diff & 1) {
|
||||
int d;
|
||||
if (yb <= ya) {
|
||||
d = -1;
|
||||
} else {
|
||||
d = 1;
|
||||
}
|
||||
|
||||
while (xa < xb) {
|
||||
if (xb == xa + 1 && abs(ya - yb) <= 1) {
|
||||
cout << "Alice\n";
|
||||
return;
|
||||
}
|
||||
|
||||
xa++;
|
||||
ya += d;
|
||||
rmax(ya, 1LL);
|
||||
rmin(ya, w);
|
||||
|
||||
xb--;
|
||||
yb += d;
|
||||
rmax(yb, 1LL);
|
||||
rmin(yb, w);
|
||||
}
|
||||
|
||||
cout << "Draw\n";
|
||||
return;
|
||||
}
|
||||
|
||||
int d;
|
||||
if (yb >= ya) {
|
||||
d = -1;
|
||||
} else {
|
||||
d = 1;
|
||||
}
|
||||
|
||||
while (xa < xb) {
|
||||
xa++;
|
||||
ya += d;
|
||||
rmax(ya, 1LL);
|
||||
rmin(ya, w);
|
||||
|
||||
if (xb == xa + 1 && abs(ya - yb) <= 1) {
|
||||
cout << "Bob\n";
|
||||
return;
|
||||
}
|
||||
|
||||
xb--;
|
||||
yb += d;
|
||||
rmax(yb, 1LL);
|
||||
rmin(yb, w);
|
||||
}
|
||||
|
||||
cout << "Draw\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 924 (Div. 2)/C. Physical Education Lesson.cpp
Normal file
142
Codeforces Round 924 (Div. 2)/C. Physical Education Lesson.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1928/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n, x;
|
||||
cin >> n >> x;
|
||||
|
||||
set<ll> ans;
|
||||
|
||||
ll now = n - x;
|
||||
if (~now & 1) {
|
||||
now >>= 1;
|
||||
|
||||
for (ll i = 1; i * i <= now; i++) {
|
||||
if (now % i == 0) {
|
||||
ans.insert(i + 1);
|
||||
ans.insert(now / i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
now = n + x - 2;
|
||||
if (~now & 1) {
|
||||
now >>= 1;
|
||||
|
||||
for (ll i = 1; i * i <= now; i++) {
|
||||
if (now % i == 0) {
|
||||
ans.insert(i + 1);
|
||||
ans.insert(now / i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ll act = 0;
|
||||
repv(i, ans) {
|
||||
if (i >= x) {
|
||||
act++;
|
||||
}
|
||||
}
|
||||
|
||||
cout << act << '\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 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp
Normal file
126
Codeforces Round 928 (Div. 4)/E. Vlad and an Odd Ordering.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1926/E */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
ll c = (n + 1) >> 1;
|
||||
if (c >= k) {
|
||||
cout << (k << 1) - 1 << '\n';
|
||||
return;
|
||||
}
|
||||
k -= c + 1;
|
||||
|
||||
rep(i, 32) {
|
||||
ll now = 2LL << i;
|
||||
|
||||
ll tmp = (n - now) / (now << 1);
|
||||
if (tmp >= k) {
|
||||
cout << now + (now << 1) * k << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
k -= tmp + 1;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
142
Codeforces Round 930 (Div. 2)/B. Binary Path.cpp
Normal file
142
Codeforces Round 930 (Div. 2)/B. Binary Path.cpp
Normal file
@@ -0,0 +1,142 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1937/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;
|
||||
V<string> a(2);
|
||||
cin >> a;
|
||||
|
||||
string best;
|
||||
|
||||
int cur = 0;
|
||||
while (cur < n - 1) {
|
||||
best.push_back(a[0][cur]);
|
||||
|
||||
if (a[0][cur + 1] > a[1][cur]) {
|
||||
break;
|
||||
}
|
||||
cur++;
|
||||
}
|
||||
|
||||
if (cur == n - 1) {
|
||||
best.push_back(a[0][n - 1]);
|
||||
}
|
||||
|
||||
while (cur < n) {
|
||||
best.push_back(a[1][cur]);
|
||||
cur++;
|
||||
}
|
||||
|
||||
vi valid(n);
|
||||
for (int i = n - 1; i >= 0 && a[1][i] == best[i + 1]; i--) {
|
||||
valid[i] = 1;
|
||||
}
|
||||
|
||||
int ans = 0;
|
||||
for (int i = 0; i < n && a[0][i] == best[i]; i++) {
|
||||
ans += valid[i];
|
||||
}
|
||||
|
||||
cout << best << '\n' << 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,130 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1957/C */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
const ll mod = 1e9 + 7;
|
||||
const int MAXN = 3e5 + 1;
|
||||
ll dp[MAXN];
|
||||
|
||||
void pre()
|
||||
{
|
||||
dp[0] = 1;
|
||||
dp[1] = 1;
|
||||
nrep(i, 2, MAXN) {
|
||||
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2] * 2;
|
||||
dp[i] %= mod;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
int n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
int total = n;
|
||||
|
||||
while (k--) {
|
||||
int r, c;
|
||||
cin >> r >> c;
|
||||
|
||||
total--;
|
||||
if (r != c) {
|
||||
total--;
|
||||
}
|
||||
}
|
||||
|
||||
cout << dp[total] << '\n';
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
124
Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp
Normal file
124
Codeforces Round 941 (Div. 1)/A. Everything Nim.cpp
Normal file
@@ -0,0 +1,124 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1965/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 = 0;
|
||||
sortv(a);
|
||||
a.erase(unique(all(a)), a.end());
|
||||
int c = 0;
|
||||
rep(i, a.size()) {
|
||||
c += a[i] == i + 1;
|
||||
}
|
||||
|
||||
if (c == a.size()) {
|
||||
cout << ((a.size() & 1) ? "Alice\n" : "Bob\n");
|
||||
return;
|
||||
}
|
||||
|
||||
cout << ((c & 1) ? "Bob\n" : "Alice\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 941 (Div. 1)/B. Missing Subsequence Sum.cpp
Normal file
126
Codeforces Round 941 (Div. 1)/B. Missing Subsequence Sum.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/* Problem URL: https://codeforces.com/contest/1965/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;
|
||||
|
||||
vl ans;
|
||||
ll now = 1;
|
||||
while ((now << 1) <= k) {
|
||||
ans.push_back(now);
|
||||
now <<= 1;
|
||||
}
|
||||
|
||||
ans.push_back(k + 1);
|
||||
ans.push_back(k - now);
|
||||
ans.push_back(k + now + 1);
|
||||
now <<= 1;
|
||||
while (now <= 1e6) {
|
||||
ans.push_back(now);
|
||||
now <<= 1;
|
||||
}
|
||||
|
||||
cout << ans.size() << '\n';
|
||||
cout << ans;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
pre();
|
||||
|
||||
int t;
|
||||
(TEST && cin >> t) || (t = 1);
|
||||
while (t--) {
|
||||
solve();
|
||||
}
|
||||
}
|
||||
146
Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp
Normal file
146
Codeforces Round 942 (Div. 1)/A. Permutation Counting.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
/* Problem URL: https://codeforces.com/problemset/problem/1967/A */
|
||||
|
||||
#include <bits/stdc++.h>
|
||||
#include <ext/pb_ds/assoc_container.hpp>
|
||||
#include <ext/pb_ds/tree_policy.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace __gnu_pbds;
|
||||
|
||||
template <class T, class comp = less<>>
|
||||
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||
|
||||
#define V vector
|
||||
|
||||
#define rmin(a, b) a = min(a, b)
|
||||
#define rmax(a, b) a = max(a, b)
|
||||
|
||||
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||
|
||||
#define repv(i, v) for (auto &i : (v))
|
||||
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||
#define sortv(v) sort(v.begin(), v.end())
|
||||
#define all(v) (v).begin(), (v).end()
|
||||
|
||||
using vi = vector<int>;
|
||||
using vvi = vector<vi>;
|
||||
using vvvi = vector<vvi>;
|
||||
using vvvvi = vector<vvvi>;
|
||||
|
||||
using ll = long long;
|
||||
|
||||
using vl = vector<ll>;
|
||||
using vvl = vector<vl>;
|
||||
using vvvl = vector<vvl>;
|
||||
using vvvvl = vector<vvvl>;
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||
os << vec[0];
|
||||
for (size_t i = 1; i < vec.size(); i++) {
|
||||
os << ' ' << vec[i];
|
||||
}
|
||||
os << '\n';
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
is >> i;
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||
for (auto &i : vec) {
|
||||
os << i[0];
|
||||
for (size_t j = 1; j < i.size(); j++) {
|
||||
os << ' ' << i[j];
|
||||
}
|
||||
os << '\n';
|
||||
}
|
||||
return os;
|
||||
}
|
||||
|
||||
template<class v>
|
||||
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||
for (auto &i : vec) {
|
||||
for (auto &j : i) {
|
||||
is >> j;
|
||||
}
|
||||
}
|
||||
return is;
|
||||
}
|
||||
|
||||
const int oo = INT32_MAX >> 1;
|
||||
const ll OO = INT64_MAX >> 1;
|
||||
|
||||
|
||||
void pre()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
#define TEST 1
|
||||
|
||||
void solve()
|
||||
{
|
||||
ll n;
|
||||
ll k;
|
||||
cin >> n >> k;
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
ll low = 1;
|
||||
ll high = 1e14;
|
||||
ll ans = 0;
|
||||
while (low <= high) {
|
||||
ll mid = (low + high) >> 1;
|
||||
ll act = mid - 1;
|
||||
|
||||
ll cost = 0;
|
||||
ll valid = 0;
|
||||
ll overflow = 0;
|
||||
rep(i, n) {
|
||||
if (cost > k) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (a[i] >= mid) {
|
||||
overflow++;
|
||||
continue;
|
||||
}
|
||||
|
||||
cost += act - a[i];
|
||||
valid++;
|
||||
}
|
||||
|
||||
if (cost <= k) {
|
||||
rmax(ans, act * n + min(k - cost, valid) + overflow - n + 1);
|
||||
low = mid + 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
high = mid - 1;
|
||||
}
|
||||
|
||||
cout << ans << '\n';
|
||||
// cout << ans * n - n + 1 << '\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