UPC LED 二分

问题 E: LED

时间限制: 2 Sec  内存限制: 512 MB
提交: 429  解决: 63
[提交] [状态] [命题人:admin]

题目描述

A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, namely, ACMOLED. An ACMOLED has a special behavior that the intensity of light emitted from it changes in two steps as the voltage of the electric current increases, as depicted in the graph below.

As shown, an ACMOLED is not activated in the voltage range from 0 to V1, while it emits light with intensity L1 ≥ 0 when the voltage reaches the first threshold V1 and light with intensity L2 ≥ L1 when the voltage reaches the x threshold V2. More specifically, if F(v) is the function that maps voltage v to the intensity of light emitted from an ACMOLED, then for four real numbers L1, L2, V1, and V2 with 0 ≤ L1 ≤ L2 and 0 < V1 < V2, we have

The very issue now is that ACM R&D still does not know the exact values of two threshold voltage values V1 and V2 and the two intensity values L1 and L2 as well. Researchers in ACM R&D plan to estimate these four values for ACMOLEDs by repeated experiments.
Experiments are performed by applying current of a specific voltage and observing the intensity of light emitted from an ACMOLED. After n repeated experiments with different voltage values, obtained are the data of n tuples (v1, l1), (v2, l2), ..., (vn, ln), where li is the observed intensity for voltage vi. Due to the impreciseness of the observing device and other reasons, the experimental data are not accurate and may contain some error. Nonetheless, they want to find a best estimated intensity function F(v) that minimizes the following error function:

where |x| denotes the absolute value of a real number x.
For a given data of n tuples, write a program that finds an estimated intensity function F that minimizes the above error function and outputs the value of error(F).

 

输入

Your program is to read from standard input. The input starts with a line containing an integer n (1 ≤ n ≤ 300,000), where n is the number of tuples (vi, li) in the experimental data. In the following n lines, each line contains two integers, which range inclusively from 0 to 109, representing vi and li in each tuple (vi, li) of the experimental data. Note that you may assume that there are no two tuples (vi, li) and (vj, lj) in the input such that 1 ≤ i < j ≤ n and vi = vj.

 

输出

Your program is to write to standard output. Print exactly one line consisting of one real number, rounded to the first decimal place, which represents the minimum value of error(F).

 

样例输入

复制样例数据

5
0 0
2 1
3 5
6 7
7 11

样例输出

1.0

题意:确定v1,v2,l1,l2使得在给定的数据中误差最小

既然要求的是最小的误差,那么想到二分答案来处理

代码:

#include <bits/stdc++.h>
 
typedef long long ll;
using namespace std;
const int maxn = 3e5 + 100;
const int inf = 0x3f3f3f3f;
struct node {
    int v;
    double l;
} s[maxn];
int n;
 
bool check(double x) {
    int i = 1;
    double minn = inf, maxx = -1;
    double l1 = 0, l2 = 0;
    for (; i <= n; i++) {
        if (s[i].v == 0 && s[i].l > x) return 1;
        minn = min(minn, s[i].l);
        maxx = max(maxx, s[i].l);
        if (maxx > x) break;
        l1 = maxx;
    }
    minn = inf, maxx = -1;
    for (; i <= n; i++) {
        if (s[i].v == 0 && s[i].l > x) return 1;
        minn = min(minn, s[i].l);
        maxx = max(maxx, s[i].l);
        if (minn + x <= l1 && maxx - x <= l1) break;
        if ((maxx - minn) / 2 > x) break;
        l2 = (maxx + minn) / 2;
    }
    minn = inf, maxx = -1;
    for (; i <= n; i++) {
        if (s[i].v == 0 && s[i].l > x) return 1;
        minn = min(minn, s[i].l);
        maxx = max(maxx, s[i].l);
        if (minn + x <= l2 && maxx - x <= l2) break;
        if ((maxx - minn) / 2 > x) break;
    }
    if (i == n + 1) return 0;
    return 1;
}
 
bool cmp(node a, node b) {
    return a.v < b.v;
}
 
int main() {
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> s[i].v >> s[i].l;
    sort(s + 1, s + n + 1, cmp);
    double l = 0, r = inf;
    while (r - l > 1e-5) {
        double mid = (l + r) / 2;
        if (check(mid)) l = mid;
        else r = mid;
    }
    printf("%.1lf\n", r);
    return 0;
}

 

[提交][状态]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值