Add a bunch of other problems
This commit is contained in:
123
Codeforces Round 1051 (Div. 2)/A. All Lengths Subtraction.cpp
Normal file
123
Codeforces Round 1051 (Div. 2)/A. All Lengths Subtraction.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2143/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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi fds(n);
|
||||
cin >> fds;
|
||||
|
||||
int now = n;
|
||||
|
||||
int act = 0;
|
||||
while (fds[act] != now) {
|
||||
act++;
|
||||
}
|
||||
|
||||
int i = act;
|
||||
int j = act;
|
||||
|
||||
bool pos = true;
|
||||
|
||||
rep(k, n - 1) {
|
||||
if (i > 0 && fds[i - 1] == now - 1) {
|
||||
now--;
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (j < n - 1 && fds[j + 1] == now - 1) {
|
||||
now--;
|
||||
j++;
|
||||
continue;
|
||||
}
|
||||
|
||||
pos = false;
|
||||
break;
|
||||
}
|
||||
|
||||
cout << (pos ? "YES\n" : "NO\n");
|
||||
}
|
||||
}
|
||||
130
Codeforces Round 1051 (Div. 2)/B. Discounts.cpp
Normal file
130
Codeforces Round 1051 (Div. 2)/B. Discounts.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2143/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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
ll n, k;
|
||||
cin >> n >> k;
|
||||
|
||||
vl a(n);
|
||||
cin >> a;
|
||||
|
||||
vl b(k);
|
||||
cin >> b;
|
||||
|
||||
sort(all(a), greater<>());
|
||||
sortv(b);
|
||||
|
||||
ll total = 0;
|
||||
int i = 0;
|
||||
int now = 0;
|
||||
|
||||
while (i < n && now < k) {
|
||||
int size = n - i;
|
||||
if (b[now] > size) {
|
||||
while (i < n) {
|
||||
total += a[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
int lim = i + b[now] - 1;
|
||||
while (i < lim) {
|
||||
total += a[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
i++;
|
||||
now++;
|
||||
}
|
||||
|
||||
while (i < n) {
|
||||
total += a[i];
|
||||
i++;
|
||||
}
|
||||
|
||||
cout << total << '\n';
|
||||
}
|
||||
}
|
||||
159
Codeforces Round 1051 (Div. 2)/C. Max Tree.cpp
Normal file
159
Codeforces Round 1051 (Div. 2)/C. Max Tree.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2143/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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
V<V<tuple<int, ll, ll>>> graph(n);
|
||||
|
||||
rep(i, n - 1) {
|
||||
int u, v;
|
||||
ll x, y;
|
||||
cin >> u >> v >> x >> y;
|
||||
|
||||
if (u > v) {
|
||||
swap(u, v);
|
||||
}
|
||||
|
||||
u--, v--;
|
||||
|
||||
graph[u].emplace_back(v, x, y);
|
||||
graph[v].emplace_back(u, 0, 0);
|
||||
}
|
||||
|
||||
vvi newgraph(n);
|
||||
|
||||
function<void(int, int)> dfs = [&](int i, int p) {
|
||||
for (auto [j, x, y] : graph[i]) {
|
||||
if (i < j) {
|
||||
if (x > y) {
|
||||
newgraph[i].push_back(j);
|
||||
continue;
|
||||
}
|
||||
if (y > x) {
|
||||
newgraph[j].push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (j == p) {
|
||||
continue;
|
||||
}
|
||||
|
||||
dfs(j, i);
|
||||
}
|
||||
};
|
||||
|
||||
dfs(0, 0);
|
||||
|
||||
int now = 1;
|
||||
vi act(n);
|
||||
|
||||
V<bool> vis(n);
|
||||
|
||||
function<void(int)> getans = [&](int i) {
|
||||
vis[i] = true;
|
||||
|
||||
for (auto j : newgraph[i]) {
|
||||
if (vis[j]) {
|
||||
continue;
|
||||
}
|
||||
getans(j);
|
||||
}
|
||||
|
||||
act[i] = now;
|
||||
now++;
|
||||
};
|
||||
|
||||
rep(i, n) {
|
||||
if (vis[i]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
getans(i);
|
||||
}
|
||||
|
||||
cout << act;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2143/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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
vi a(n);
|
||||
cin >> a;
|
||||
|
||||
ll mod = 1e9 + 7;
|
||||
|
||||
vvvl memo(n, vvl(n + 1, vl(n + 1, -1)));
|
||||
|
||||
function<ll(int, int, int)> dp = [&](int i, int maximal, int mp) {
|
||||
if (i >= n) {
|
||||
return 1LL;
|
||||
}
|
||||
|
||||
ll &ans = memo[i][maximal][mp];
|
||||
if (ans != -1) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
ans = dp(i + 1, maximal, mp);
|
||||
|
||||
if (a[i] < maximal && a[i] < mp) {
|
||||
return ans;
|
||||
}
|
||||
|
||||
if (a[i] >= maximal) {
|
||||
ans += dp(i + 1, a[i], mp);
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
if (a[i] < maximal) {
|
||||
ans += dp(i + 1, maximal, max(mp, a[i]));
|
||||
ans %= mod;
|
||||
}
|
||||
|
||||
return ans;
|
||||
};
|
||||
|
||||
cout << dp(0, 0, 0) << '\n';
|
||||
}
|
||||
}
|
||||
133
Codeforces Round 1051 (Div. 2)/E. Make Good.cpp
Normal file
133
Codeforces Round 1051 (Div. 2)/E. Make Good.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
/* Problem URL: https://codeforces.com/contest/2143/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;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
ios::sync_with_stdio(false);
|
||||
cin.tie(nullptr);
|
||||
|
||||
int t;
|
||||
cin >> t;
|
||||
while (t--) {
|
||||
int n;
|
||||
cin >> n;
|
||||
|
||||
string par;
|
||||
cin >> par;
|
||||
|
||||
if (n & 1) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
vi count(2);
|
||||
repv(i, par) {
|
||||
count[i == ')']++;
|
||||
}
|
||||
|
||||
if (abs(count[0] - count[1]) % 4 != 0) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
string fds;
|
||||
int c = 0;
|
||||
|
||||
repv(i, par) {
|
||||
fds += i;
|
||||
|
||||
if (fds.size() > 1 && fds[fds.size() - 1] == fds[fds.size() - 2]) {
|
||||
fds.pop_back();
|
||||
fds.pop_back();
|
||||
c++;
|
||||
}
|
||||
}
|
||||
|
||||
if (!fds.empty() && (fds.back() == '(') && c == 0) {
|
||||
cout << "-1\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c) {
|
||||
fds = "((" + fds + "))";
|
||||
c -= 2;
|
||||
}
|
||||
|
||||
fds = string(c, '(') + fds + string(c, ')');
|
||||
cout << fds << '\n';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user