zoukankan      html  css  js  c++  java
  • POJ 3276

    Face The Right Way
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 2193   Accepted: 1039

    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

    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

     
    反转法解
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 
     8 #define MAX_N 5005
     9 
    10 int N;
    11 int dir[MAX_N],f[MAX_N];
    12 
    13 int cal(int K) {
    14         memset(f,0,sizeof(f));
    15         int sum = 0;
    16         int res = 0;
    17         for(int i = 1; i + K - 1 <= N; ++i) {
    18                 if((dir[i] + sum) % 2) {
    19                         ++res;
    20                         f[i] = 1;
    21                 }
    22                 sum += f[i];
    23                 if(i - K + 1 >= 1) {
    24                         sum -= f[i - K + 1];
    25                 }
    26         }
    27 
    28         for(int i = N - K + 2; i <= N; ++i) {
    29                 if((dir[i] + sum) % 2) {
    30                         //printf(" i = %d sum = %d dir = %d
    ",i,sum,dir[i]);
    31                         return -1;
    32                 }
    33                 if(i - K + 1 >= 1) sum -= f[i - K + 1];
    34         }
    35 
    36         return res;
    37 }
    38 
    39 void solve() {
    40         int ansm,ansk,t;
    41         for(int k = 1; k <= N; ++k) {
    42                 //printf("cal = %d
    ",cal(k));
    43                 if((t = cal(k)) >= 0 && t < ansm) {
    44                         ansm = t;
    45                         ansk = k;
    46                 }
    47         }
    48 
    49         printf("%d %d
    ",ansk,ansm);
    50 }
    51 int main()
    52 {
    53    // freopen("sw.in","r",stdin);
    54     scanf("%d",&N);
    55     for(int i = 1; i <= N; ++i) {
    56             char ch[5];
    57             scanf("%s",&ch);
    58             if(ch[0] == 'F') dir[i] = 0;
    59             else dir[i] = 1;
    60             //printf("%c",ch);
    61     }
    62 
    63     solve();
    64 
    65     return 0;
    66 }
    View Code
  • 相关阅读:
    如何调用ActiveX网页中的JavaScript函数
    VC++开发的ActiveX如何加入安全机制,避免IE中提示“在此页上的ActiveX控件和本页上的其他部分的交互可能不安全,你想允许这种交互吗?”
    ActiveX多线程回调JavaScript
    JavaScript脚本如何访问VC++开发的ActiveX中的方法
    ActiveX异步回调JavaScript
    com/atl套间编程中如何实现定时invoke容器中的方法
    VC++开发的ActiveX如何通过JavaScript脚本和EOS应用交互
    thread wrapper
    最新Mysql中文帮助
    如何避免重复的http请求
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3632427.html
Copyright © 2011-2022 走看看