Submission #63898263
Source Code Expand
Copy
#include <stdio.h>#include <inttypes.h>int N;char S[512345];uint64_t left_scores[512345];int main(void) {uint64_t ans = UINT64_MAX, cur = 0;int i, cnt;if (scanf("%d", &N) != 1) return 1;if (scanf("%512344s", S) != 1) return 1;for (i = 0, cnt = 0; i < N; i++) {if (S[i] == '1') {cur += i - cnt;cnt++;}}for (i = 0; i < N; i++) {left_scores[i] = cur;
#include <stdio.h>
#include <inttypes.h>
int N;
char S[512345];
uint64_t left_scores[512345];
int main(void) {
uint64_t ans = UINT64_MAX, cur = 0;
int i, cnt;
if (scanf("%d", &N) != 1) return 1;
if (scanf("%512344s", S) != 1) return 1;
for (i = 0, cnt = 0; i < N; i++) {
if (S[i] == '1') {
cur += i - cnt;
cnt++;
}
}
for (i = 0; i < N; i++) {
left_scores[i] = cur;
if (S[i] == '1') {
/* 切り離す */
cnt--;
} else {
/* 右に移動する */
cur -= cnt;
}
}
cur = 0;
for (i = N - 1, cnt = 0; i >= 0; i--) {
if (S[i] == '1') {
cur += (N - 1 - cnt) - i;
cnt++;
}
}
for (i = N - 1; i >= 0; i--) {
if (S[i] == '1') {
/* 切り離す */
cnt--;
/* 評価する */
if (cur + left_scores[i] < ans) ans = cur + left_scores[i];
} else {
/* 左に移動する */
cur -= cnt;
}
}
printf("%" PRIu64 "\n", ans == UINT64_MAX ? 0 : ans);
return 0;
}
/*
まず、1 を全部左に寄せる
その後、右に移動していく
ただし、1 の本来の位置が左端に来たら、その1 をそこに残す
右からも同様にやる → 左右を合わせたのがそこを中心としたときのスコア
半端なところに置くより、既存の 1 どれかを中心にしたほうが不利にならないだろ (予想)
*/
Submission Info
| Submission Time | |
|---|---|
| Task | D - Swap to Gather |
| User | mikecat |
| Language | C (gcc 12.2.0) |
| Score | 425 |
| Code Size | 1370 Byte |
| Status | AC |
| Exec Time | 12 ms |
| Memory | 6128 KB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 425 / 425 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 02_random2_05.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 0 ms | 1644 KB |
| 00_sample_01.txt | AC | 0 ms | 1568 KB |
| 00_sample_02.txt | AC | 0 ms | 1636 KB |
| 01_random_00.txt | AC | 2 ms | 2244 KB |
| 01_random_01.txt | AC | 10 ms | 5836 KB |
| 01_random_02.txt | AC | 8 ms | 4496 KB |
| 01_random_03.txt | AC | 4 ms | 5956 KB |
| 01_random_04.txt | AC | 6 ms | 6104 KB |
| 01_random_05.txt | AC | 7 ms | 5988 KB |
| 01_random_06.txt | AC | 9 ms | 6100 KB |
| 01_random_07.txt | AC | 11 ms | 6108 KB |
| 01_random_08.txt | AC | 12 ms | 5988 KB |
| 01_random_09.txt | AC | 11 ms | 6128 KB |
| 01_random_10.txt | AC | 9 ms | 6016 KB |
| 01_random_11.txt | AC | 7 ms | 5988 KB |
| 01_random_12.txt | AC | 6 ms | 6000 KB |
| 01_random_13.txt | AC | 4 ms | 5996 KB |
| 02_random2_00.txt | AC | 4 ms | 5988 KB |
| 02_random2_01.txt | AC | 4 ms | 6100 KB |
| 02_random2_02.txt | AC | 4 ms | 6128 KB |
| 02_random2_03.txt | AC | 4 ms | 5960 KB |
| 02_random2_04.txt | AC | 4 ms | 6032 KB |
| 02_random2_05.txt | AC | 4 ms | 6000 KB |
| 03_handmade_00.txt | AC | 0 ms | 1716 KB |
| 03_handmade_01.txt | AC | 0 ms | 1568 KB |
| 03_handmade_02.txt | AC | 0 ms | 1608 KB |
| 03_handmade_03.txt | AC | 0 ms | 1644 KB |
| 03_handmade_04.txt | AC | 4 ms | 6000 KB |