UVa837 - Light and Transparencies(排序)

本文介绍了一种计算光线穿过一系列透明薄膜后的透光率的方法。通过定义透明系数并结合光线传播方向,计算不同区段的光线透射百分比。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Light and Transparencies 

When light traverses a transparent film, some energy is absorbed and the rest is transmitted to other side of the film. The percentage of light that is transmitted may be defined as Transparency Coefficient.

When several films are in the same direction of light, the correspondent transparency coefficients are multiplied. The goal of this problem is to determine the percentage of light that is projected on the ground, after traversing a given set of films.

\epsfbox{p837.eps}

Figure 1 Three films and correspondent Transparent Coeficients.


Consider the set of lines segments in fig. 1. They represent transparent films in the above conditions (transparency coefficients are written in brackets). Also consider that light is propagating in the vertical direction, from top to bottom.

Accordingly to the figure, the end points of the lines define a set of projected segments onto the ground (ground is represented by the X axe). For each projected segment, it is possible to evaluate the percentage of light that reaches the ground and, for the entire set of segments, a list can be obtained:

-inf, 2.0 -> 1.000
2.0, 4.0 -> 0.900
4.0, 7.0 -> 0.630
7.0, 9.0 -> 0.504
9.0, 13.5 -> 0.560
13.0, 17.0 -> 0.800
17.0, +inf -> 1.000

To simplify the problem, it is assumed that neither vertical lines nor crossing lines are given. Also no coincidences exist in the vertical projection of all given points (in other words, the X coordinates of the end points are all different from each other). On the other hand, a coordinate may be any real value from $ \infty$ to $ \infty$.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.


The input is a text file containing several lines, as follows.

The first line of the input contains the number NL (integer format) of line segments. It is followed by NLlines of text defining, each one, a line segment.

Accordingly to the above explanations, a line segment is defined by the coordinates of its two end pointsP1 and P2 and the transparency coefficient r, in the sequence xyxyr, separated by single spaces (all the five values are in the real format). No order is considered for the two points P1 and P2.

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.


The first line of the output contains the number NP (integer format) of projected segments. It is followed by NP lines of text, defining, each one, a projected segment. These lines must be sorted in ascending order of X values.

A projected segment must be defined by its coordinates X1 and X2, followed by the evaluated percentage of light. All the tree values must be in real format, rounded to 3 decimal digits and separated by single spaces. Infinite values must be represented by `-inf' or `+inf'.

Sample Input 

1

3
2.0 2.0 9.0 2.0 0.9
13.5 2.0 4.0 8.5 0.7
17.0 10.0 7.0 8.5 0.8

Sample Output 

7
-inf 2.000 1.000
2.000 4.000 0.900
4.000 7.000 0.630
7.000 9.000 0.504
9.000 13.500 0.560
13.500 17.000 0.800
17.000 +inf 1.000
刚开始想复杂了,考虑了把连续的透明度合并,提交了几次,总是Wrong Answer,后来看题意说是,没有点相交和垂直直线的情况

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

const double EPS = 1e-6;
const int INF = 0x3f3f3f3f;

struct Line
{
    double x1, x2, trans;
    bool operator < (const Line &other) const
    {
        if (fabs(x1 - other.x1) > EPS) return x1 < other.x1;

        return x2 < other.x2;
    }
};


vector<double> vPoint;
vector<Line> vLine;

void input();
void solve();

int main()
{
#ifndef ONLINE_JUDGE
    freopen("e:\\uva_in.txt", "r", stdin);
#endif

    int t;
    scanf("%d", &t);
    while (t--) {
        input();
        solve();
        if (t) printf("\n");
    }
    return 0;
}

void input()
{
    int n;
    Line line;

    scanf("%d", &n);
    vPoint.clear();
    vLine.clear();
    for (int i = 0; i < n; i++) {
        scanf("%lf%*lf%lf%*lf%lf", &(line.x1), &(line.x2), &(line.trans));
        //printf("%lf %lf %lf\n", line.x1, line.x2, line.trans);
        if (line.x1 > line.x2) {
            swap(line.x1, line.x2);
        }
        vPoint.push_back(line.x1);;
        vPoint.push_back(line.x2);
        vLine.push_back(line);
    }

    sort(vPoint.begin(), vPoint.end());
    sort(vLine.begin(), vLine.end());

    /*
    printf("size=%d\n", vLine.size());
    for (size_t i = 0; i < vLine.size(); i++) {
        printf("%.3lf %.3lf\n", vLine[i].x1, vLine[i].x2);
    }

    */
}

void solve()
{
    vector<Line> ans;
    Line line;
    line.x1 = -INF, line.x2 = vPoint.front(), line.trans = 1.0;

    ans.push_back(line);
    for (int i = 0; i < vPoint.size() - 1; i++) {
        double trans = 1.0;
        for (int j = 0; j < vLine.size(); j++) {
            if (vPoint[i] >= vLine[j].x1 && vPoint[i] <= vLine[j].x2 &&
                    vPoint[i + 1] >= vLine[j].x1 && vPoint[i + 1] <= vLine[j].x2) {
                trans *= vLine[j].trans;
            }
        }
        line.x1 = vPoint[i], line.x2 = vPoint[i + 1], line.trans = trans;
        ans.push_back(line);
    }

    line.x1 = vPoint.back(), line.x2 = INF, line.trans = 1.0;
    ans.push_back(line);

    printf("%d\n", ans.size());
    printf("-inf %.3lf %.3lf\n", ans[0].x2, 1.0);
    for (int i = 1; i < ans.size() - 1; i++) {
        printf("%.3lf %.3lf %.3lf\n", ans[i].x1, ans[i].x2, ans[i].trans);
    }
    printf("%.3lf +inf %.3lf\n", ans[ans.size() - 1].x1, 1.0);
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值