提出 #39197616
ソースコード 拡げる
#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*/
提出情報
コンパイルエラー
./Main.cpp: In function ‘int main()’:
./Main.cpp:19:7: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
19 | scanf("%d%d%s", &n, &k, s + 1);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
ジャッジ結果
| セット名 | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 0 / 500 | ||||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | 00-sample-001.txt, 00-sample-002.txt |
| All | 00-sample-001.txt, 00-sample-002.txt, 01-001.txt, 01-002.txt, 01-003.txt, 01-004.txt, 01-005.txt, 01-006.txt, 01-007.txt, 01-008.txt, 01-009.txt, 01-010.txt, 01-011.txt, 01-012.txt, 01-013.txt, 01-014.txt, 01-015.txt, 01-016.txt, 01-017.txt, 01-018.txt, 01-019.txt, 01-020.txt, 01-021.txt, 01-022.txt, 01-023.txt, 01-024.txt, 01-025.txt, 01-026.txt, 01-027.txt, 01-028.txt, 01-029.txt, 01-030.txt, 01-031.txt, 01-032.txt, 01-033.txt, 01-034.txt, 01-035.txt, 01-036.txt, 01-037.txt, 01-038.txt, 01-039.txt, 01-040.txt, 01-041.txt, 01-042.txt, 01-043.txt, 01-044.txt, 01-045.txt, 01-046.txt, 01-047.txt, 01-048.txt, 01-049.txt, 01-050.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| 00-sample-001.txt | AC | 4 ms | 3660 KiB |
| 00-sample-002.txt | AC | 2 ms | 3616 KiB |
| 01-001.txt | AC | 2 ms | 3524 KiB |
| 01-002.txt | AC | 3 ms | 3588 KiB |
| 01-003.txt | WA | 2 ms | 3648 KiB |
| 01-004.txt | AC | 3 ms | 3560 KiB |
| 01-005.txt | AC | 2 ms | 3652 KiB |
| 01-006.txt | AC | 2 ms | 3512 KiB |
| 01-007.txt | AC | 3 ms | 3664 KiB |
| 01-008.txt | AC | 2 ms | 3560 KiB |
| 01-009.txt | AC | 2 ms | 3568 KiB |
| 01-010.txt | AC | 2 ms | 3512 KiB |
| 01-011.txt | AC | 15 ms | 4820 KiB |
| 01-012.txt | WA | 12 ms | 4932 KiB |
| 01-013.txt | WA | 12 ms | 5104 KiB |
| 01-014.txt | AC | 7 ms | 4688 KiB |
| 01-015.txt | WA | 11 ms | 4872 KiB |
| 01-016.txt | WA | 7 ms | 4876 KiB |
| 01-017.txt | AC | 7 ms | 4120 KiB |
| 01-018.txt | AC | 10 ms | 4284 KiB |
| 01-019.txt | WA | 9 ms | 4192 KiB |
| 01-020.txt | AC | 7 ms | 4728 KiB |
| 01-021.txt | WA | 8 ms | 4764 KiB |
| 01-022.txt | WA | 6 ms | 4740 KiB |
| 01-023.txt | AC | 4 ms | 3740 KiB |
| 01-024.txt | AC | 5 ms | 4040 KiB |
| 01-025.txt | AC | 4 ms | 3948 KiB |
| 01-026.txt | AC | 16 ms | 4900 KiB |
| 01-027.txt | WA | 13 ms | 5004 KiB |
| 01-028.txt | WA | 13 ms | 4928 KiB |
| 01-029.txt | AC | 13 ms | 5284 KiB |
| 01-030.txt | AC | 13 ms | 5384 KiB |
| 01-031.txt | WA | 16 ms | 5428 KiB |
| 01-032.txt | AC | 6 ms | 4464 KiB |
| 01-033.txt | WA | 7 ms | 4460 KiB |
| 01-034.txt | WA | 7 ms | 4412 KiB |
| 01-035.txt | AC | 13 ms | 5312 KiB |
| 01-036.txt | AC | 14 ms | 5672 KiB |
| 01-037.txt | WA | 14 ms | 5608 KiB |
| 01-038.txt | AC | 5 ms | 4328 KiB |
| 01-039.txt | WA | 5 ms | 4364 KiB |
| 01-040.txt | WA | 5 ms | 4376 KiB |
| 01-041.txt | AC | 4 ms | 3748 KiB |
| 01-042.txt | AC | 3 ms | 3712 KiB |
| 01-043.txt | AC | 6 ms | 4636 KiB |
| 01-044.txt | AC | 4 ms | 3716 KiB |
| 01-045.txt | WA | 8 ms | 4768 KiB |
| 01-046.txt | AC | 4 ms | 3784 KiB |
| 01-047.txt | WA | 5 ms | 4812 KiB |
| 01-048.txt | AC | 5 ms | 3780 KiB |
| 01-049.txt | AC | 2 ms | 3576 KiB |
| 01-050.txt | AC | 2 ms | 3536 KiB |