Add more problems

This commit is contained in:
2026-03-24 17:04:11 -03:00
parent eda108e6b9
commit bfbe19b7d4
5 changed files with 758 additions and 0 deletions

View File

@@ -0,0 +1,177 @@
/* Problem URL: https://codeforces.com/problemset/problem/1854/A1 */
#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;
int mi = oo;
int mii = -1;
int ma = -oo;
int mai = -1;
rep(i, n) {
if (a[i] < mi) {
mi = a[i];
mii = i;
}
if (a[i] >= ma) {
ma = a[i];
mai = i;
}
}
if (ma == mi) {
cout << "0\n";
return;
}
V<pair<int, int>> ans;
auto printans = [&]() {
cout << ans.size() << '\n';
repv(i, ans) {
cout << i.first << ' ' << i.second << '\n';
}
};
if (ma > 0) {
ans.emplace_back(mai + 1, mai + 1);
ans.emplace_back(mai + 1, mai + 1);
ans.emplace_back(mai + 1, mai + 1);
ans.emplace_back(mai + 1, mai + 1);
ans.emplace_back(mai + 1, mai + 1);
ans.emplace_back(n, mai + 1);
ans.emplace_back(n, n);
ans.emplace_back(n, n);
ans.emplace_back(n, n);
rep(i, n) {
ans.emplace_back(i + 1, n);
ans.emplace_back(n, n);
}
printans();
return;
}
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(mii + 1, mii + 1);
ans.emplace_back(1, mii + 1);
ans.emplace_back(1, 1);
ans.emplace_back(1, 1);
ans.emplace_back(1, 1);
for (int i = n - 1; i >= 0; i--) {
ans.emplace_back(i + 1, 1);
ans.emplace_back(1, 1);
}
printans();
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,144 @@
/* Problem URL: https://codeforces.com/problemset/problem/1876/B */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
void pre()
{
}
#define TEST 0
void solve()
{
int n;
cin >> n;
vi a(n);
cin >> a;
vl c(2e5);
rep(i, n) {
for (int j = i + 1; j <= n; j += i + 1) {
rmax(a[i], a[j - 1]);
}
c[a[i]]++;
}
const ll mod = 998244353;
auto fpow = [&](ll a, ll p) {
ll ans = 1;
rep(i, 61) {
if ((p >> i) & 1) {
(ans *= a) %= mod;
}
(a *= a) %= mod;
}
return ans;
};
ll ans = 0;
for (int i = 2e5 - 1; i >= 0; i--) {
if (c[i] == 0) {
continue;
}
n -= c[i];
(ans += ((fpow(2, c[i]) - 1) % mod + mod) % mod * fpow(2, n) % mod * i % mod) %= 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();
}
}

View File

