Function
链接:https://vjudge.net/contest/402242#problem/C
思路:
因为x都是正整数,所以一开始x都为1,由f(x+1)-f(x)=2aix+ai+bi,每次找到最小的f(x+1)-f(x),然后这个函数的x+1,维护一下这个增长率。
代码:
#include<bits/stdc++.h> using namespace std; #define int long long typedef long long ll; const int maxn = 1e5+7; struct Node{ int a, b, c; }f[maxn]; struct S{ int a, val; bool operator < (const S &n1) const { return val > n1.val; } S(){} S(int aa, int bb) : a(aa), val(bb){} }; priority_queue<S> q; signed main() { int n, m; ll ans = 0; cin >> n >> m; for(int i = 1; i <= n; i++) { cin >> f[i].a >> f[i].b >> f[i].c; ans += f[i].a+f[i].b+f[i].c; } for(int i = 1; i <= n; i++) { int x = 2*f[i].a + f[i].a + f[i].b; q.push(S(f[i].a, x)); } for(int i = n + 1; i <= m; i++) { S tp = q.top(); ans += tp.val; tp.val += 2*tp.a; q.push(tp); } cout << ans << endl; }