题解 | #【模板】实数域三分#

【模板】实数域三分

https://www.nowcoder.com/practice/695908ca830f4952ae6e2ed196fe5d64

#include <iostream>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <numeric>
#include <ctime>
#include <string>
#include <bitset>
#include <unordered_map>
#include <unordered_set>

using namespace std;
using ll = long long;
const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18;

int n;

struct Node {
    double k, a, b;
} a[N];

double check(double mid) {
    double ans = 0;

    for (int i = 1; i <= n; i++) {
        ans += fabs(a[i].k * mid + a[i].a) + a[i].b;
    }

    return ans;
}

void solve() {
    double L, R;
    cin >> n; 
    cin >> L >> R;

    for (int i = 1; i <= n; i++) {
        cin >> a[i].k >> a[i].a >> a[i].b;
    }

    while (fabs(R - L) > 1e-7) {
        double mid = (L + R) / 2;
        double mmid = (mid + R) / 2;

        if (check(mid) < check(mmid))R = mmid;
        else L = mid;
    }

    cout << fixed << setprecision(10) << check(L) << "\n";
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    int t = 1;
    cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务