0
点赞
收藏
分享

微信扫一扫

AcWing第19场周赛题解

豆丁趣 2022-06-10 阅读 70

A. 3991. 满足条件的01串

题目链接:​​https://www.acwing.com/problem/content/3994/​​

题目大意:略。

解题思路:简单模拟一遍即可。

示例程序:

#include <bits/stdc++.h>
using namespace std;
int T, n;
char s[1010];

bool check() {
for (int i = 2; s[i]; i++)
if (s[i-1] == '1' && s[i] == '1')
return false;
for (int i = 1; s[i]; i++)
if (s[i] == '0' && !(s[i-1] == '1' || s[i+1] == '1'))
return false;
return true;
}

int main() {
cin >> T;
while (T--) {
cin >> n >> s+1;
puts(check() ? "Yes" : "No");
}
return 0;
}

B. 3992. 树上有猴

题目链接:​​https://www.acwing.com/problem/content/3995/​​

题目大意:略。

解题思路:设初始数量为 0,求最大值和最小值,根据偏移求结果。

示例程序:

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

int n, w, a, tmp, mx, mi;

int main() {
cin >> n >> w;
while (n--) {
cin >> a;
tmp += a;
mx = max(mx, tmp);
mi = min(mi, tmp);
}
cout << max(0, w - (mx - mi) + 1) << endl;
return 0;
}

C. 3993. 石子游戏

题目链接:​​https://www.acwing.com/problem/content/3996/​​

题目大意:

解题思路:桶,贪心。因为数据范围是 \(2 \times 10^5\),所以可以开一个同记录一下每个数值出现地次数,然后贪心去消。计数需要对差分前缀和。

示例程序:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;

int n, k, h, a, cnt[maxn];

int main() {
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> a;
h = max(h, a);
cnt[a]++;
}
for (int i = h-1; i >= 1; i--)
cnt[i] += cnt[i+1];
assert(cnt[1] == n);
int tmp = 0, cc = 0;
while (cnt[h] < n) {
if (tmp < cnt[h]) {
cc++;
tmp = k; // 这里一开始写成 += 了但其实是 =
}
tmp -= cnt[h--];
}
cout << cc << endl;
return 0;
}


举报

相关推荐

0 条评论