抢到 D 题首A,嘻嘻。
B 题70pts变 0pts,不嘻嘻。
A
挺简单的,答案是 1 的个数加联通块个数。
因为你需要跳到一个联通块的开头,然后在一个联通块里面跳联通块大小减一次,最后就需要跳回到目录,易证这是最小值。
#include <iostream>
using namespace std;
const int kMaxN = 1005;
int n, a[kMaxN];
int main() {
cin >> n;
int ans = 0, cnt = 0;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (cnt && a[i] == 0) {
ans += 1 + cnt, cnt = 0;
} else if (a[i] == 1) {
cnt++;
}
}
if (cnt) {
ans += cnt;
} else {
ans--;
}
cout << (ans <= 0 ? 0 : ans) << '\n';
return 0;
}B
错因是算出来的字符串错了。
char a = i + '0', b = j + '0';
c += m - n;
for (int i = 0; i < m; i++) {
char tmp = (i & 1 ? b : a); // 这里
if (tmp > r[i]) {
c--;
break;
} else if (tmp < r[i]) {
break;
}
}
for (int i = 0; i < n; i++) {
char tmp = (i & 1 ? b : a); // 这里
if (tmp < l[i]) { // here
// cout << i << ' ' << tmp << '\n';
break;
} else if (tmp > l[i]) { // 原本这两个地方的 l 打成 r 了
c++;
break;
} else if (i == n - 1) {
c++;
}
}其他的就如上面代码所示,下次需要考虑地更加详细。
C
其实我算 这个命题的时候算错了:(。
下次不能直接就交,而且最后写出来的正解是 0pts,但是暴力有 70pts。
正确的思路是下哦那个高位到低位考虑,然后贪心地选择每一位,尽量选择二进制为较大的,因为 。
最后写出来的代码:
#include <iostream>
using namespace std;
using ll = long long;
const ll kMaxN = 2e5 + 1;
ll n, l[kMaxN], r[kMaxN], now[kMaxN];
int main() {
cin >> n;
for (ll i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
}
ll ans = 0;
for (ll i = 61; i >= 0; i--) {
bool f = 1;
for (ll j = 1; j <= n; j++) {
ll L = now[j] + (1ll << i), R = now[j] + (2ll << i) - 1;
if (L > r[j] || R < l[j]) {
f = 0;
break;
}
}
if (f) {
ans += 1ll << i;
for (ll j = 1; j <= n; j++) {
now[j] += 1ll << i;
}
} else {
for (ll j = 1; j <= n; j++) {
ll L = now[j] + (1ll << i), R = now[j] + (2ll << i) - 1;
if (L <= r[j] && R >= l[j]) {
if (L - 1 < l[j]) {
now[j] += (1ll << i);
}
}
}
}
}
cout << ans << '\n';
return 0;
}D-CF478D Red-Green Towers
首先算出最高高度是 这个数上下浮动一下,在较大的时候 会偏大,需要减一。
然后把 看作 个物品做背包 DP。 为背包容量,设最后求出来的数组是 ,然后求 。
#include <assert.h>
#include <cmath>
#include <ctime>
#include <iostream>
using namespace std;
using ll = long long;
const ll kMaxN = 200005, kMod = 1e9 + 7;
ll r, g, f[kMaxN], a[kMaxN];
int main() {
cin >> r >> g;
ll s = sqrt((r + g) * 2);
if (s * (s + 1) / 2 > r + g) {
s--;
}
if (r > g) {
swap(r, g);
}
f[0] = 1;
for (int i = 1; i <= s; i++) {
for (int j = r; j >= i; j--) {
f[j] = (f[j] + f[j - i]) % kMod;
}
}
ll ans = 0;
for (int i = r; i + g >= s * (s + 1) / 2; i--) {
ans = (ans + f[i]) % kMod;
}
cout << ans << '\n';
return 0;
}