核心算法,大家读了题都懂
直接上代码
AC代码
C++#include<bits/stdc++.h>//万能头文件
using namespace std;
#define int long long
int n,m;
priority_queue<pair<int,int> > q,qaq;
int a[2000010],b[2000010],c[2000010],j[2000010];
int f(const int a, const int b, const int c, const int j)
{
return a*j*j+b*j+c;
}
int ans = 0,ans1[2000010],ans2[2000010];
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++)
{
j[i] = 1;
cin>>a[i]>>b[i]>>c[i];
qaq.push(make_pair(-f(a[i],b[i],c[i],j[i]),i));
}
q.push(qaq.top()); qaq.pop();
for (int i = 1; i < n; i++)
{
pair<int, int> to = q.top(); q.pop(),now = qaq.top(); qaq.pop();
ans += now.first + to.first;
j[now.second]++;
j[to.second]++;
ans1[i] = now.second;
ans2[i] = to.second;
q.push(make_pair(-f(a[now.second], b[now.second], c[now.second], j[now.second]), now.second));
q.push(make_pair(-f(a[to.second], b[to.second], c[to.second], j[to.second]), to.second));
}
for (int i=n;i<=m;i++)
{
pair<int,int>t1=q.top();q.pop(),t2 = q.top();q.pop();
ans+=t1.first+t2.first;
j[t1.second]++;
j[t2.second]++;
ans1[i] = t1.second;
ans2[i] = t2.second;
q.push(make_pair(-f(a[t1.second],b[t1.second],c[t1.second],j[t1.second]),t1.second));
q.push(make_pair(-f(a[t2.second],b[t2.second],c[t2.second],j[t2.second]),t2.second));
}
cout << -ans << "\n";
for (int i = 1; i <= m; i++) cout << ans1[i] << " " << ans2[i] << "\n";//输出不要忘记
return 0;//华丽结束
}
//点个赞不过分吧qwq