Compare commits
5 Commits
f34355b975
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 97658bef82 | |||
| 3ac1071724 | |||
| 16b9334310 | |||
| ac1107bd79 | |||
| 25fe530b27 |
@@ -0,0 +1,182 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1912/K */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
const ll mod = 998244353;
|
||||||
|
constexpr int MAXN = 2e5 + 10;
|
||||||
|
ll fact[MAXN];
|
||||||
|
ll inv[MAXN];
|
||||||
|
ll invf[MAXN];
|
||||||
|
|
||||||
|
ll comb(ll n, ll k) {
|
||||||
|
return fact[n] * invf[k] % mod * invf[n - k] % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
fact[0] = 1;
|
||||||
|
nrep(i, 1, MAXN) {
|
||||||
|
fact[i] = fact[i - 1] * i % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
inv[1] = 1;
|
||||||
|
nrep(i, 2, MAXN) {
|
||||||
|
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
invf[0] = 1;
|
||||||
|
nrep(i, 1, MAXN) {
|
||||||
|
invf[i] = invf[i - 1] * inv[i] % mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 0
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vl a(n);
|
||||||
|
ll c = 0;
|
||||||
|
repv(i, a) {
|
||||||
|
cin >> i;
|
||||||
|
c += ~i & 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
nrep(i, 3, c + 1) {
|
||||||
|
(ans += comb(c, i)) %= mod;
|
||||||
|
}
|
||||||
|
int seq[] = {1, 0, 1};
|
||||||
|
|
||||||
|
vvvl memo(n, vvl(3, vl(2, -1)));
|
||||||
|
auto init = [&]() {
|
||||||
|
rep(i, n) {
|
||||||
|
rep(j, 3) {
|
||||||
|
rep(k, 2) {
|
||||||
|
memo[i][j][k] = -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function<ll(int, int, int)> dp = [&](int i, int c, int val) {
|
||||||
|
if (i >= n) {
|
||||||
|
return 0LL;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll &ans = memo[i][c][val];
|
||||||
|
if (ans != -1) {
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
ans = 0;
|
||||||
|
if (seq[c] == (a[i] & 1)) {
|
||||||
|
(ans += dp(i + 1, (c + 1) % 3, val || (c == 2)) + (val || (c == 2))) %= mod;
|
||||||
|
}
|
||||||
|
(ans += dp(i + 1, c, val)) %= mod;
|
||||||
|
return ans;
|
||||||
|
};
|
||||||
|
|
||||||
|
(ans += dp(0, 0, 0)) %= mod;
|
||||||
|
seq[0] = 0;
|
||||||
|
seq[1] = 1;
|
||||||
|
init();
|
||||||
|
(ans += dp(0, 0, 0)) %= mod;
|
||||||
|
seq[0] = 1;
|
||||||
|
seq[2] = 0;
|
||||||
|
init();
|
||||||
|
(ans += dp(0, 0, 0)) %= mod;
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/755/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 0
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
set<int> s;
|
||||||
|
int ans = 0;
|
||||||
|
rep(i, n) {
|
||||||
|
int a;
|
||||||
|
cin >> a;
|
||||||
|
if (a == i + 1) {
|
||||||
|
ans++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
s.insert(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans + (s.size() + 1) / 2 << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
153
Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp
Normal file
153
Codeforces Alpha Round 20 (Codeforces format)/C. Dijkstra?.cpp
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/20/C */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 0
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
V<V<pair<int, ll>>> graph(n);
|
||||||
|
|
||||||
|
while (m--) {
|
||||||
|
int a, b, c;
|
||||||
|
cin >> a >> b >> c;
|
||||||
|
a--, b--;
|
||||||
|
graph[a].emplace_back(b, c);
|
||||||
|
graph[b].emplace_back(a, c);
|
||||||
|
}
|
||||||
|
|
||||||
|
vi p(n, -1);
|
||||||
|
vl dis(n, OO);
|
||||||
|
priority_queue<pair<ll, int>, V<pair<ll, int>>, greater<>> pq;
|
||||||
|
dis[0] = 0;
|
||||||
|
pq.emplace(0, 0);
|
||||||
|
while (!pq.empty()) {
|
||||||
|
auto [c, i] = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
|
||||||
|
if (c > dis[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto [j, w] : graph[i]) {
|
||||||
|
if (dis[j] > c + w) {
|
||||||
|
dis[j] = c + w;
|
||||||
|
p[j] = i;
|
||||||
|
pq.emplace(dis[j], j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dis[n - 1] == OO) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi ans;
|
||||||
|
int now = n - 1;
|
||||||
|
while (now != -1) {
|
||||||
|
ans.push_back(now + 1);
|
||||||
|
now = p[now];
|
||||||
|
}
|
||||||
|
reverse(all(ans));
|
||||||
|
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,121 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2179/problem/D */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
ll cur = (1 << n) - 1;
|
||||||
|
int lim = __builtin_popcount(cur);
|
||||||
|
int mod = 0;
|
||||||
|
rep(i, 1 << n) {
|
||||||
|
cout << cur + (mod << (__builtin_popcount(cur) + 1)) << ' ';
|
||||||
|
if (cur > 0 && __builtin_popcount(cur) + __builtin_popcount(mod) >= lim - 1) {
|
||||||
|
mod = 0;
|
||||||
|
cur ^= 1 << (31 - __builtin_clz(cur));
|
||||||
|
} else {
|
||||||
|
mod++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << '\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 1074 (Div. 4)/E. The Robotic Rush.cpp
Normal file
160
Codeforces Round 1074 (Div. 4)/E. The Robotic Rush.cpp
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2185/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, q;
|
||||||
|
cin >> n >> m >> q;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
vl b(m);
|
||||||
|
cin >> a >> b;
|
||||||
|
sortv(b);
|
||||||
|
|
||||||
|
string c;
|
||||||
|
cin >> c;
|
||||||
|
|
||||||
|
V<pair<int, int>> mov(q + 1);
|
||||||
|
int now = 0;
|
||||||
|
nrep(i, 1, q + 1) {
|
||||||
|
char cur = c[i - 1];
|
||||||
|
mov[i] = mov[i - 1];
|
||||||
|
now += (cur == 'R') - (cur == 'L');
|
||||||
|
rmin(mov[i].first, now);
|
||||||
|
rmax(mov[i].second, now);
|
||||||
|
}
|
||||||
|
|
||||||
|
vi ans(q + 1);
|
||||||
|
ans[0] = n;
|
||||||
|
rep(i, n) {
|
||||||
|
int low = 0;
|
||||||
|
int high = q + 1;
|
||||||
|
while (low < high) {
|
||||||
|
int mid = (low + high) >> 1;
|
||||||
|
|
||||||
|
ll posl = a[i] + mov[mid].first;
|
||||||
|
ll posr = a[i] + mov[mid].second;
|
||||||
|
|
||||||
|
auto itr = lower_bound(all(b), posl);
|
||||||
|
auto itr2 = upper_bound(all(b), posr);
|
||||||
|
|
||||||
|
if (((itr == b.end()) || (*itr < posl || *itr > a[i])) && ((itr2 == b.begin()) || (*prev(itr2) > posr || *prev(itr2) < a[i]))) {
|
||||||
|
low = mid + 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
high = mid;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (low == q + 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ans[low]--;
|
||||||
|
}
|
||||||
|
|
||||||
|
nrep(i, 1, q + 1) {
|
||||||
|
ans[i] += ans[i - 1];
|
||||||
|
cout << ans[i] << ' ';
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
119
Codeforces Round 1077 (Div. 2)/A. Divisible Permutation.cpp
Normal file
119
Codeforces Round 1077 (Div. 2)/A. Divisible Permutation.cpp
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2188/problem/A */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
vi p(n);
|
||||||
|
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
if ((n - i) & 1) {
|
||||||
|
p[i] = (n - i + 1) >> 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
p[i] = n - ((n - i - 1) >> 1);
|
||||||
|
}
|
||||||
|
cout << p;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
134
Codeforces Round 1077 (Div. 2)/B. Seats.cpp
Normal file
134
Codeforces Round 1077 (Div. 2)/B. Seats.cpp
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2188/B */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
string a;
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
int i = 0;
|
||||||
|
while (i < n && a[i] == '0') {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == n) {
|
||||||
|
cout << (n + 2) / 3 << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ans = (i + 1) / 3;
|
||||||
|
int c = 0;
|
||||||
|
while (i < n) {
|
||||||
|
if (a[i] == '0') {
|
||||||
|
c++;
|
||||||
|
} else {
|
||||||
|
ans += c / 3;
|
||||||
|
c = 0;
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ans += (c + 1) / 3;
|
||||||
|
cout << ans + count(all(a), '1') << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
108
Codeforces Round 1078 (Div. 2)/A. Lawn Mower.cpp
Normal file
108
Codeforces Round 1078 (Div. 2)/A. Lawn Mower.cpp
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2194/problem/A */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
ll h, w;
|
||||||
|
cin >> h >> w;
|
||||||
|
cout << h - h / w << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
121
Codeforces Round 1078 (Div. 2)/B. Offshores.cpp
Normal file
121
Codeforces Round 1078 (Div. 2)/B. Offshores.cpp
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2194/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;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
ll sum = 0;
|
||||||
|
repv(i, a) {
|
||||||
|
cin >> i;
|
||||||
|
sum += i / x * y;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
repv(i, a) {
|
||||||
|
rmax(ans, i + sum - i / x * y);
|
||||||
|
}
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
165
Codeforces Round 1078 (Div. 2)/C. Secret message.cpp
Normal file
165
Codeforces Round 1078 (Div. 2)/C. Secret message.cpp
Normal file
@@ -0,0 +1,165 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2194/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;
|
||||||
|
|
||||||
|
bitset<26> ev;
|
||||||
|
rep(i, 26) {
|
||||||
|
ev[i] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
V<string> a(k);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vi divs;
|
||||||
|
vi tp;
|
||||||
|
|
||||||
|
for (int i = 1; i * i <= n; i++) {
|
||||||
|
if (n % i == 0) {
|
||||||
|
divs.push_back(i);
|
||||||
|
if (i * i != n) {
|
||||||
|
tp.push_back(n / i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = tp.size() - 1; i >= 0; i--) {
|
||||||
|
divs.push_back(tp[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
string ans(n, ' ');
|
||||||
|
|
||||||
|
auto check = [&](int sz) {
|
||||||
|
rep(i, sz) {
|
||||||
|
bitset<26> val = ev;
|
||||||
|
|
||||||
|
for (int j = i; j < n; j += sz) {
|
||||||
|
bitset<26> cur;
|
||||||
|
rep(l, k) {
|
||||||
|
cur[a[l][j] - 'a'] = true;
|
||||||
|
}
|
||||||
|
val &= cur;
|
||||||
|
if (val == 0) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
char ch = 63 - __builtin_clzll(val.to_ullong()) + 'a';
|
||||||
|
for (int j = i; j < n; j += sz) {
|
||||||
|
ans[j] = ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
repv(i, divs) {
|
||||||
|
if (check(i)) {
|
||||||
|
cout << ans << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
155
Codeforces Round 1078 (Div. 2)/D. Table Cut.cpp
Normal file
155
Codeforces Round 1078 (Div. 2)/D. Table Cut.cpp
Normal file
@@ -0,0 +1,155 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2194/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;
|
||||||
|
|
||||||
|
vvi a(n, vi(m));
|
||||||
|
ll c = 0;
|
||||||
|
repv(i, a) {
|
||||||
|
repv(j, i) {
|
||||||
|
cin >> j;
|
||||||
|
c += j == 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ll lim = c >> 1;
|
||||||
|
ll ans = lim * ((c + 1) >> 1);
|
||||||
|
ll cur = 0;
|
||||||
|
|
||||||
|
int i = n - 1;
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
|
while (cur < lim) {
|
||||||
|
cur += a[i][j] == 1;
|
||||||
|
j++;
|
||||||
|
if (j >= m) {
|
||||||
|
j = 0;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
string act;
|
||||||
|
rep(k, i) {
|
||||||
|
act.push_back('D');
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(k, j) {
|
||||||
|
act.push_back('R');
|
||||||
|
}
|
||||||
|
|
||||||
|
act.push_back('D');
|
||||||
|
i++;
|
||||||
|
|
||||||
|
nrep(k, j, m) {
|
||||||
|
act.push_back('R');
|
||||||
|
}
|
||||||
|
|
||||||
|
nrep(k, i, n) {
|
||||||
|
act.push_back('D');
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
cout << act << '\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,132 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2216/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;
|
||||||
|
|
||||||
|
vi ans;
|
||||||
|
|
||||||
|
vi lim(k);
|
||||||
|
cin >> lim;
|
||||||
|
|
||||||
|
vi a(n);
|
||||||
|
cin >> a;
|
||||||
|
vi p(n);
|
||||||
|
iota(all(p), 0);
|
||||||
|
sort(all(p), [&](int i, int j){
|
||||||
|
return a[i] > a[j];
|
||||||
|
});
|
||||||
|
|
||||||
|
repv(i, p) {
|
||||||
|
nrep(j, a[i], k + 1) {
|
||||||
|
ans.push_back(i + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans.size() << '\n';
|
||||||
|
repv(i, ans) {
|
||||||
|
cout << i << ' ';
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2216/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;
|
||||||
|
ll p, q;
|
||||||
|
cin >> n >> k >> p >> q;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
vvl pref(3, vl(n + 1));
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
pref[0][i] = pref[0][i - 1] + a[i - 1] % p;
|
||||||
|
pref[1][i] = pref[1][i - 1] + a[i - 1] % q % p;
|
||||||
|
pref[2][i] = pref[2][i - 1] + min(a[i - 1] % p, a[i - 1] % q % p);
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ans = OO;
|
||||||
|
|
||||||
|
nrep(i, k - 1, n) {
|
||||||
|
ll cur = min(pref[0][i + 1] - pref[0][i - k + 1], pref[1][i + 1] - pref[1][i - k + 1]);
|
||||||
|
ll bf = pref[2][i - k + 1] - pref[2][0];
|
||||||
|
ll af = pref[2][n] - pref[2][i + 1];
|
||||||
|
rmin(ans, cur + af + bf);
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
119
Codeforces Round 1093 (Div. 2)/A. Blocked.cpp
Normal file
119
Codeforces Round 1093 (Div. 2)/A. Blocked.cpp
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2220/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;
|
||||||
|
sort(all(a), greater<>());
|
||||||
|
|
||||||
|
nrep(i, 1, n) {
|
||||||
|
if (a[i] == a[i - 1]) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << a;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
127
Codeforces Round 1093 (Div. 2)/B. OIE Excursion.cpp
Normal file
127
Codeforces Round 1093 (Div. 2)/B. OIE Excursion.cpp
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2220/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 m;
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
int c = 1;
|
||||||
|
nrep(i, 1, n) {
|
||||||
|
if (a[i] == a[i - 1]) {
|
||||||
|
c++;
|
||||||
|
if (c >= m) {
|
||||||
|
cout << "NO\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
c = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "YES\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
134
Codeforces Round 1093 (Div. 2)/C. Grid L.cpp
Normal file
134
Codeforces Round 1093 (Div. 2)/C. Grid L.cpp
Normal file
@@ -0,0 +1,134 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2220/problem/C */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
ll p, q;
|
||||||
|
cin >> p >> q;
|
||||||
|
|
||||||
|
auto test = [&](ll p, ll q) -> ll {
|
||||||
|
if (p < 0 || q < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll mi = min(p, q);
|
||||||
|
p -= mi;
|
||||||
|
q -= mi;
|
||||||
|
|
||||||
|
if (q || p % 3) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return mi + p / 3;
|
||||||
|
};
|
||||||
|
|
||||||
|
ll ans = -1;
|
||||||
|
rmax(ans, test(p - 4, q));
|
||||||
|
rmax(ans, test(p, q - 2));
|
||||||
|
rmax(ans, test(p - 2, q - 1));
|
||||||
|
if (ans == -1) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "1 " << 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,164 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2220/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;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
int sz = n * 2 + 1;
|
||||||
|
|
||||||
|
vi p(sz + 1, -1);
|
||||||
|
auto calc = [&](int mid) {
|
||||||
|
if (p[mid] != -1) {
|
||||||
|
return p[mid];
|
||||||
|
}
|
||||||
|
cout << "? " << mid;
|
||||||
|
nrep(i, 1, mid + 1) {
|
||||||
|
cout << ' ' << i;
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
int s;
|
||||||
|
cin >> s;
|
||||||
|
|
||||||
|
cout << "? " << sz - mid;
|
||||||
|
nrep(i, mid + 1, sz + 1) {
|
||||||
|
cout << ' ' << i;
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
|
int t;
|
||||||
|
cin >> t;
|
||||||
|
|
||||||
|
if (s > t) {
|
||||||
|
return p[mid] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (t > s) {
|
||||||
|
return p[mid] = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
return p[mid] = ((mid - s) & 1) * 3;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto ans = [&](int i, int j, int k) {
|
||||||
|
cout << "! " << i << ' ' << j << ' ' << k << endl;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
auto s = [&](int t) {
|
||||||
|
int low = 1;
|
||||||
|
int high = sz;
|
||||||
|
while (low < high) {
|
||||||
|
int mid = (low + high) >> 1;
|
||||||
|
if (calc(mid) >= t) {
|
||||||
|
high = mid;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
low = mid + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return low;
|
||||||
|
};
|
||||||
|
|
||||||
|
ans(s(1), s(2), s(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
193
Codeforces Round 766 (Div. 2)/E. Not Escaping.cpp
Normal file
193
Codeforces Round 766 (Div. 2)/E. Not Escaping.cpp
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/1627/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, k;
|
||||||
|
cin >> n >> m >> k;
|
||||||
|
|
||||||
|
vl x(n);
|
||||||
|
cin >> x;
|
||||||
|
|
||||||
|
vvi var(n);
|
||||||
|
|
||||||
|
V<tuple<int, int, int, int, int>> edges;
|
||||||
|
while (k--) {
|
||||||
|
int a, b, c, d, e;
|
||||||
|
cin >> a >> b >> c >> d >> e;
|
||||||
|
a--, b--, c--, d--;
|
||||||
|
edges.emplace_back(a, b, c, d, e);
|
||||||
|
var[a].push_back(b);
|
||||||
|
var[c].push_back(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
var[0].push_back(0);
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
sortv(var[i]);
|
||||||
|
var[i].erase(unique(all(var[i])), var[i].end());
|
||||||
|
}
|
||||||
|
|
||||||
|
vvl dis(n);
|
||||||
|
V<V<V<tuple<int, int, ll>>>> graph(n);
|
||||||
|
rep(i, n) {
|
||||||
|
graph[i].resize(var[i].size());
|
||||||
|
dis[i].assign(var[i].size(), OO);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto [a, b, c, d, e] : edges) {
|
||||||
|
b = lower_bound(all(var[a]), b) - var[a].begin();
|
||||||
|
d = lower_bound(all(var[c]), d) - var[c].begin();
|
||||||
|
|
||||||
|
graph[a][b].emplace_back(c, d, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
priority_queue<tuple<int, ll, int>, V<tuple<int, ll, int>>, greater<>> pq;
|
||||||
|
dis[0][0] = 0;
|
||||||
|
pq.emplace(0, 0, 0);
|
||||||
|
|
||||||
|
while (!pq.empty()) {
|
||||||
|
auto [i, c, j] = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
|
||||||
|
if (c > dis[i][j]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto [u, v, w] : graph[i][j]) {
|
||||||
|
if (dis[u][v] > c - w) {
|
||||||
|
dis[u][v] = c - w;
|
||||||
|
pq.emplace(u, c - w, v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j > 0) {
|
||||||
|
ll dd = (var[i][j] - var[i][j - 1]) * x[i];
|
||||||
|
if (dis[i][j - 1] > c + dd) {
|
||||||
|
dis[i][j - 1] = c + dd;
|
||||||
|
pq.emplace(i, c + dd, j - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j < var[i].size() - 1) {
|
||||||
|
ll dd = (var[i][j + 1] - var[i][j]) * x[i];
|
||||||
|
if (dis[i][j + 1] > c + dd) {
|
||||||
|
dis[i][j + 1] = c + dd;
|
||||||
|
pq.emplace(i, c + dd, j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ans = OO;
|
||||||
|
rep(i, dis[n - 1].size()) {
|
||||||
|
int v = var[n - 1][i];
|
||||||
|
ll dd = (m - v - 1) * x[n - 1] + dis[n - 1][i];
|
||||||
|
rmin(ans, dd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ans == OO) {
|
||||||
|
cout << "NO ESCAPE\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
167
Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp
Normal file
167
Codeforces Round 867 (Div. 3)/E. Making Anti-Palindromes.cpp
Normal file
@@ -0,0 +1,167 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1822/E */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
string a;
|
||||||
|
cin >> n >> a;
|
||||||
|
|
||||||
|
if (n & 1) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
V<int> c(26);
|
||||||
|
repv(i, a) {
|
||||||
|
c[i - 'a']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, 26) {
|
||||||
|
if (c[i] > (n >> 1)) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
multiset<int, greater<>> c2;
|
||||||
|
fill(all(c), 0);
|
||||||
|
rep(i, n >> 1) {
|
||||||
|
if (a[i] == a[n - i - 1]) {
|
||||||
|
c[a[i] - 'a']++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rep(i, 26) {
|
||||||
|
if (c[i] > 0) {
|
||||||
|
c2.insert(c[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ans = 0;
|
||||||
|
while (c2.size() > 1) {
|
||||||
|
auto itr = c2.begin();
|
||||||
|
auto itr2 = next(itr);
|
||||||
|
|
||||||
|
int ca = *itr;
|
||||||
|
int cb = *itr2;
|
||||||
|
c2.erase(itr);
|
||||||
|
c2.erase(itr2);
|
||||||
|
|
||||||
|
ans++;
|
||||||
|
ca--;
|
||||||
|
cb--;
|
||||||
|
|
||||||
|
if (ca > 0) {
|
||||||
|
c2.insert(ca);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cb > 0) {
|
||||||
|
c2.insert(cb);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!c2.empty()) {
|
||||||
|
ans += *c2.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
141
Codeforces Round 891 (Div. 3)/F. Sum and Product.cpp
Normal file
141
Codeforces Round 891 (Div. 3)/F. Sum and Product.cpp
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1857/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;
|
||||||
|
map<ll, ll> c;
|
||||||
|
rep(i, n) {
|
||||||
|
ll a;
|
||||||
|
cin >> a;
|
||||||
|
c[a]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int q;
|
||||||
|
cin >> q;
|
||||||
|
while (q--) {
|
||||||
|
ll x, y;
|
||||||
|
cin >> x >> y;
|
||||||
|
|
||||||
|
ll d = x*x - 4 * y;
|
||||||
|
if (d < 0) {
|
||||||
|
cout << "0 ";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll r1 = ceill((x - sqrtl(d)) / 2);
|
||||||
|
ll r2 = (x + sqrtl(d)) / 2;
|
||||||
|
if (r1 + r2 != x || r1 * r2 != y) {
|
||||||
|
cout << "0 ";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r1 == r2) {
|
||||||
|
cout << c[r1] * (c[r1] - 1) / 2 << ' ';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << c[r1] * c[r2] << ' ';
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
159
Codeforces Round 913 (Div. 3)/F. Shift and Reverse.cpp
Normal file
159
Codeforces Round 913 (Div. 3)/F. Shift and Reverse.cpp
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1907/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;
|
||||||
|
map<ll, ll> c;
|
||||||
|
vl a(n);
|
||||||
|
repv(i, a) {
|
||||||
|
cin >> i;
|
||||||
|
c[i]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.size() == 1) {
|
||||||
|
cout << "0\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto calcans = [&](vl &a, int val) {
|
||||||
|
ll b = prev(c.end())->first;
|
||||||
|
|
||||||
|
int k = 0;
|
||||||
|
while (a[k] != b) {
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (k < n && a[k] == b) {
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
|
||||||
|
int c = min(n - k, k + val * 2);
|
||||||
|
vl act;
|
||||||
|
nrep(i, k, n) {
|
||||||
|
act.push_back(a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, k) {
|
||||||
|
act.push_back(a[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, n - 1) {
|
||||||
|
if (act[i] > act[i + 1]) {
|
||||||
|
return oo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
};
|
||||||
|
|
||||||
|
int cur = calcans(a, 1);
|
||||||
|
reverse(all(a));
|
||||||
|
rmin(cur, calcans(a, 0) + 1);
|
||||||
|
|
||||||
|
if (cur >= oo) {
|
||||||
|
cout << "-1\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
cout << cur << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
132
Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp
Normal file
132
Codeforces Round 946 (Div. 3)/C. Beautiful Triple Pairs.cpp
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1974/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;
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
map<pair<int, int>, ll> c1;
|
||||||
|
map<pair<int, int>, ll> c2;
|
||||||
|
map<pair<int, int>, ll> c3;
|
||||||
|
map<tuple<int, int, int>, ll> c4;
|
||||||
|
rep(i, n - 2) {
|
||||||
|
int a1 = a[i];
|
||||||
|
int a2 = a[i + 1];
|
||||||
|
int a3 = a[i + 2];
|
||||||
|
|
||||||
|
ans += c1[{a1, a2}];
|
||||||
|
ans += c2[{a1, a3}];
|
||||||
|
ans += c3[{a2, a3}];
|
||||||
|
ans -= c4[{a1, a2, a3}] * 3;
|
||||||
|
|
||||||
|
c1[{a1, a2}]++;
|
||||||
|
c2[{a1, a3}]++;
|
||||||
|
c3[{a2, a3}]++;
|
||||||
|
c4[{a1, a2, a3}]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/* Problem URL: https://codeforces.com/problemset/problem/2021/C2 */
|
/* Problem URL: https://codeforces.com/contest/2021/problem/C2 */
|
||||||
|
|
||||||
#include <bits/stdc++.h>
|
#include <bits/stdc++.h>
|
||||||
#include <ext/pb_ds/assoc_container.hpp>
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
@@ -91,104 +91,75 @@ void solve()
|
|||||||
int n, m, q;
|
int n, m, q;
|
||||||
cin >> n >> m >> q;
|
cin >> n >> m >> q;
|
||||||
|
|
||||||
vi a(n);
|
|
||||||
cin >> a;
|
|
||||||
|
|
||||||
vi var(n);
|
vi var(n);
|
||||||
V<set<int>> ap(n);
|
V<set<int>> p(n);
|
||||||
rep(i, n) {
|
rep(i, n) {
|
||||||
var[a[i] - 1] = i;
|
int x;
|
||||||
ap[i].insert(oo);
|
cin >> x;
|
||||||
|
var[x - 1] = i;
|
||||||
|
p[i].insert(oo);
|
||||||
}
|
}
|
||||||
|
|
||||||
vi b(m);
|
vi b(m);
|
||||||
rep(i, m) {
|
rep(i, m) {
|
||||||
cin >> b[i];
|
cin >> b[i];
|
||||||
ap[var[b[i] - 1]].insert(i);
|
b[i]--;
|
||||||
|
p[var[b[i]]].insert(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sz = n;
|
vi df(n - 1);
|
||||||
while (__builtin_popcount(sz) != 1) {
|
int c = 0;
|
||||||
sz++;
|
rep(i, n - 1) {
|
||||||
|
df[i] = *p[i + 1].begin() - *p[i].begin();
|
||||||
|
c += df[i] < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
V<ordered_set<pair<int, int>>> seg(sz * 2);
|
string ans[] = {
|
||||||
vl sum(sz * 2);
|
"TIDAK",
|
||||||
nrep(i, sz, sz * 2) {
|
"YA"
|
||||||
seg[i].insert({oo, i - sz});
|
};
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = sz - 1; i > 0; i--) {
|
cout << ans[c == 0] << '\n';
|
||||||
repv(j, seg[i * 2]) {
|
|
||||||
seg[i].insert(j);
|
auto rem = [&](int i) {
|
||||||
|
if (i < n - 1 && df[i] < 0) {
|
||||||
|
c--;
|
||||||
}
|
}
|
||||||
repv(j, seg[i * 2 + 1]) {
|
|
||||||
seg[i].insert(j);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto update = [&](int i, int v) {
|
if (i > 0 && df[i - 1] < 0) {
|
||||||
int ind = i;
|
c--;
|
||||||
i += sz;
|
|
||||||
auto bf = *seg[i].begin();
|
|
||||||
seg[i].clear();
|
|
||||||
seg[i].insert({v, i - sz});
|
|
||||||
int now = i & 1;
|
|
||||||
int dd = 0;
|
|
||||||
|
|
||||||
for (i >>= 1; i > 0; i >>= 1) {
|
|
||||||
seg[i].erase(bf);
|
|
||||||
seg[i].insert({v, ind});
|
|
||||||
sum[i] += dd;
|
|
||||||
|
|
||||||
if (now) {
|
|
||||||
int s = seg[i * 2].size() - seg[i * 2].order_of_key({v, oo});
|
|
||||||
int s2 = seg[i * 2].size() - seg[i * 2].order_of_key({bf.first, oo});
|
|
||||||
sum[i] -= s2;
|
|
||||||
sum[i] += s;
|
|
||||||
dd -= s2;
|
|
||||||
dd += s;
|
|
||||||
} else {
|
|
||||||
int s = seg[i * 2 + 1].order_of_key({v, -1});
|
|
||||||
int s2 = seg[i * 2 + 1].order_of_key({bf.first, -1});
|
|
||||||
sum[i] -= s2;
|
|
||||||
sum[i] += s;
|
|
||||||
dd -= s2;
|
|
||||||
dd += s;
|
|
||||||
}
|
|
||||||
|
|
||||||
now = i & 1;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
rep(i, n) {
|
auto add = [&](int i) {
|
||||||
update(i, *ap[i].begin());
|
if (i < n - 1) {
|
||||||
}
|
df[i] = *p[i + 1].begin() - *p[i].begin();
|
||||||
|
c += df[i] < 0;
|
||||||
|
}
|
||||||
|
|
||||||
auto check = [&]() {
|
if (i > 0) {
|
||||||
string ans[] = {
|
df[i - 1] = *p[i].begin() - *p[i - 1].begin();
|
||||||
"TIDAK",
|
c += df[i - 1] < 0;
|
||||||
"YA"
|
}
|
||||||
};
|
|
||||||
cout << ans[sum[1] == 0] << '\n';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
check();
|
|
||||||
while (q--) {
|
while (q--) {
|
||||||
int i, j;
|
int i, j;
|
||||||
cin >> i >> j;
|
cin >> i >> j;
|
||||||
i--;
|
i--, j--;
|
||||||
|
|
||||||
int bf = b[i];
|
int bf = b[i];
|
||||||
b[i] = j;
|
b[i] = j;
|
||||||
int actbf = var[bf - 1];
|
j = var[j];
|
||||||
int actb = var[b[i] - 1];
|
bf = var[bf];
|
||||||
|
rem(bf);
|
||||||
|
p[bf].erase(i);
|
||||||
|
add(bf);
|
||||||
|
rem(j);
|
||||||
|
p[j].insert(i);
|
||||||
|
add(j);
|
||||||
|
|
||||||
ap[actbf].erase(i);
|
cout << ans[c == 0] << '\n';
|
||||||
ap[actb].insert(i);
|
|
||||||
update(actbf, *ap[actbf].begin());
|
|
||||||
update(actb, *ap[actb].begin());
|
|
||||||
check();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,111 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2225/problem/A */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
ll x, y;
|
||||||
|
cin >> x >> y;
|
||||||
|
|
||||||
|
ll mod = y / x;
|
||||||
|
|
||||||
|
cout << (mod > 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,153 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2225/problem/B */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
string a;
|
||||||
|
cin >> a;
|
||||||
|
repv(i, a) {
|
||||||
|
i -= 'a';
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = a.size();
|
||||||
|
vi one(n);
|
||||||
|
int now = 0;
|
||||||
|
rep(i, n) {
|
||||||
|
one[i] = a[i] == now;
|
||||||
|
now ^= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi two(n);
|
||||||
|
now = 1;
|
||||||
|
rep(i, n) {
|
||||||
|
two[i] = a[i] == now;
|
||||||
|
now ^= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto check = [&](vi &a) {
|
||||||
|
int i = 0;
|
||||||
|
while (i < n && a[i] == 0) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i >= n) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < n && a[i] == 1) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (i < n && a[i] == 0) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return i == n;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (check(one) || check(two)) {
|
||||||
|
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,122 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2225/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;
|
||||||
|
|
||||||
|
string a[2];
|
||||||
|
cin >> a[0] >> a[1];
|
||||||
|
|
||||||
|
vi dp(n + 1);
|
||||||
|
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
int p = i - 1;
|
||||||
|
dp[i] = (a[0][p] != a[1][p]) + dp[i - 1];
|
||||||
|
if (i > 1) {
|
||||||
|
rmin(dp[i], (a[0][p] != a[0][p - 1]) + (a[1][p] != a[1][p - 1]) + dp[i - 2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << dp[n] << '\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,133 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2225/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, x;
|
||||||
|
cin >> n >> x;
|
||||||
|
|
||||||
|
const ll mod = 998244353;
|
||||||
|
|
||||||
|
auto calc = [&](ll n, ll x) {
|
||||||
|
if (x < 0) {
|
||||||
|
return 0LL;
|
||||||
|
}
|
||||||
|
ll ans = 0;
|
||||||
|
ll cur = x / 4;
|
||||||
|
if (cur >= 0) {
|
||||||
|
ll bf = cur +1;
|
||||||
|
|
||||||
|
ll m = x % 4;
|
||||||
|
ll up = x + 3 - m;
|
||||||
|
if (up <= n) {
|
||||||
|
ll diff = n - up;
|
||||||
|
diff = diff / 4 + 1;
|
||||||
|
bf %= mod;
|
||||||
|
diff %= mod;
|
||||||
|
ans = (bf * diff) % mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
};
|
||||||
|
|
||||||
|
cout << (calc(n, x) + calc(n - 2, x - 2)) % mod << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2062/E1 */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vl w(n);
|
||||||
|
ordered_set<pair<ll, int>> c;
|
||||||
|
priority_queue<pair<ll, int>> pq;
|
||||||
|
rep(i, n) {
|
||||||
|
cin >> w[i];
|
||||||
|
c.insert({w[i], i});
|
||||||
|
pq.emplace(w[i], i);
|
||||||
|
}
|
||||||
|
|
||||||
|
vvi graph(n);
|
||||||
|
rep(i, n - 1) {
|
||||||
|
int a, b;
|
||||||
|
cin >> a >> b;
|
||||||
|
a--, b--;
|
||||||
|
graph[a].push_back(b);
|
||||||
|
graph[b].push_back(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
vi ac(n);
|
||||||
|
V<ordered_set<pair<ll, int>>> ev(n);
|
||||||
|
|
||||||
|
function<void(int, int)> dfs = [&](int i, int p) {
|
||||||
|
repv(j, graph[i]) {
|
||||||
|
if (j == p) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
dfs(j, i);
|
||||||
|
|
||||||
|
if (ev[j].size() > ev[i].size()) {
|
||||||
|
ev[i].swap(ev[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
repv(k, ev[j]) {
|
||||||
|
ev[i].insert(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ac[i] = ev[i].size() - ev[i].order_of_key({w[i], oo});
|
||||||
|
ev[i].insert({w[i], i});
|
||||||
|
};
|
||||||
|
|
||||||
|
dfs(0, 0);
|
||||||
|
|
||||||
|
while (!pq.empty()) {
|
||||||
|
auto [wi, i] = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
|
||||||
|
if (ac[i] == c.size() - c.order_of_key({wi, oo})) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << i + 1 << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "0\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
139
IME++ Starters Try-outs 2023/J. Jagged Roads.cpp
Normal file
139
IME++ Starters Try-outs 2023/J. Jagged Roads.cpp
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/gym/104415/problem/J */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
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;
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
V<V<pair<int, long double>>> graph(n);
|
||||||
|
while (m--) {
|
||||||
|
int a, b;
|
||||||
|
ll c;
|
||||||
|
cin >> a >> b >> c;
|
||||||
|
a--, b--;
|
||||||
|
graph[a].emplace_back(b, log(c) / log(7));
|
||||||
|
graph[b].emplace_back(a, log(c) / log(7));
|
||||||
|
}
|
||||||
|
|
||||||
|
priority_queue<pair<long double, int>, V<pair<long double, int>>, greater<>> pq;
|
||||||
|
V<long double> dis(n, 1e18L);
|
||||||
|
dis[0] = 0;
|
||||||
|
pq.emplace(0, 0);
|
||||||
|
|
||||||
|
while (!pq.empty()) {
|
||||||
|
auto [c, i] = pq.top();
|
||||||
|
pq.pop();
|
||||||
|
if (c > dis[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto [j, w] : graph[i]) {
|
||||||
|
if (dis[j] > c + w) {
|
||||||
|
dis[j] = c + w;
|
||||||
|
pq.emplace(dis[j], j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << fixed << setprecision(15) << dis[n - 1] << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
151
Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp
Normal file
151
Manthan, Codefest 18 (rated, Div. 1 + Div. 2)/D. Valid BFS?.cpp
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1037/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;
|
||||||
|
vvi graph(n);
|
||||||
|
rep(i, n - 1) {
|
||||||
|
int a, b;
|
||||||
|
cin >> a >> b;
|
||||||
|
a--, b--;
|
||||||
|
graph[a].push_back(b);
|
||||||
|
graph[b].push_back(a);
|
||||||
|
}
|
||||||
|
|
||||||
|
vi ord(n);
|
||||||
|
cin >> ord;
|
||||||
|
|
||||||
|
vvi dict(n + 1);
|
||||||
|
|
||||||
|
function<void(int, int)> dfs = [&](int i, int p) {
|
||||||
|
repv(j, graph[i]) {
|
||||||
|
if (j == p) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
dict[i].push_back(j + 1);
|
||||||
|
dfs(j, i);
|
||||||
|
}
|
||||||
|
sortv(dict[i]);
|
||||||
|
};
|
||||||
|
|
||||||
|
dfs(0, 0);
|
||||||
|
|
||||||
|
int cur = 1;
|
||||||
|
rep(i, n) {
|
||||||
|
vi tmp;
|
||||||
|
rep(j, dict[ord[i] - 1].size()) {
|
||||||
|
tmp.push_back(ord[cur]);
|
||||||
|
cur++;
|
||||||
|
}
|
||||||
|
|
||||||
|
sortv(tmp);
|
||||||
|
if (tmp != dict[ord[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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/2096/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;
|
||||||
|
V<pair<ll, ll>> a(n);
|
||||||
|
map<ll, int> cx;
|
||||||
|
map<ll, int> cy;
|
||||||
|
repv(i, a) {
|
||||||
|
cin >> i.first >> i.second;
|
||||||
|
cx[i.first]++;
|
||||||
|
cy[i.first + i.second]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll x;
|
||||||
|
repv(i, cx) {
|
||||||
|
if (i.second & 1) {
|
||||||
|
x = i.first;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ll y;
|
||||||
|
repv(i, cy) {
|
||||||
|
if (i.second & 1) {
|
||||||
|
y = i.first;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << x << ' ' << y - x << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
2
TODO.md
2
TODO.md
@@ -147,7 +147,7 @@ Official divs from codeforces
|
|||||||
I honestly have no idea how to solve this, the editorial didn't help much, I'll get
|
I honestly have no idea how to solve this, the editorial didn't help much, I'll get
|
||||||
back at it after learning NTT or something.
|
back at it after learning NTT or something.
|
||||||
|
|
||||||
- [ ] [C2. Adjust The Presentation (Hard Version)](https://codeforces.com/problemset/problem/2021/C2)
|
- [X] [C2. Adjust The Presentation (Hard Version)](https://codeforces.com/problemset/problem/2021/C2)
|
||||||
|
|
||||||
I already have the idea figured out, but my solution ended up getting TLE, I'll have to come
|
I already have the idea figured out, but my solution ended up getting TLE, I'll have to come
|
||||||
with something better.
|
with something better.
|
||||||
|
|||||||
@@ -147,6 +147,10 @@ A little collection of interesting problems that I randomly decided to do.
|
|||||||
A really easy problem, at least it is once you realize that it's just a tree and the
|
A really easy problem, at least it is once you realize that it's just a tree and the
|
||||||
calculation is simple.
|
calculation is simple.
|
||||||
|
|
||||||
|
- [E. Not Escaping](https://codeforces.com/problemset/problem/1627/E)
|
||||||
|
|
||||||
|
AMAZING graph problem, I actually had fun solving it.
|
||||||
|
|
||||||
## Math
|
## Math
|
||||||
|
|
||||||
- [E2. Rudolf and Snowflakes (hard version)](https://codeforces.com/problemset/problem/1846/E2)
|
- [E2. Rudolf and Snowflakes (hard version)](https://codeforces.com/problemset/problem/1846/E2)
|
||||||
|
|||||||
Reference in New Issue
Block a user