三分模板。
#include<bits/stdc++.h> #define int long long #define double long double #define x first #define y second using namespace std; typedef long long LL; typedef long long ll; typedef pair<int, int> PII; const int N = 1e6 + 10; const int M = 1e3 + 10; int mod = 1e9 + 7; struct link { int k, a, b; } a[N]; int n; int check(int x) { int res = 0; for (int i = 1; i <= n; i++) res += abs(a[i].k * x + a[i].a) + a[i].b; return res; } void solve() { cin >> n; int l, r; cin >> l >> r; for (int i = 1; i <= n; i++) cin >> a[i].k >> a[i].a >> a[i].b; while (r-l>1) { int m1 = l + (r - l)/3; int m2 = r - (r - l)/3; int tp1 = check(m1); int tp2 = check(m2); if (tp1 <= tp2) r = m2-1; else l=m1+1; } cout<<min(check(l),check(r))<<"\n"; } signed main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); int _; _ = 1; cin >> _; while (_--) { solve(); } }