Skip to content

Commit

Permalink
Codeforces: EPIC Institute of Technology Round Summer 2024 (Div. 1 + …
Browse files Browse the repository at this point in the history
…Div. 2)
  • Loading branch information
Macesuted committed Jul 1, 2024
1 parent 6ec2f2b commit e7b44f0
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Codeforces/1987A.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @file 1987A.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

void solve(void) {
int n, k;
cin >> n >> k;
cout << (n - 1) * k + 1 << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
53 changes: 53 additions & 0 deletions Codeforces/1987B.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @file 1987B.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 100005

int a[maxn];

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
int64_t tot = 0, maxv = 0;
for (int i = 2; i <= n; i++) {
if (a[i] >= a[i - 1]) continue;
int64_t delt = a[i - 1] - a[i];
tot += delt, maxv = max(maxv, delt), a[i] = a[i - 1];
}
cout << tot + maxv << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
50 changes: 50 additions & 0 deletions Codeforces/1987C.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @file 1987C.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 100005

int a[maxn];

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = n - 1; i; i--) {
a[i] = max(a[i], a[i + 1] + 1);
}
cout << a[1] << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
62 changes: 62 additions & 0 deletions Codeforces/1987D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @file 1987D.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 5005

int a[maxn], cnt[maxn], f[maxn][maxn];

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cnt[i] = 0;
for (int i = 1; i <= n; i++) cin >> a[i], cnt[a[i]]++;

for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= n + 1; j++) f[i][j] = 1e9;

f[1][0] = 0;
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= n; j++)
if (f[i][j] != (int)1e9) {
if (cnt[i]) f[i + 1][j + 1] = min(f[i + 1][j + 1], f[i][j] + 1);
if (j >= cnt[i]) f[i + 1][j - cnt[i]] = min(f[i + 1][j - cnt[i]], f[i][j]);
}
}
int ans = INT_MAX;
for (int j = 0; j <= n + 1; j++) ans = min(ans, f[n + 1][j]);
cout << ans << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
88 changes: 88 additions & 0 deletions Codeforces/1987E.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @file 1987E.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 5005

int fa[maxn], siz[maxn], lson[maxn], dep[maxn];
int64_t a[maxn], rest[maxn];
vector<int> son[maxn];
multimap<int, int>* choi[maxn];
int64_t ans;

void dfs(int p) {
siz[p] = 1;
if (son[p].empty()) {
choi[p] = new multimap<int, int>();
choi[p]->emplace(dep[p], p);
rest[p] = INT64_MAX;
return;
}
int64_t sum = lson[p] = 0;
for (auto i : son[p]) {
dep[i] = dep[p] + 1, dfs(i), siz[p] += siz[i], sum += a[i];
if (!lson[p] || siz[lson[p]] < siz[i]) lson[p] = i;
}
choi[p] = choi[lson[p]];
for (auto i : son[p]) {
if (i == lson[p]) continue;
for (auto j : *choi[i]) choi[p]->emplace(j.first, j.second);
delete choi[i];
}
while (a[p] > sum) {
int q = choi[p]->begin()->second;
int64_t delt = min(a[p] - sum, rest[q]);
sum += delt, rest[q] -= delt, ans += delt * (dep[q] - dep[p]);
if (!rest[q]) choi[p]->erase(choi[p]->begin());
}
rest[p] = sum - a[p];
if (rest[p]) choi[p]->emplace(dep[p], p);
return;
}

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];

for (int i = 1; i <= n; i++) son[i].clear();
for (int i = 2; i <= n; i++) cin >> fa[i], son[fa[i]].push_back(i);
ans = dep[1] = 0;
dfs(1);
delete choi[1];

cout << ans << endl;
return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
72 changes: 72 additions & 0 deletions Codeforces/1987F1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @file 1987F1.cpp
* @author Macesuted (i@macesuted.moe)
* @date 2024-06-30
*
* @copyright Copyright (c) 2024
*
*/

#include <bits/stdc++.h>
using namespace std;

#ifndef LOCAL
#define endl '\n'
#endif

bool mem1;

#define maxn 805

int a[maxn], f[maxn][maxn], g[maxn][maxn];

void solve(void) {
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];

for (int i = 0; i <= n + 1; i++)
for (int j = 0; j <= i; j++) f[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = i; j <= n; j++) g[i][j] = INT_MAX;

for (int len = 2; len <= n; len += 2)
for (int l = 1, r = len; r <= n; l++, r++)
if (a[l] <= l && (l - a[l]) % 2 == 0) {
int t = (l - a[l]) / 2;
for (int p = l + 1; p <= r; p += 2)
if (g[l + 1][p - 1] <= t) g[l][r] = min(g[l][r], max({t, g[p + 1][r] - (p - l + 1) / 2}));
}

for (int i = n; i; i--)
for (int j = i; j >= 1; j -= 2) {
f[i][j] = f[i + 1][j + 1];
if (j <= a[i] && a[i] <= i && (i - a[i]) % 2 == 0) {
int t = (i - a[i]) / 2;
for (int p = i + 1; p <= n; p += 2)
if (g[i + 1][p - 1] <= t) f[i][j] = max(f[i][j], f[p + 1][j] + (p - i + 1) / 2);
}
}

cout << f[1][1] << endl;

return;
}

bool mem2;

int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif

int _ = 1;
cin >> _;
while (_--) solve();

#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}
Loading

0 comments on commit e7b44f0

Please sign in to comment.