AtCoder Beginner Contest 135
AtCoder Beginner Contest 135
A - Harmony
#include <bits/stdc++.h>
using namespace std;
int main () {
int a, b;
cin >> a >> b;
if (a == b) cout << 0;
else {
int ans = (a + b) / 2;
if (a + b & 1) cout << "IMPOSSIBLE";
else if (ans < min (a, b) || ans > max (a, b)) cout << "IMPOSSIBLE";
else cout << ans;
}
}
B - 0 or 1 Swap
#include <bits/stdc++.h>
using namespace std;
const int N = 55;
int a[N], n, cnt;
int main () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
if (i != a[i]) cnt ++;
}
if (cnt == 2 || !cnt) cout << "YES";
else cout << "NO";
}
C - City Savers
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int a[N], n, ans;
signed main () {
cin >> n;
for (int i = 1; i <= n + 1; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
int x; cin >> x;
ans += min (x, a[i] + a[i+1]);
a[i+1] -= min (a[i+1], max(0ll, x - a[i]));
}
cout << ans;
}
D - Digits Parade
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
int f[N][13], n;
signed main () {
string s;
cin >> s;
n = s.size ();
s = ' ' + s;
f[0][0] = 1;
for (int i = 1; i <= n; i++) {
if (s[i] != '?') {
int x = s[i] - '0';
for (int j = 0; j < 13; j++) {
(f[i][(j * 10 + x) % 13] += f[i-1][j]) %= mod;
}
}
else {
for(int k = 0; k < 10; k++) {
for (int j = 0; j < 13; j++) {
(f[i][(j * 10 + k) % 13] += f[i-1][j]) %= mod;
}
}
}
}
cout << f[n][5] << endl;
}
E - Golf
难想
#include <bits/stdc++.h>
using namespace std;
int main () {
int k, n, fx, fy;
cin >> k >> fx >> fy;
int signx = 1, signy = 1;
if (fx < 0) signx = -1;
if (fy < 0) signy = -1; //转换到第一象限做
fx = abs (fx), fy = abs (fy);
if ((fx + fy & 1) && k % 2 == 0) {
cout << "-1\n";
return 0;
}
if (k == fx + fy) n = 1; //一步到达
else {
n = max(2, (fx + fy + k - 1) / k);
while ((n * k - fx - fy) & 1) n ++;
}
cout << n << endl;
int neg = (n * k - fx - fy) / 2, pos = (n * k + fx + fy) / 2;
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
if (neg > 0) { //先反向移动
if (neg >= k) {
if (fx - x <= fy - y) x -= k;
else y -= k;
neg -= k;
}
else {
if (fx - x <= fy - y) x -= neg, y += k - neg;
else y -= neg, x += k - neg;
neg = 0;
}
}
else {
if (x < fx) {
if (fx - x >= k) x += k;
else y += k - (fx - x), x = fx;
}
else y += k;
}
cout << x * signx << ' ' << y * signy << endl;
}
}
//fx + fy >= nk && (nk - (fx+fy)) % 2 == 0
F - Strings of Eternity
分类:
AtCoder