题解 | #【模板】整数域三分#
【模板】整数域三分
https://www.nowcoder.com/practice/12e52f8182354b94b7ff4800dc1c0cfc
#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 {
ll k, a, b;
} a[N];
ll check(ll mid) {
ll ans = 0;
for (int i = 1; i <= n; i++) {
ans += abs(a[i].k * mid + a[i].a) + a[i].b;
}
return ans;
}
void solve() {
ll 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(L+1!=R){
ll lmid=L+(R-L)/3;
ll rmid=R-(R-L)/3;
if(check(lmid)<=check(rmid))R=rmid-1;
else L=lmid+1;
}
cout<<min(check(L),check(R))<<'\n';
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
while (t--) {
solve();
}
return 0;
}
查看10道真题和解析