65,208
社区成员
发帖
与我相关
我的任务
分享
过了,采用贪心思想,先按照智力从小到大排序,可以这样理解,最优解中肯定有一个人智力最小,所以我们轮流让每个人做智力最小的人,肯定能找到最优解
然后每次取k-1个人,不断找,找到k-1个人的美貌值都是最大的
#include<iostream>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std;
const int maxn=10000+5;
struct node{
int w,v;
}stu[maxn];
bool cmp1(node x,node y)
{
return x.w<y.w;
}
bool cmp2(node x,node y)
{
return x.v>y.v;
}
int n,k,limit,res,ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>n>>k>>limit)
{
res=-1;
for(int i=1;i<=n;i++)
cin>>stu[i].w>>stu[i].v;
sort(stu+1,stu+n+1,cmp1);
for(int i=1;i<=n-k+1;i++)
{
ans=stu[i].v;
int w=stu[i].w;
vector<node> tmp;
for(int j=i+1;stu[j].w-w<=limit&&j<=n;j++)
tmp.push_back(stu[j]);
if(tmp.size()<k-1) continue;
sort(tmp.begin(),tmp.end(),cmp2);
for(int m=0;m<k-1;m++)
ans+=tmp[m].v;
res=max(res,ans);
}
cout<<res<<endl;
}
return 0;
}