24h購物| | PChome| 登入
2012-04-12 19:38:31| 人氣2,009| 回應0 | 上一篇 | 下一篇

[UVA][Math] 920 - Sunny Mountains

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

Problem A

Sunny Mountains

During their honeymoon, Mrs and Mr Smith went to the Himalayas. How they were surprised when they observed that, during the sunset, all the snow touched by the sunbeams turned red.

Such a magnificent landscape leaves everyone plenty of emotion, but Mr Smith's number obsession overcame all this. He rapidly began evaluating distances, which made Mrs Smith quite upset.

Problem

Your work is to help him calculate the size, in meters, of the mountainsides that became red as the sun sets. Mr Smith's honeymoon depends on you! Please be quick and efficient.

For the sake of simplicity, consider that, during the sunset, the sunbeams are horizontal and assume that the landscape is described by the set of coordinates of the mountain peaks and cols. This can be depicted by the following figure. A landscape, in this context, is then a sequence of peaks and cols (i.e., only a col follows a peak and conversely).

Note that, in this picture, the sunny mountainsides are emphasized by bold lines and the coordinates of the landscape are emphasized by bold points.

Thus, the goal of this problem is to calculate the total length in meters of the bold lines.

For this task consider that: (1) for all coordinates (x, y), 0 <= x <= 30000 and 0 <= y <= 8848; (2) the unit is the meter; (3) all the X-coordinates are pair-wise distinct; (4) the leftmost point has 0 as X-coordinate and the rightmost point has 0 as Y-coordinate; (5) The total number of coordinates given is n <= 100.

Input

The first line of input contains C (0 < C < 100 ), the number of test cases that follows.

Each test case starts with a line containing the number N of coordinate pairs. The remaining  N lines for each test case contain the coordinates defining the landscape. Each of these lines contains two integers, x and y, separated by a single space. The first integer, x, is the X-coordinate, and the second, y, is the Y-coordinate of the considered point.

Output

The output is formed by a sequence of lines, one for each test case. Each line contains a single real number with exactly two decimal digits. This number represents the length in meters of the sunny mountainsides for the corresponding test case.

Sample Input

2
11
1100 1200
0 500
1400 100
600 600
2800 0
400 1100
1700 600
1500 800
2100 300
1800 700
2400 500
2
0 1000
1000 0

Sample Output

1446.34
1414.21
 

模擬

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct {
    int x, y;
}Point;
int cmp(const void *i, const void *j) {
    Point *x, *y;
    x = (Point *)i, y = (Point *)j;
    return y->x - x->x;
}
int main() {
    Point D[100];
    int C, n, i;
    scanf("%d", &C);
    while(C--) {
        scanf("%d", &n);
        for(i = 0; i < n; i++)
            scanf("%d %d", &D[i].x, &D[i].y);
        qsort(D, n, sizeof(Point), cmp);
        int maxH = 0;
        double sum = 0;
        for(i = 1; i < n; i++) {
            if(D[i].y > maxH) {
                double x, y;
                x = D[i].x-(double)(D[i].x-D[i-1].x)*(D[i].y-maxH)/(D[i].y-D[i-1].y);
                y = maxH;
                sum += sqrt((x-D[i].x)*(x-D[i].x) + (y-D[i].y)*(y-D[i].y));
                maxH = D[i].y;
            }
        }
        printf("%.2lf\n", sum);
    }
    return 0;
}

台長: Morris
人氣(2,009) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][Math] 11909 - Soya Milk
此分類上一篇:[UVA] 270 - Lining Up

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