NOIP模擬(10.27)T1 壽司
阿新 • • 發佈:2019-02-08
壽司
題目背景:
10.27 NOIP模擬T1
分析:暴力
老子有句喵喵喵真的非常想講······
覺得今天T1最難的一定不是隻有我,T1程式碼最長的也一定不是隻有我,總之這個真的叫T1嗎,不過給我留了50分我還真是謝謝你了······
直接講題吧,考慮如果我們先不考慮成環的問題,那麼很顯然的我們需要將B給移到兩邊,或者R移到兩邊,我們這裡直接說R吧,顯然,這樣做的答案,應該是,每一個被移到左邊去的R左邊原來的B的個數,每一個被移到右邊去的R右邊原來的B的個數,那麼很顯然的,我們最好的方式是將,第(B的總個數 + 1) / 2個B(以下將第(B的總個數 + 1) / 2個B的位置簡稱為
Source
/* created by scarlyw */ #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <string> #include <cmath> #include <cctype> #include <queue> #include <vector> #include <ctime> const int MAXN = 2000000 + 10; const long long INF = 1000000000000000000; int t, len, n; int lb[MAXN], lr[MAXN], rb[MAXN], rr[MAXN]; char s[MAXN]; int main() { std::ios::sync_with_stdio(NULL); std::cin.tie(NULL), std::cout.tie(NULL); std::cin >> t; while (t--) { std::cin >> (s + 1), len = strlen(s + 1); long long fans1 = INF; for (int i = 1; i <= len; ++i) s[i + len] = s[i]; n = len, len *= 2; for (int i = 1; i <= len; ++i) { lb[i] = lb[i - 1], lr[i] = lr[i - 1]; if (s[i] == 'B') lb[i]++; else lr[i]++; } for (int i = len; i >= 1; --i) { rb[i] = rb[i + 1], rr[i] = rr[i + 1]; if (s[i] == 'B') rb[i]++; else rr[i]++; } long long ans1 = 0; int pos = (lb[n] + 1) / 2, p = -1; for (int i = 1; i <= n; ++i) { if (lb[i] == pos) { p = i; for (int j = n; j >= i; --j) if (s[j] == 'R') ans1 += (long long)(rb[j] - rb[n + 1]); break ; } if (s[i] == 'R') ans1 += (long long)lb[i]; } int head = 1, tail = n; fans1 = ans1; while (head <= n) { if (s[head] == 'B') { ans1 -= lr[p] - lr[head - 1]; ans1 += rr[p] - rr[++tail], ++head; while (s[++p] != 'B') { ans1 += (long long)(lb[p] - lb[head - 1]); ans1 -= (long long)(rb[p] - rb[tail + 1]); } } else head++, tail++; fans1 = std::min(fans1, ans1); } std::cout << fans1 << '\n'; } return 0; }