@@ -0,0 +1,181 @@
/* Problem URL: https://codeforces.com/problemset/problem/2044/G2 */
#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);
vvi rev(n);
rep(i, n) {
cin >> p[i];
p[i]--;
rev[p[i]].push_back(i);
}
V<bool> vis(n);
V<bool> loop(n);
vvi ne(20, vi(n, -1));
rep(i, n) {
ne[0][i] = p[i];
}
function<void(int)> getloop = [&](int i) {
vis[i] = true;
int t = p[i];
int h = p[t];
while (t != h) {
if (vis[t]) {
return;
}
vis[t] = true;
t = p[t];
h = p[p[h]];
}
vis[t] = true;
loop[t] = true;
h = p[h];
while (h != t) {
loop[h] = true;
vis[h] = true;
h = p[h];
}
};
rep(i, n) {
if (vis[i]) {
continue;
}
getloop(i);
}
ll ans = 0;
function<ll(int)> dfs = [&](int i) {
ll ans = 1;
repv(j, rev[i]) {
ans += dfs(j);
}
return ans;
};
rep(i, n) {
if (!loop[i]) {
continue;
}
repv(j, rev[i]) {
if (loop[j]) {
continue;
}
rmax(ans, dfs(j));
}
}
cout << ans + 2 << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -0,0 +1,247 @@
/* Problem URL: https://codeforces.com/problemset/problem/2096/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;
vvl h(n, vl(n));
cin >> h;
V<V<pair<int, ll>>> graph(n * 2);
auto addedge = [&](int u, int v, ll c)
{
graph[u].emplace_back(v, c);
};
vl a(n);
vl b(n);
cin >> a >> b;
vl memo(n * 2, -1);
function<ll(int)> dfs = [&](int i) {
if (i >= (n - 1) * 2) {
return 0LL;
}
ll &ans = memo[i];
if (ans != -1) {
return ans;
}
ans = OO;
repv(j, graph[i]) {
ll tmp = dfs(j.first);
if (tmp == OO) {
continue;
}
rmin(ans, tmp + j.second);
}
return ans;
};
rep(j, n - 1) {
int pos = (1 << 4) - 1;
int msk = (1 << 5) - 1;
rep(i, n) {
if (h[i][j] == h[i][j + 1]) {
pos &= msk ^ 1;
pos &= msk ^ (1 << 3);
continue;
}
if (h[i][j] == h[i][j + 1] - 1) {
pos &= msk ^ (1 << 2);
continue;
}
if (h[i][j] - 1 == h[i][j + 1]) {
pos &= msk ^ (1 << 1);
}
}
if (pos == 0) {
cout << "-1\n";
return;
}
if (pos & 1) {
addedge(j * 2, (j + 1) * 2, 0);
}
if (pos & 2) {
addedge(j * 2, (j + 1) * 2 + 1, b[j + 1]);
}
if (pos & 4) {
addedge(j * 2 + 1, (j + 1) * 2, 0);
}
if (pos & 8) {
addedge(j * 2 + 1, (j + 1) * 2 + 1, b[j + 1]);
}
}
ll ans = min(dfs(0), dfs(1) + b[0]);
if (ans >= OO) {
cout << "-1\n";
return;
}
graph.assign(n * 2, V<pair<int, ll>>());
fill(all(memo), -1);
rep(i, n - 1) {
int pos = (1 << 4) - 1;
int msk = (1 << 5) - 1;
rep(j, n) {
if (h[i][j] == h[i + 1][j]) {
pos &= msk ^ 1;
pos &= msk ^ (1 << 3);
continue;
}
if (h[i][j] == h[i + 1][j] - 1) {
pos &= msk ^ (1 << 2);
continue;
}
if (h[i][j] - 1 == h[i + 1][j]) {
pos &= msk ^ (1 << 1);
}
}
if (pos == 0) {
cout << "-1\n";
return;
}
if (pos & 1) {
addedge(i * 2, (i + 1) * 2, 0);
}
if (pos & 2) {
addedge(i * 2, (i + 1) * 2 + 1, a[i + 1]);
}
if (pos & 4) {
addedge(i * 2 + 1, (i + 1) * 2, 0);
}
if (pos & 8) {
addedge(i * 2 + 1, (i + 1) * 2 + 1, a[i + 1]);
}
}
ll tmp = min(dfs(0), dfs(1) + a[0]);
if (tmp >= OO) {
cout << "-1\n";
return;
}
cout << ans + tmp << '\n';
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}

View File

@@ -116,6 +116,15 @@ A little collection of interesting problems that I randomly decided to do.
Cool graph problem, not exactly difficult, but interesting to think about it. Cool graph problem, not exactly difficult, but interesting to think about it.
- [C. Wonderful City](https://codeforces.com/problemset/problem/2096/C)
Amazing graph/dp problem, actually interesting to think about it.
- [G2. Medium Demon Problem (hard version)](https://codeforces.com/problemset/problem/2044/G2)
A very simple problem once you finally understand it, interesting to learn about dfs
and all that.
## 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)