POJ 3276 尺取法 反转问题
2016-02-10 15:06
363 查看
Face The Right Way
Description
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
Sample Output
题目大意:
n头牛,排成一排,有些头朝前,有些头朝后。问,要把所有牛的头都弄成朝前,最小需要几步,每次最少需要几头牛调换头的位置。
遇到的问题和思路:(挑战程序设计的150页的例题)
从第一个牛开始,如果第一个牛是B,那就需要反转,如果是F,就不需要反转.且如果这头牛的朝向是正确的,就不需要再管它了。推广以后就是,每K头牛中,每次对最前面的这头牛进行操作,操作以后,就再也不需要管理这头牛了。
注意,如果对前面k头牛操作到最后的n-k头了,这个时候就不需要再交换了。但是别忘了要判断最后k-1头牛是否头的方向是正确的。
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 3537 | Accepted: 1638 |
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
题目大意:
n头牛,排成一排,有些头朝前,有些头朝后。问,要把所有牛的头都弄成朝前,最小需要几步,每次最少需要几头牛调换头的位置。
遇到的问题和思路:(挑战程序设计的150页的例题)
从第一个牛开始,如果第一个牛是B,那就需要反转,如果是F,就不需要反转.且如果这头牛的朝向是正确的,就不需要再管它了。推广以后就是,每K头牛中,每次对最前面的这头牛进行操作,操作以后,就再也不需要管理这头牛了。
注意,如果对前面k头牛操作到最后的n-k头了,这个时候就不需要再交换了。但是别忘了要判断最后k-1头牛是否头的方向是正确的。
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; int n; int a[5000 + 50]; int work(int k){ int sum = 0, res = 0; int flag[5000 + 50]; memset(flag, 0, sizeof(flag)); for (int i = 0; i + k <= n; i++){ if ((a[i] + sum) % 2 != 0){//如果a[i]是1,那么前面操作了偶数次,就还需要再操作一次。反之a[i]是0,前面操作了奇数次,就要在操作一次 flag[i] = 1; res++;//记录操作的次数 } sum += flag[i];//因为是k个范围内的,所以超出了这个范围就要重新划定sum if (i - k + 1 >= 0){ sum -= flag[i - k + 1]; } } //检查后面的牛还有没有朝后面的情况 for (int i = n - k + 1; i < n; i++){ if ((a[i] + sum) % 2 == 1){ return -1; } if (i - k + 1 >= 0) sum -= flag[i - k + 1]; } return res; } void solve(){ int K = 1, M = n; for (int k = 1; k <= n; k++){ int m = work(k); if (m == -1)continue; else if (M > m){ K = k; M = m; } } printf("%d %d\n", K, M); } int main(){ while(scanf("%d", &n) != EOF){ memset(a, 0, sizeof(a)); char ch[2]; for (int i = 0; i < n; i++){ scanf("%s", ch); if (ch[0] == 'B') a[i] = 1; else a[i] = 0;//0是forward,1是backword } /*for (int i = 0; i < n; i++)printf(" %d ", a[i]); printf("\n");*/ solve(); } return 0; } </cstring></algorithm></cstdio>
相关文章推荐
- DS(数据结构)复习
- c#之多线程基本概念
- windows设置临时环境变量path
- [bzoj1031][JSOI2007]字符加密Cipher
- Light OJ 1084 Winter (线段树+DP)
- JavaScrip 笔记 2
- BroadcastReceiver的register与unregister的timing问题
- [bzoj1030][JSOI2007]文本生成器
- 尼姆博弈+威佐夫博奕
- mysql获取上一步INSERT操作产生的ID
- cvMatchTemplate() 模板匹配
- USACO 1.5 sprime
- 在Linux信号处理中longjmp和siglongjmp
- [从头学数学] 第80节 数学广角──植树问题
- 海明码的生成
- java中static内存图解
- Android设计模式-单例模式
- 2016蓝桥杯算法训练——矩阵乘法
- Android 学习(2)
- Attaching the Filter Device Object to the Target Device Object