Sicily 1284. Tornado!

1284. Tornado!

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

Is this crazy weather the result of mankind's continuous interference in the environment? Or is it simply the normal cycle of climate changes through the ages? No one seems to know for sure, but the fact is that natural phenomena such as tornadoes and hurricanes have been hitting our country with more force and frequence than in past decades.

One tornado has just hit Silverado Farm, a cattle and milk producer, and made havoc. The barn roof was torn, several trees were uprooted, the farm truck was overturned... But the worst thing is that the tornado destroyed several sections of the fence that surrounded the property. The fence was very well built, with concrete posts every two meters, and barbed wire enclosing the whole farm perimeter (the perimeter, in meters, is an even number, making the fence perfectly regular).

Now several posts are broken or missing, and there are gaps in the fence. To prevent the cattle from getting out of the property, the fence must be restored as quickly as possible. Reconstructing the fence to its original form, with concrete posts, will take a long time. In the meantime, the farm owners decided to close the gaps with a temporary fence, made with wooden posts. Wooden posts will be placed in exactly the same spots where missing/broken concrete posts were/are. However, in order to make the temporary reconstruction faster and less expensive, the owners decided to use fewer posts: a wooden post will be used to replace a missing/broken concrete post only if the length of the barbed wired needed to close the distance to the next post (wooden or concrete) exceeds four meters.

 

\epsfbox{p3468.eps}

Given the description of which posts are missing/broken, you must write a program to determine the smallest number of wooden posts needed to close all the gaps in the fence, according to the owners' decision.

Input

The input contains several test cases. The first line of a test case contains one integer N indicating the number of original concrete posts in the fence (5<=N<=5000) . The second line of a test case contains Nintegers Xi indicating the state of each concrete post after the tornado(0<=Xi<=1for1<=i<=N) . If Xi = 1 post i is in good condition, if Xi = 0post i is broken or missing. Note that post N is next to post 1. The end of input is indicated by N = 0 .

Output

For each test case in the input your program must produce one line of output, containing an integer indicating the smallest number of wooden posts that are needed to restore the fence, according to the owners' decision.

Sample Input

10
1 0 0 1 0 0 1 0 1 1
11
1 0 0 1 0 0 0 1 1 0 1
12
0 0 0 0 0 1 1 0 0 0 1 1
0

Sample Output

2
2
3
读了好久的题目,原来就是插桩最优解,注意两个数字之间的距离是2米,首尾连接

感觉开个数组会好弄些,为了省内存就直接getchar了

#include <stdio.h>
int main() {
    int n;
    while (scanf("%d", &n) && n) {
        getchar();
        int head_zero = 0;
        int zero = 0;
        int need = 0;
        char temp;
        while (1) { //calculate the zero at head
            temp = getchar();
            if (temp == '1' || temp == '\n') {
                break;
            } else if (temp == '0') {
                head_zero++;
            }
        }
        if (head_zero == n) { //if all input num are zeros 
            printf("%d\n", (n + 1) / 2);
            continue;
        }
        while (1) {
            temp = getchar();
            if (temp == '1') {
                zero = 0;
            }
            if (temp == '0') {
                zero++;
            }
            if (zero == 2) {
                need++;
                zero = 0;
            }
            if (temp == '\n') {
                break;
            }
        }
        for (int i = 0; i < head_zero; i++) {//put the head_zero to tail to calculate
            zero++;
            if (zero == 2) {
                need++;
                zero = 0;
            }
        }
        printf("%d\n", need);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值