這次學院天梯賽選拔賽的題目,一看題目就知道是貪心算法。開始也想到了用左右兩邊的內插補點排序來做貪心,即內插補點的絕對值越大的,越排在前面,然後具體求哪邊就是看哪邊大。當時遇到的問題是,假如都是左邊大于右邊呢,那右邊的屬性如何點滿,然後就卡主了。
題目連結:http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1008&cid=34811
#include<bits/stdc++.h>
using namespace std;
struct tree
{
long long int l,r;
};
bool cmp(tree a,tree b)
{
return (a.r-a.l)>(b.r-b.l);
}
tree tr[1000005];
int main()
{
int t,n,a,b;
cin>>t;
while(t--)
{
cin>>n>>a>>b;
long long int sums=0;
for(int i=0;i<n;i++)
{
cin>>tr[i].l>>tr[i].r;
sums+=tr[i].l;
}
sort(tr,tr+n,cmp);
for(int i=0;i<b;i++)
{
sums+=tr[i].r-tr[i].l;
}
cout<<sums<<endl;
}
return 0;
}