poj3276 Face The Right Way
Posted 王宜鸣
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj3276 Face The Right Way相关的知识,希望对你有一定的参考价值。
思路:
枚举。前缀和优化。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int INF = 0x3f3f3f3f; 6 char a[5005]; 7 int f[15555], sum[15555], n; 8 int solve(int k) 9 { 10 memset(f, 0, sizeof f); 11 memset(sum, 0, sizeof sum); 12 for (int i = 5005; i <= 5005 + n - k; i++) 13 { 14 int tmp = sum[i - 1] - sum[i - k]; 15 if ((tmp % 2 == 0 && a[i - 5005] == ‘B‘) || (tmp % 2 == 1 && a[i - 5005] == ‘F‘)) 16 { 17 f[i] = 1; 18 } 19 sum[i] = sum[i - 1] + f[i]; 20 } 21 for (int i = 5006 + n - k; i < 5005 + n; i++) 22 { 23 sum[i] = sum[i - 1] + f[i]; 24 int tmp = sum[i - 1] - sum[i - k]; 25 if ((tmp % 2 == 0 && a[i - 5005] == ‘B‘) || (tmp % 2 == 1 && a[i - 5005] == ‘F‘)) 26 { 27 return -1; 28 } 29 } 30 return sum[5005 + n - k]; 31 } 32 int main() 33 { 34 cin >> n; 35 for (int i = 0; i < n; i++) 36 { 37 cin >> a[i]; 38 } 39 int minn = INF, res = 0; 40 for (int k = 1; k <= n; k++) 41 { 42 int tmp = solve(k); 43 if (tmp >= 0 && tmp < minn) { minn = tmp; res = k; } 44 } 45 cout << res << " " << minn << endl; 46 return 0; 47 }
以上是关于poj3276 Face The Right Way的主要内容,如果未能解决你的问题,请参考以下文章
POJ3276 Face The Right Way (尺取法)