Codeforces Round #342 (Div. 2)

时间:2021-02-16 13:08:45

贪心 A - Guest From the Past

先买塑料和先买玻璃两者取最大值

#include <bits/stdc++.h>

typedef long long ll;

int main(void)  {
ll n, a, b, c; std::cin >> n >> a >> b >> c;
ll ans = 0;
if (n >= a) {
ll cnt1 = n / a;
ll res = n % a;
if (res >= b) {
cnt1 += (res - b) / (b - c) + 1;
}
ans = std::max (ans, cnt1);
}
if (n >= b) {
ll cnt2 = (n - b) / (b - c) + 1;
cnt2 += (n - cnt2 * (b - c)) / a;
ans = std::max (ans, cnt2);
}
std::cout << ans << '\n'; return 0;
}

暴力 B - War of the Corporations

#include <bits/stdc++.h>

const int N = 1e5 + 5;
char str1[N], str2[33]; int main(void) {
scanf ("%s%s", &str1, &str2);
int len1 = strlen (str1);
int len2 = strlen (str2);
int ans = 0;
for (int i=0; i<len1; ++i) {
if (str1[i] == str2[0]) {
bool flag = true;
for (int ii=i+1, j=1; j<len2; ++ii, ++j) {
if (str1[ii] != str2[j]) {
flag = false; break;
}
}
if (flag) {
ans++; i += len2 - 1;
}
}
}
printf ("%d\n", ans); return 0;
}

构造 C - K-special Tables

#include <bits/stdc++.h>

int a[502][502];

int main(void)  {
int n, k; std::cin >> n >> k;
int now = n * n;
int mx = 0;
for (int i=1; i<=n; ++i) {
for (int j=n; j>=k; --j) {
a[i][j] = now--;
if (j == k) mx += a[i][j];
}
}
for (int i=1; i<=n; ++i) {
for (int j=k-1; j>=1; --j) {
a[i][j] = now--;
}
}
std::cout << mx << '\n';
for (int i=1; i<=n; ++i) {
for (int j=1; j<=n; ++j) {
std::cout << a[i][j] << ' ';
}
std::cout << '\n';
} return 0;
}

构造 D - Finals in arithmetic

题意:一个数字a + 反过来的ar == n (<=10^100000),已知n,求a

分析:完全看别人代码看懂的。不考虑进位的话,那么n应该是是回文的。那么处理成不进位的,每一位0~18。

#include <bits/stdc++.h>

const int N = 1e5 + 5;
char str[N];
char ans[N];
int s[N];
int n; bool judge(void) {
for (int i=0; i<n/2;) {
int l = i, r = n - 1 - i;
if (s[l] == s[r]) ++i;
else if (s[l] == s[r] + 1 || s[l] == s[r] + 11) {
s[l]--; s[l+1] += 10;
}
else if (s[l] == s[r] + 10) {
s[r] += 10; s[r-1]--;
}
else return false;
}
if (n % 2 == 1) {
if (s[n/2] % 2 == 1 || s[n/2] > 18 || s[n/2] < 0) return false;
ans[n/2] = s[n/2] / 2 + '0';
}
for (int i=0; i<n/2; ++i) {
if (s[i] > 18 || s[i] < 0) return false;
ans[i] = (s[i] + 1) / 2 + '0';
ans[n-1-i] = s[i] / 2 + '0';
}
return ans[0] > '0';
} int main(void) {
scanf ("%s", str);
n = strlen (str);
for (int i=0; i<n; ++i) s[i] = str[i] - '0';
if (judge ()) printf ("%s\n", ans);
else if (str[0] == '1' && n > 1) {
for (int i=0; i<n; ++i) {
s[i] = str[i+1] - '0';
}
s[0] += 10; n--;
if (judge ()) printf ("%s\n", ans);
else puts ("0");
}
else puts ("0"); return 0;
}