【HDOJ】1008 Elevator

这是一道水题,贴出来是因为等待电梯需要注意,比如楼层相同,2->2仍旧需要停,停的总时间就是n。但这显然不是正常的电梯(因为这种非正常状况wa),最开始我思考的时候是临近的楼层相同则不停,停的层数用wait表示,如下所示:

#include <stdio.h>

#define MAXNUM 1005

int floors[MAXNUM];

int main() {
    unsigned int n;
    unsigned int sum, wait, up, down;
    unsigned int i, j;

    while (scanf("%d", &n) != EOF && n) {
        for (i=0; i<n; ++i)
            scanf("%d", &floors[i]);

        i = 0;
        wait = 1;
        up = floors[0];
        down = 0;
        while (i<n-1) {
            j = i;
            while (j<n-1 && floors[j] <= floors[j+1]) {
                if (floors[j] != floors[j+1])
                    wait++;
                j++;
            }
            up += floors[j] - floors[i];

            i = j;
            while (i<n-1 && floors[i] >= floors[i+1]) {
                if (floors[i] != floors[i+1])
                    wait++;
                i++;
            }
            down += floors[j] - floors[i];
        }

        sum = up*6 + down*4 + n*5;
        printf("%d\n", sum);
    }

    return 0;
}

 

posted on 2014-03-06 17:03  Bombe  阅读(128)  评论(0编辑  收藏  举报

导航