UVA920 Sunny Mountains排序

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA920 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.
    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

问题链接UVA920 Sunny Mountains
问题简述:(略)
问题分析:用排序来解决,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA920 Sunny Mountains */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
struct Point {
    int x, y;
} p[N];

bool cmp(Point a, Point b)
{
    return a.x > b.x;
}

double dist(Point a, Point b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

int main()
{
    int t, n;
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        for (int i = 0; i < n; i++)
            scanf("%d%d", &p[i].x, &p[i].y);

        sort(p, p + n, cmp);

        double sums = 0;
        int last = 0;
        for (int i = 1; i < n; i++)
            if (p[i].y > p[last].y) {
                sums += dist(p[i], p[i - 1]) * (p[i].y - p[last].y) / (p[i].y - p[i - 1].y);
                last = i;
            }

        printf("%.2f\\n", sums);
    }

    return 0;
}

以上是关于UVA920 Sunny Mountains排序的主要内容,如果未能解决你的问题,请参考以下文章

牛客 - Elo mountains(AC自动机+可持久化数组优化)

UVa 10305 给任务排序

Uva 10305 给任务排序

UVA10305 拓扑排序

[拓扑排序]Ordering Tasks UVA - 10305

UVA10305:拓扑排序