洛谷P2882 [USACO07MAR]面对正确的方式Face The Right Way
1 #include <bits/stdc++.h> 2 #define For(i, j, k) for(int i=j; i<=k; i++) 3 #define Dow(i, j, k) for(int i=j; i>=k; i--) 4 #define LL long long 5 using namespace std; 6 inline int read() { 7 int x = 0, f = 1; 8 char ch = getchar(); 9 while(ch<'0'||ch>'9') { if(ch=='-') f = -1; ch = getchar(); } 10 while(ch>='0'&&ch<='9') { x = x*10+ch-48; ch = getchar(); } 11 return x * f; 12 } 13 void write(int x) { 14 if(x<0) putchar('-'), x = -x; 15 if(x>9) write(x/10); 16 putchar(x%10+48); 17 } 18 inline void writeln(int x) { write(x); putchar('\n'); } 19 20 const int N = 5011; 21 int n, ans, KK; 22 int a[N], f[N]; 23 24 int main() { 25 n = read(); 26 For(i, 1, n) { 27 char ch[5]; 28 scanf("%s", ch+1); 29 if(ch[1]=='B') a[i] = 1; 30 } 31 32 ans = 1e9; 33 For(K, 1, n) { 34 int t = 0, ok = 1, sum = 0; 35 For(i, 1, n) f[i] = 0; 36 For(i, 1, n-K+1) { 37 if( (a[i]+t)%2==1 ) { 38 ++f[i+K-1]; 39 ++t; 40 ++sum; 41 } 42 t -= f[i]; 43 f[i] = 0; 44 } 45 For(i, n-K+2, n) { 46 if( (a[i]+t)%2==1 ) { 47 ok = 0; 48 break; 49 } 50 t -= f[i]; 51 f[i] = 0; 52 } 53 if(ok && sum<ans) { 54 KK = K; 55 ans = sum; 56 } 57 } 58 printf("%d %d\n", KK, ans); 59 }