|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目:
给出由X,Y构成的长度N的字符串S。选择S中位于不同位置的K字符,如果选择的字符是X,则改成Y,如果是Y,则改成X。
请问修改K次后的字符串中Y彼此相邻的地方最大为多少,也就是有多少 i, 满足 1 <= i < n 并且 Si = Si+1 以及 Si == 'Y'
本人代码:
- #include <bits/stdc++.h>
- using namespace std;
- int n, k;
- char s[200011];
- int ypos[500011], tot, dtot;
- struct D {
- int l, r, va;
- } d[500011];
- bool fd[500011];
- bool cmp(const D &a, const D &b) {
- if (b.l == 0 || b.r == n + 1) return 1;
- if (a.l == 0 || a.r == n + 1) return 0;
- return a.va < b.va;
- }
- int main() {
- scanf("%d%d%s", &n, &k, s + 1);
- //printf("\n%d", cmp({0, 2, 1}, {4, 7, 2}));
- //exit(0);
- if (k == 0) {
- for (int i = 1; i < n; ++i) {
- if (s[i] == s[i + 1] && s[i] == 'Y') ++tot;
- }
- printf("%d", tot);
- return 0;
- }
- for (int i = 1; i <= n; ++i) {
- if (s[i] == 'Y') {
- ypos[++tot] = i;
- }
- }
- if (tot == 0) {
- printf("%d", k - 1);
- return 0;
- }
- for (int i = 1; i < tot; ++i) {
- if (ypos[i + 1] - ypos[i] - 1 == 0) continue;
- ++dtot;
- d[dtot].l = ypos[i];
- d[dtot].r = ypos[i + 1];
- d[dtot].va = ypos[i + 1] - ypos[i] - 1;
- }
- if (ypos[1] != 0) {
- ++dtot;
- d[dtot].l = 0;
- d[dtot].r = ypos[1];
- d[dtot].va = ypos[1] - 1;
- }
- if (ypos[tot] != n) {
- ++dtot;
- d[dtot].l = ypos[tot];
- d[dtot].r = n + 1;
- d[dtot].va = n + 1 - ypos[tot] - 1;
- }
- sort(d + 1, d + dtot + 1, cmp);
- //for (int i = 1; i <= dtot; ++i) {
- // printf("%d %d %d\n", d[i].l, d[i].r, d[i].va);
- //}
- for (int i = 1; i <= dtot && k; ++i) {
- for (int j = d[i].l + 1; j < d[i].r && k; ++j) {
- //assert(s[j] == 'X');
- s[j] = 'Y';
- assert(fd[j] == 0);
- fd[j] = 1;
- --k;
- }
- }
- int res = 0;
- for (int i = 1; i < n; ++i) {
- if (s[i] == s[i + 1] && s[i] == 'Y') {
- ++res;
- }
- }
- // puts(s + 1);
- if (k == 0) {
- printf("%d", res);
- return 0;
- }
- if (fd[1] == 0) {
- --res;
- --k;
- }
- if (fd[n] == 0 && k) {
- --res;
- --k;
- }
- for (int i = 2; i < n && k; ++i) {
- if (!fd[i]) {
- fd[i] = 1;
- res -= 2;
- --k;
- }
- }
- printf("%d", res);
- return 0;
- }/*
- 10 5
- XYXYXXYYYX*/
复制代码
题目链接:https://atcoder.jp/contests/arc157/tasks/arc157_b
测评链接:https://atcoder.jp/contests/arc157/submissions/39197616
求助,我调了 70 分钟都没想出来,思路就是类似贪心,能最短的把一串 'Y' 连起来,就优先考虑,如果后面还有多余的 K,还是贪心尽量少减少答案
是不是那个细节没注意啊,求助各位,或者给一个 Hack 数据也可以,谢谢 |
|