๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include<bits/stdc++.h>
using namespace std;
int memoize(int l, int r, int residue, const vector<char>& colors) {
int n = colors.size();
int dp[n][n][n];
memset(dp, -1, sizeof(dp));
if (r < l)
return 0;
else if (dp[l][r][residue] != -1)
return dp[l][r][residue];
dp[l][r][residue] = (residue + 1) * (residue + 1) + memoize(l, r - 1, 0, colors);
for (int i = l; i < r; i++) {
if (colors[i] == colors[r]) {
dp[l][r][residue] = max(dp[l][r][residue], memoize(l, i - 1, 0, colors) + memoize(i + 1, r, residue + 1, colors));
}
}
return dp[l][r][residue];
}
int solve(int n, vector<char> colors) {
return memoize(0, n - 1, 0, colors);
}
int main() {
int n;
cin >> n;
vector<char> colors(n);
for (int i = 0; i < n; i++) {
cin >> colors[i];
}
cout << solve(n, colors);
return 0;
}
Crayon Removal Scoring Game โ
using namespace std;
int memoize(int l, int r, int residue, const vector<char>& colors) {
int n = colors.size();
int dp[n][n][n];
memset(dp, -1, sizeof(dp));
if (r < l)
return 0;
else if (dp[l][r][residue] != -1)
return dp[l][r][residue];
dp[l][r][residue] = (residue + 1) * (residue + 1) + memoize(l, r - 1, 0, colors);
for (int i = l; i < r; i++) {
if (colors[i] == colors[r]) {
dp[l][r][residue] = max(dp[l][r][residue], memoize(l, i - 1, 0, colors) + memoize(i + 1, r, residue + 1, colors));
}
}
return dp[l][r][residue];
}
int solve(int n, vector<char> colors) {
return memoize(0, n - 1, 0, colors);
}
int main() {
int n;
cin >> n;
vector<char> colors(n);
for (int i = 0; i < n; i++) {
cin >> colors[i];
}
cout << solve(n, colors);
return 0;
}
Crayon Removal Scoring Game โ
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
if (y > x) {
cout << 2 << '\n';
} else if (y >= 2 && x - y >= 2) {
cout << 3 << '\n';
} else {
cout << -1 << '\n';
}
}
return 0;
}
CODEFORCES A
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
long long x, y;
cin >> x >> y;
if (y > x) {
cout << 2 << '\n';
} else if (y >= 2 && x - y >= 2) {
cout << 3 << '\n';
} else {
cout << -1 << '\n';
}
}
return 0;
}
CODEFORCES A
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
if(!(cin >> t)) return 0;
while (t--) {
int n;
cin >> n;
int m = 2 * n;
vector<int> a(m + 1, 0);
vector<char> used(m + 1, 0);
int cur = 1;
for (int x = n; x >= 1; --x) {
while (used[cur]) ++cur;
int i = cur;
int j = i + x;
while (j <= m && used[j]) j += x;
a[i] = a[j] = x;
used[i] = used[j] = 1;
}
for (int i = 1; i <= m; ++i) {
if (i > 1) cout << ' ';
cout << a[i];
}
cout << '\n';
}
return 0;
}
B
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
if(!(cin >> t)) return 0;
while (t--) {
int n;
cin >> n;
int m = 2 * n;
vector<int> a(m + 1, 0);
vector<char> used(m + 1, 0);
int cur = 1;
for (int x = n; x >= 1; --x) {
while (used[cur]) ++cur;
int i = cur;
int j = i + x;
while (j <= m && used[j]) j += x;
a[i] = a[j] = x;
used[i] = used[j] = 1;
}
for (int i = 1; i <= m; ++i) {
if (i > 1) cout << ' ';
cout << a[i];
}
cout << '\n';
}
return 0;
}
B