24h購物| | PChome| 登入
2013-06-11 12:42:00| 人氣910| 回應0 | 上一篇 | 下一篇

[UVA] 11683 - Laser Sculpture

推薦 0 收藏 0 轉貼0 訂閱站台


  Laser Sculpture 

Since its invention, in 1958, the laser has been used in a huge variety of applications, like electronic equipment, cirurgical instruments, weapons, and much more.

epsfbox{p11683a.eps}

The image above shows a diagram of an equipment to sculpt, with a laser, a block of some solid material. In the figure we can see a laser emitter that moves horizontally to the right and left with a constant speed. When the emitter is turned on when moving, a layer a specific width is removed from the block, being vaporized by the laser.

The image below illustrates the process of sculpting with a laser, showing an example of (a) a block, with a height of 5 mm and a length of 8 mm, at the start of the process, (b) the format that we want the sculpted block to be, and (c) the sequence of layer removals during the process, considering that in each step, a layer with 1 mm of width is removed. In the first step, the piece numbered 1 is removed; in the second, the one numbered with 2; and so it goes on. During the process of sculpting, the laser was turned on 7 times, in total, once for each removed piece of the block.

epsfbox{p11683b.eps}

Write a program that, given the height and the length of the block, and its final format, find out the total number of times the laser must be turned on.

Input 

The input contains several test cases. Each test case is composed by two lines. The first line of a test case contains two integers A and C, separated by a blank space, indicating, respectively, the height ( 1$ le$A$ le$104) and the length ( 1$ le$C$ le$104) of the block to be sculpted, in milimeters. The second line contains C integers Xi, each one indicating the final height, in milimeters of the block between the positions i and i + 1 through the length ( 0$ le$Xi$ le$A, for 0$ le$i$ le$C - 1). Consider that on each step, a layer of width 1 mm is removed on the parts of the block where the laser is turned on.

The end of the input is indicated by a line that contains only two zeros, separated by a blank space.

Output 

For each test case, your program must print a single line, containing an integer, indicating the number of times that the laser must be turned on to sculpt the block in the indicated format.

Sample Input 

5 8
1 2 3 2 0 3 4 5
3 3
1 0 2
4 3
4 4 1
0

Sample Output 

7
3
3



只要計算山峰與前一個山谷的高度差加總即可。


#include <stdio.h>

int main() {
    int A, C, X[10005];
    int i, j, k;
    while(scanf("%d %d", &A, &C) == 2) {
        if(A == 0)  break;
        for(i = 0; i < C; i++)
            scanf("%d", &X[i]);
        for(i = 1, j = 0; i < C; i++)
            if(X[i] != X[j])
                X[++j] = X[i];
        C = j+1;
        X[C] = A;
        int low = X[0], ret = 0;
        for(i = 1; i < C; i++) {
            if(X[i-1] < X[i] && X[i] > X[i+1]) {
                ret += X[i] - low;
                low = A;
            } else {
                if(X[i] < low)
                    low = X[i];
            }
        }
        printf("%d\n", ret + A - low);
    }
    return 0;
}

台長: Morris
人氣(910) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][greedy] 410 - Station Balance
此分類上一篇:[UVA][dp] 11081 - Strings

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文