#include <cstdio> #include <cstring> int main(void) { int n, i, r, s, *a; char *c; scanf("%d\n", &n); c = new char[n+1]; a = new int[n+1]; memset(a, 0, (n + 1) * sizeof(*a)); r = 0; s = 0; fread(c, sizeof(*c), n, stdin); c[n] = '\0'; for (i = 0; i < n; i++) { if (c[i] == 'P') { if (r <= i) r = i + 1; while (c[r] == 'P') r++; if (r < n) { c[i] = 'L'; c[r] = 'P'; a[i]++; if (i + 1 < r) { a[i+1]++; a[r]--; } a[r+1]--; } } s += a[i]; printf("%d ", s); } printf("\n"); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <cstdio> #include <cstring> int main(void) { int n, i, r, s, *a; char *c; scanf("%d\n", &n); c = new char[n+1]; a = new int[n+1]; memset(a, 0, (n + 1) * sizeof(*a)); r = 0; s = 0; fread(c, sizeof(*c), n, stdin); c[n] = '\0'; for (i = 0; i < n; i++) { if (c[i] == 'P') { if (r <= i) r = i + 1; while (c[r] == 'P') r++; if (r < n) { c[i] = 'L'; c[r] = 'P'; a[i]++; if (i + 1 < r) { a[i+1]++; a[r]--; } a[r+1]--; } } s += a[i]; printf("%d ", s); } printf("\n"); return 0; } |