More stuff
This commit is contained in:
148
Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp
Normal file
148
Codeforces Round 1069 (Div. 2)/C. Needle in a Haystack.cpp
Normal file
@@ -0,0 +1,148 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2175/problem/C */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
string a, b;
|
||||||
|
cin >> a >> b;
|
||||||
|
|
||||||
|
vi count(26);
|
||||||
|
vi count2(26);
|
||||||
|
|
||||||
|
repv(i, a) {
|
||||||
|
count2[i - 'a']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
repv(i, b) {
|
||||||
|
count[i - 'a']++;
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, 26) {
|
||||||
|
if (count[i] < count2[i]) {
|
||||||
|
cout << "Impossible\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
count[i] -= count2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
int cur = 0;
|
||||||
|
string ans;
|
||||||
|
|
||||||
|
rep(i, 26) {
|
||||||
|
while (cur < a.size() && a[cur] <= i + 'a') {
|
||||||
|
ans.push_back(a[cur]);
|
||||||
|
cur++;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (count[i]) {
|
||||||
|
count[i]--;
|
||||||
|
ans.push_back(i + 'a');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
while (cur < a.size()) {
|
||||||
|
ans.push_back(a[cur]);
|
||||||
|
cur++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
110
Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp
Normal file
110
Codeforces Round 1074 (Div. 4)/A. Perfect Root.cpp
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2185/problem/A */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
cout << i * i << " \n"[i == n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
112
Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp
Normal file
112
Codeforces Round 1080 (Div. 3)/A. Sieve of Erato67henes.cpp
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2195/problem/A */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
cout << (count(all(a), 67) ? "YES\n" : "NO\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
152
Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp
Normal file
152
Codeforces Round 1080 (Div. 3)/B. Heapify 1.cpp
Normal file
@@ -0,0 +1,152 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2195/problem/B */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vi a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vvi s;
|
||||||
|
vi st;
|
||||||
|
V<bool> vis(n);
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
if (vis[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
s.emplace_back();
|
||||||
|
st.push_back(i);
|
||||||
|
for (int j = i; j < n; j = (j + 1) * 2 - 1) {
|
||||||
|
vis[j] = true;
|
||||||
|
s.back().push_back(j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repv(i, s) {
|
||||||
|
sort(all(i), [&](int i, int j){
|
||||||
|
return a[i - 1] < a[j - 1];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
vi ans(n);
|
||||||
|
|
||||||
|
rep(i, s.size()) {
|
||||||
|
int start = st[i];
|
||||||
|
auto &v = s[i];
|
||||||
|
|
||||||
|
for (int j = 0; j < v.size(); j++, start = (start + 1) * 2 - 1) {
|
||||||
|
ans[start] = a[v[j] - 1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
nrep(i, 1, n) {
|
||||||
|
if (ans[i] < ans[i - 1]) {
|
||||||
|
cout << "NO\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "YES\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
128
Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp
Normal file
128
Codeforces Round 1080 (Div. 3)/C. Dice Roll Sequence.cpp
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2195/problem/C */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vi a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vvl dp(n + 1, vl(6, OO));
|
||||||
|
fill(all(dp[0]), 0);
|
||||||
|
|
||||||
|
nrep(i, 1, n + 1) {
|
||||||
|
int cur = a[i - 1] - 1;
|
||||||
|
|
||||||
|
rep(j, 6) {
|
||||||
|
int add = cur != j;
|
||||||
|
rep(k, 6) {
|
||||||
|
if (k == j || k == 6 - j - 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rmin(dp[i][j], add + dp[i - 1][k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << *min_element(all(dp.back())) << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
123
Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp
Normal file
123
Codeforces Round 1080 (Div. 3)/D. Absolute Cinema.cpp
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2195/problem/D */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
ll sum = a[0] + a.back();
|
||||||
|
for (int i = n - 1; i > 0; i--) {
|
||||||
|
a[i] = a[i - 1] - a[i];
|
||||||
|
}
|
||||||
|
a[0] = sum / (n - 1);
|
||||||
|
|
||||||
|
vl ans(n);
|
||||||
|
ans.back() = (a[0] + a.back()) / 2;
|
||||||
|
rep(i, n - 1) {
|
||||||
|
ans[i] = (a[i] - a[i + 1]) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
153
Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp
Normal file
153
Codeforces Round 839 (Div. 3)/E. Permutation Game.cpp
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1772/E */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
vi a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vi p1(n);
|
||||||
|
vi p2(n);
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
p1[i] = (a[i] != (i + 1));
|
||||||
|
p2[i] = (a[i] != (n - i));
|
||||||
|
}
|
||||||
|
|
||||||
|
int c[2] = {0, 0};
|
||||||
|
int d = 0;
|
||||||
|
rep(i, n) {
|
||||||
|
if (p1[i] && p1[i] == p2[i]) {
|
||||||
|
d++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
c[0] += p1[i];
|
||||||
|
c[1] += p2[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
int cur = 0;
|
||||||
|
while (1) {
|
||||||
|
if (c[cur]) {
|
||||||
|
c[cur]--;
|
||||||
|
cur ^= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (d) {
|
||||||
|
if (d == 1 && c[cur^1] == 0) {
|
||||||
|
cout << "Tie\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
d--;
|
||||||
|
cur ^= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
string win[] = {"First", "Second"};
|
||||||
|
cout << win[cur] << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
205
Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp
Normal file
205
Codeforces Round 856 (Div. 2)/D. Counting Factorizations.cpp
Normal file
@@ -0,0 +1,205 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1794/D */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
const ll mod = 998244353;
|
||||||
|
|
||||||
|
constexpr MAXN = 1e6 + 10;
|
||||||
|
bool prime[MAXN];
|
||||||
|
bool vis[MAXN];
|
||||||
|
|
||||||
|
vl fact[MAXN];
|
||||||
|
vl inv[MAXN];
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
fill(prime, prime + MAXN, true);
|
||||||
|
prime[0] = false;
|
||||||
|
prime[1] = false;
|
||||||
|
|
||||||
|
nrep(i, 2, MAXN) {
|
||||||
|
if (!prime[i]) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = i * 2; j < MAXN; j += i) {
|
||||||
|
prime[j] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fact[0] = 1;
|
||||||
|
fact[1] = 1;
|
||||||
|
nrep(i, 2, MAXN) {
|
||||||
|
fact[i] = fact[i - 1] * i % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
inv[0] = 1;
|
||||||
|
inv[1] = 1;
|
||||||
|
nrep(i, 2, MAXN) {
|
||||||
|
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
|
||||||
|
}
|
||||||
|
|
||||||
|
nrep(i, 2, MAXN) {
|
||||||
|
inv[i] = inv[i - 1] * inv[i] % mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
vi act(n << 1);
|
||||||
|
cin >> act;
|
||||||
|
|
||||||
|
set<int> up;
|
||||||
|
|
||||||
|
vi ot;
|
||||||
|
vi primes;
|
||||||
|
|
||||||
|
rep(i, n << 1) {
|
||||||
|
if (prime[act[i]]) {
|
||||||
|
if (vis[act[i]]) {
|
||||||
|
ot.push_back(act[i]);
|
||||||
|
} else {
|
||||||
|
up.insert(act[i]);
|
||||||
|
vis[act[i]] = true;
|
||||||
|
primes.push_back(act[i]);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ot.push_back(act[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (primes.size() < n) {
|
||||||
|
cout << "0\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto comb = [&](int n, int k) {
|
||||||
|
return fact[n] * inv[k] % mod * inv[n - k] % mod;
|
||||||
|
};
|
||||||
|
|
||||||
|
ll ans = comb(primes.size(), n);
|
||||||
|
|
||||||
|
if (ot.empty()) {
|
||||||
|
cout << ans << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sortv(ot);
|
||||||
|
|
||||||
|
int c = 1;
|
||||||
|
vi tmp;
|
||||||
|
|
||||||
|
nrep(i, 1, ot.size()) {
|
||||||
|
if (ot[i] == ot[i - 1]) {
|
||||||
|
c++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp.push_back(c);
|
||||||
|
c = 1;
|
||||||
|
}
|
||||||
|
tmp.push_back(c);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
repv(i, up) {
|
||||||
|
vis[i] = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
217
Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp
Normal file
217
Codeforces Round 895 (Div. 3)/E. Data Structures Fan.cpp
Normal file
@@ -0,0 +1,217 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1872/E */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
string b;
|
||||||
|
cin >> b;
|
||||||
|
|
||||||
|
while (__builtin_popcount(n) != 1) {
|
||||||
|
n++;
|
||||||
|
a.push_back(0);
|
||||||
|
b.push_back('0');
|
||||||
|
}
|
||||||
|
|
||||||
|
vl seg0(n << 1);
|
||||||
|
vl seg1(n << 1);
|
||||||
|
vi lazy(n << 1);
|
||||||
|
|
||||||
|
nrep(i, n, n << 1) {
|
||||||
|
if (b[i - n] == '0') {
|
||||||
|
seg0[i] = a[i - n];
|
||||||
|
} else {
|
||||||
|
seg1[i] = a[i - n];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = n - 1; i > 0; i--) {
|
||||||
|
seg0[i] = seg0[i * 2] ^ seg0[i * 2 + 1];
|
||||||
|
seg1[i] = seg1[i * 2] ^ seg1[i * 2 + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
auto propagate = [&](int i) {
|
||||||
|
if (!lazy[i]) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy[i] = 0;
|
||||||
|
|
||||||
|
swap(seg0[i], seg1[i]);
|
||||||
|
|
||||||
|
if (i < n) {
|
||||||
|
lazy[i * 2] ^= 1;
|
||||||
|
lazy[i * 2 + 1] ^= 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function<pair<ll, ll>(int, int, int, int, int)> update = [&](int i, int l, int r, int tl, int tr) -> pair<ll, ll> {
|
||||||
|
propagate(i);
|
||||||
|
|
||||||
|
if (l > tr || r < tl) {
|
||||||
|
return {seg0[i], seg1[i]};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (l >= tl && r <= tr) {
|
||||||
|
lazy[i] ^= 1;
|
||||||
|
propagate(i);
|
||||||
|
return {seg0[i], seg1[i]};
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = (l + r) >> 1;
|
||||||
|
|
||||||
|
auto a1 = update(i * 2, l, mid, tl, tr);
|
||||||
|
auto a2 = update(i * 2 + 1, mid + 1, r, tl, tr);
|
||||||
|
|
||||||
|
seg0[i] = a1.first ^ a2.first;
|
||||||
|
seg1[i] = a1.second ^ a2.second;
|
||||||
|
|
||||||
|
return {seg0[i], seg1[i]};
|
||||||
|
};
|
||||||
|
|
||||||
|
function<pair<ll, ll>(int, int, int, int, int)> query = [&](int i, int l, int r, int tl, int tr) -> pair<ll, ll> {
|
||||||
|
if (l > tr || r < tl) {
|
||||||
|
return {0, 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
propagate(i);
|
||||||
|
|
||||||
|
if (l >= tl && r <= tr) {
|
||||||
|
return {seg0[i], seg1[i]};
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = (l + r) >> 1;
|
||||||
|
|
||||||
|
auto a1 = query(i * 2, l, mid, tl, tr);
|
||||||
|
auto a2 = query(i * 2 + 1, mid + 1, r, tl, tr);
|
||||||
|
|
||||||
|
return {a1.first ^ a2.first, a1.second ^ a2.second};
|
||||||
|
};
|
||||||
|
|
||||||
|
int q;
|
||||||
|
cin >> q;
|
||||||
|
while (q--) {
|
||||||
|
int op;
|
||||||
|
cin >> op;
|
||||||
|
if (op == 1) {
|
||||||
|
int l, r;
|
||||||
|
cin >> l >> r;
|
||||||
|
update(1, 0, n - 1, l - 1, r - 1);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
int g;
|
||||||
|
cin >> g;
|
||||||
|
|
||||||
|
if (g == 0) {
|
||||||
|
cout << query(1, 0, n - 1, 0, n - 1).first << ' ';
|
||||||
|
} else {
|
||||||
|
cout << query(1, 0, n - 1, 0, n - 1).second << ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
156
Codeforces Round 914 (Div. 2)/C. Array Game.cpp
Normal file
156
Codeforces Round 914 (Div. 2)/C. Array Game.cpp
Normal file
@@ -0,0 +1,156 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1904/C */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
ll k;
|
||||||
|
cin >> n >> k;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
if (k >= 3) {
|
||||||
|
cout << "0\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sort(all(a), greater<>());
|
||||||
|
|
||||||
|
ll ans = *min_element(all(a));
|
||||||
|
|
||||||
|
vl ot;
|
||||||
|
|
||||||
|
rep(i, n - 1) {
|
||||||
|
nrep(j, i + 1, n) {
|
||||||
|
rmin(ans, max(a[i] - a[j] * k, a[i] % a[j]));
|
||||||
|
|
||||||
|
if (k > 1) {
|
||||||
|
ll cur = a[i] - a[j];
|
||||||
|
int ind = lower_bound(all(a), cur, greater<>()) - a.begin();
|
||||||
|
if (ind < n) {
|
||||||
|
rmin(ans, abs(a[ind] - cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ind < n - 1) {
|
||||||
|
rmin(ans, abs(a[ind + 1] - cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ind < n - 2) {
|
||||||
|
rmin(ans, abs(a[ind + 2] - cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ind > 0) {
|
||||||
|
rmin(ans, abs(a[ind - 1] - cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ind > 1) {
|
||||||
|
rmin(ans, abs(a[ind - 2] - cur));
|
||||||
|
}
|
||||||
|
|
||||||
|
rmin(ans, abs(cur - a[j]));
|
||||||
|
rmin(ans, abs(cur - a[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
136
Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp
Normal file
136
Codeforces Round 924 (Div. 2)/D. Lonely Mountain Dungeons.cpp
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1928/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;
|
||||||
|
ll b;
|
||||||
|
ll x;
|
||||||
|
cin >> n >> b >> x;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
int m = *max_element(all(a));
|
||||||
|
|
||||||
|
vi s(n);
|
||||||
|
vl ac(n);
|
||||||
|
|
||||||
|
vvi t(m);
|
||||||
|
ll total = -x * (m - 1);
|
||||||
|
rep(i, n) {
|
||||||
|
rep(j, a[i]) {
|
||||||
|
t[j].push_back(i);
|
||||||
|
}
|
||||||
|
total += a[i] * (a[i - 1]) / 2 * b;
|
||||||
|
ac[i] = a[i] * (a[i - 1]) / 2 * b;
|
||||||
|
}
|
||||||
|
|
||||||
|
ll ans = total;
|
||||||
|
for (int i = m - 1; i > 0; i--) {
|
||||||
|
repv(j, t[i]) {
|
||||||
|
total -= ac[j];
|
||||||
|
ll sz = i;
|
||||||
|
ll cmp = a[i] % sz;
|
||||||
|
sz -= cmp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
132
Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp
Normal file
132
Codeforces Round 927 (Div. 3)/E. Final Countdown.cpp
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1932/E */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
cin >> n;
|
||||||
|
string a;
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
reverse(all(a));
|
||||||
|
|
||||||
|
vi tmp(n + 1);
|
||||||
|
for (int i = n - 1; i >= 0; i--) {
|
||||||
|
tmp[i] = tmp[i + 1] + (a[i] - '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
string res;
|
||||||
|
|
||||||
|
int c = 0;
|
||||||
|
rep(i, n) {
|
||||||
|
c += tmp[i];
|
||||||
|
res.push_back(c % 10 + '0');
|
||||||
|
c /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.push_back(c + '0');
|
||||||
|
while (res.back() == '0') {
|
||||||
|
res.pop_back();
|
||||||
|
}
|
||||||
|
reverse(all(res));
|
||||||
|
cout << res << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
185
Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp
Normal file
185
Codeforces Round 946 (Div. 3)/D. Ingenuity-2.cpp
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1974/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;
|
||||||
|
string a;
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
|
||||||
|
pair<int, int> var[256];
|
||||||
|
var['N'] = {0, 1};
|
||||||
|
var['S'] = {0, -1};
|
||||||
|
var['E'] = {1, 0};
|
||||||
|
var['W'] = {-1, 0};
|
||||||
|
|
||||||
|
repv(i, a) {
|
||||||
|
x += var[i].first;
|
||||||
|
y += var[i].second;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((x & 1) || (y & 1)) {
|
||||||
|
cout << "NO\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
x >>= 1;
|
||||||
|
y >>= 1;
|
||||||
|
|
||||||
|
if (x == 0 && y == 0) {
|
||||||
|
if (a.size() == 2) {
|
||||||
|
cout << "NO\n";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int ind1 = find(all(a), 'N') - a.begin();
|
||||||
|
int ind2 = find(all(a), 'S') - a.begin();
|
||||||
|
if (ind1 == n || ind2 == n) {
|
||||||
|
ind1 = find(all(a), 'E') - a.begin();
|
||||||
|
ind2 = find(all(a), 'W') - a.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
if (i == ind1 || i == ind2) {
|
||||||
|
cout << 'H';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "R";
|
||||||
|
}
|
||||||
|
cout << '\n';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
repv(i, a) {
|
||||||
|
if (i == 'N' && y > 0) {
|
||||||
|
y--;
|
||||||
|
cout << "R";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 'S' && y < 0) {
|
||||||
|
y++;
|
||||||
|
cout << "R";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 'E' && x > 0) {
|
||||||
|
x--;
|
||||||
|
cout << "R";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i == 'W' && x < 0) {
|
||||||
|
x++;
|
||||||
|
cout << "R";
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "H";
|
||||||
|
}
|
||||||
|
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,212 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/problemset/problem/1985/H1 */
|
||||||
|
|
||||||
|
#include <bits/stdc++.h>
|
||||||
|
#include <ext/pb_ds/assoc_container.hpp>
|
||||||
|
#include <ext/pb_ds/tree_policy.hpp>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace __gnu_pbds;
|
||||||
|
|
||||||
|
template <class T, class comp = less<>>
|
||||||
|
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
|
||||||
|
|
||||||
|
#define V vector
|
||||||
|
|
||||||
|
#define rmin(a, b) a = min(a, b)
|
||||||
|
#define rmax(a, b) a = max(a, b)
|
||||||
|
|
||||||
|
#define rep(i, lim) for (int i = 0; i < (lim); i++)
|
||||||
|
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
|
||||||
|
|
||||||
|
#define repv(i, v) for (auto &i : (v))
|
||||||
|
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
|
||||||
|
#define sortv(v) sort(v.begin(), v.end())
|
||||||
|
#define all(v) (v).begin(), (v).end()
|
||||||
|
|
||||||
|
using vi = vector<int>;
|
||||||
|
using vvi = vector<vi>;
|
||||||
|
using vvvi = vector<vvi>;
|
||||||
|
using vvvvi = vector<vvvi>;
|
||||||
|
|
||||||
|
using ll = long long;
|
||||||
|
|
||||||
|
using vl = vector<ll>;
|
||||||
|
using vvl = vector<vl>;
|
||||||
|
using vvvl = vector<vvl>;
|
||||||
|
using vvvvl = vector<vvvl>;
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
|
||||||
|
os << vec[0];
|
||||||
|
for (size_t i = 1; i < vec.size(); i++) {
|
||||||
|
os << ' ' << vec[i];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<v> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
is >> i;
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
os << i[0];
|
||||||
|
for (size_t j = 1; j < i.size(); j++) {
|
||||||
|
os << ' ' << i[j];
|
||||||
|
}
|
||||||
|
os << '\n';
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class v>
|
||||||
|
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
|
||||||
|
for (auto &i : vec) {
|
||||||
|
for (auto &j : i) {
|
||||||
|
is >> j;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return is;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int oo = INT32_MAX >> 1;
|
||||||
|
const ll OO = INT64_MAX >> 1;
|
||||||
|
|
||||||
|
|
||||||
|
void pre()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define TEST 1
|
||||||
|
|
||||||
|
void solve()
|
||||||
|
{
|
||||||
|
int n, m;
|
||||||
|
cin >> n >> m;
|
||||||
|
|
||||||
|
V<string> a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
vi dsu(n * m);
|
||||||
|
vi size(n * m);
|
||||||
|
rep(i, n * m) {
|
||||||
|
dsu[i] = i;
|
||||||
|
size[i] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
stack<pair<int, int>> s;
|
||||||
|
auto save = [&]() {
|
||||||
|
s.emplace(-1, -1);
|
||||||
|
};
|
||||||
|
|
||||||
|
auto roll = [&]() {
|
||||||
|
while (s.top().first != -1) {
|
||||||
|
auto [i, j] = s.top();
|
||||||
|
size[i] -= size[j];
|
||||||
|
dsu[j] = j;
|
||||||
|
s.pop();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
function<int(int)> find_p = [&](int i) {
|
||||||
|
if (dsu[i] == i) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return find_p(dsu[i]);
|
||||||
|
};
|
||||||
|
|
||||||
|
int ans = 1;
|
||||||
|
|
||||||
|
auto join = [&](int i, int j) {
|
||||||
|
i = find_p(i);
|
||||||
|
j = find_p(j);
|
||||||
|
|
||||||
|
if (i == j) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size[i] < size[j]) {
|
||||||
|
swap(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
s.emplace(i, j);
|
||||||
|
size[i] += size[j];
|
||||||
|
rmax(ans, size[i]);
|
||||||
|
dsu[j] = i;
|
||||||
|
};
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
rep(j, m) {
|
||||||
|
if (a[i][j] == '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i > 0 && a[i - 1][j] == '#') {
|
||||||
|
join(i * m + j, (i - 1) * m + j);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j > 0 && a[i][j - 1] == '#') {
|
||||||
|
join(i * m + j, i * m + j - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(i, n) {
|
||||||
|
save();
|
||||||
|
rep(j, m) {
|
||||||
|
if (i > 0 && a[i - 1][j] == '#') {
|
||||||
|
join(i * m + j, (i - 1) * m + j);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (i < n - 1 && a[i + 1][j] == '#') {
|
||||||
|
join(i * m + j, (i + 1) * m + j);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j > 0) {
|
||||||
|
join(i * m + j, i * m + j - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
roll();
|
||||||
|
}
|
||||||
|
|
||||||
|
rep(j, m) {
|
||||||
|
save();
|
||||||
|
rep(i, n) {
|
||||||
|
if (i > 0) {
|
||||||
|
join(i * m + j, (i - 1) * m + j);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j > 0 && a[i][j - 1] == '#') {
|
||||||
|
join(i * m + j, i * m + j - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (j < m - 1 && a[i][j + 1] == '#') {
|
||||||
|
join(i * m + j, i * m + j + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
roll();
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << ans << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
121
Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp
Normal file
121
Codeforces Round 978 (Div. 2)/B. Kar Salesman.cpp
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
/* Problem URL: https://codeforces.com/contest/2022/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, x;
|
||||||
|
cin >> n >> x;
|
||||||
|
|
||||||
|
ll ans = 0;
|
||||||
|
|
||||||
|
vl a(n);
|
||||||
|
cin >> a;
|
||||||
|
|
||||||
|
ll ma = 0;
|
||||||
|
ll total = 0;
|
||||||
|
repv(i, a) {
|
||||||
|
rmax(ma, i);
|
||||||
|
total += i;
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << max(ma, (total + x - 1) / x) << '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
ios::sync_with_stdio(false);
|
||||||
|
cin.tie(nullptr);
|
||||||
|
|
||||||
|
pre();
|
||||||
|
|
||||||
|
int t;
|
||||||
|
(TEST && cin >> t) || (t = 1);
|
||||||
|
while (t--) {
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
10
TODO.md
10
TODO.md
@@ -111,6 +111,16 @@ Official divs from codeforces
|
|||||||
Another problem that's kinda interesting, but I guess my head is not working
|
Another problem that's kinda interesting, but I guess my head is not working
|
||||||
recently, I'll get back to it once I can finally work again.
|
recently, I'll get back to it once I can finally work again.
|
||||||
|
|
||||||
|
- [ ] [D. Counting Factorizations](https://codeforces.com/problemset/problem/1794/D)
|
||||||
|
|
||||||
|
I didn't have enough time and just wanted to do something else when I was doing
|
||||||
|
this problem, I should get back to it when I have the head for it.
|
||||||
|
|
||||||
|
- [ ] [D. Lonely Mountain Dungeons](https://codeforces.com/problemset/problem/1928/D)
|
||||||
|
|
||||||
|
I got everything I need to do it, but I still need to find the equation to solve
|
||||||
|
every single case...
|
||||||
|
|
||||||
- Div 3
|
- Div 3
|
||||||
|
|
||||||
- [ ] [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G)
|
- [ ] [G. Cry Me a River](https://codeforces.com/contest/2137/problem/G)
|
||||||
|
|||||||
@@ -112,3 +112,9 @@ A little collection of interesting problems that I randomly decided to do.
|
|||||||
Not so hard if you know how to deal with C++ or any other language with big integer, but
|
Not so hard if you know how to deal with C++ or any other language with big integer, but
|
||||||
it's interesting to think about regardless, can also be solved with a binary search, but
|
it's interesting to think about regardless, can also be solved with a binary search, but
|
||||||
it's not required.
|
it's not required.
|
||||||
|
|
||||||
|
## DSU
|
||||||
|
|
||||||
|
- [https://codeforces.com/problemset/problem/1985/H1](https://codeforces.com/problemset/problem/1985/H1)
|
||||||
|
|
||||||
|
DSU with rollback problem, cool to think about and not really that difficult.
|
||||||
|
|||||||
Reference in New Issue
Block a user