POJ 3276 (水题)
2016-02-17 14:31
351 查看
Face The Right Way
Farmer John has arranged his N (1 ≤ N ≤ 5,000) cows in a row and many of them are facing forward, like good cows. Some of them are facing backward, though, and he needs them all to face forward to make his life perfect. Fortunately, FJ recently bought an automatic cow turning machine. Since he purchased the discount model, it must be irrevocably preset to turn K (1 ≤ K ≤ N) cows at once, and it can only turn cows that are all standing next to each other in line. Each time the machine is used, it reverses the facing direction of a contiguous group of K cows in the line (one cannot use it on fewer than K cows, e.g., at the either end of the line of cows). Each cow remains in the same *location* as before, but ends up facing the *opposite direction*. A cow that starts out facing forward will be turned backward by the machine and vice-versa. Because FJ must pick a single, never-changing value of K, please help him determine the minimum value of K that minimizes the number of operations required by the machine to make all the cows face forward. Also determine M, the minimum number of machine operations required to get all the cows facing forward using that value of K. Input Line 1: A single integer: N Lines 2..N+1: Line i+1 contains a single character, F or B, indicating whether cow i is facing forward or backward. Output Line 1: Two space-separated integers: K and M Sample Input 7 B B F B F B B Sample Output 3 3 Hint For K = 3, the machine must be operated three times: turn cows (1,2,3), (3,4,5), and finally (5,6,7) Source USACO 2007 March Gold |
如果用g[i]表示第i次操作有没有翻转,那么g[i-k+1]+g[i-k+2]+...+g[i]模2就是i位置最终有没有翻转。
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <time.h>
#include <vector>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
#define maxn 51111
char a[maxn];
int f[maxn], g[maxn];
int n;
int ok (int k) {
memset (g, 0, sizeof g);
for (int i = 1; i <= n; i++) {
f[i] = (a[i] == 'B');
}
int cnt = 0, sum = 0;
for (int i = 1; i <= n-k+1; i++) {
if (i > k) {
sum -= g[i-k];
}
if (sum%2 != f[i]) {
cnt++;
sum++;
g[i] = 1;
}
}
for (int i = n-k+2; i <= n; i++) {
sum -= g[i-k];
if (sum%2 != f[i])
return 11111111;
}
return cnt;
}
int main () {
while (scanf ("%d", &n) == 1) {
getchar ();
for (int i = 1; i <= n; i++) {
a[i] = getchar ();
getchar ();
}
int ans = 11111111, len;
for (int i = 1; i <= n; i++) {
int cur = ok (i);
if (cur < ans) {
ans = cur;
len = i;
}
}
printf ("%d %d\n", len, ans);
}
return 0;
}
相关文章推荐
- iOS开发之Objective-C与JavaScript交互操作
- Android studio 导入项目报 Error:Cause: peer not authenticated 异常
- Sqlserver查询数据库文件大小和剩余空间
- 递归
- bash检查文件格式
- Servlet 获取多个参数
- CodeIgniter类的使用
- DEV 皮肤子多样化定义
- LeetCode Happy Number
- 数据库范式(通俗易懂)
- Android重写EditText回车等事件
- 大话设计模式之解释器模式——行为型
- Android Studio so文件如何添加,如何解决极光推送无法推送的奇怪问题
- GCD定时器
- 推荐的优秀博客--多关注,多看看
- Java Web应用调优线程池:没你想的那么复杂
- Redis 未授权访问缺陷可轻易导致系统被黑
- 使用gitlab上传PubKey时报错500
- [转]MySQL 5.6 my.cnf配置优化
- 关于带系统导航栏坐标的问题