OJ的bug真多(这道题其实只用输出载重量)。。。于是集体爆0.
【DP】卡车


思路

01背包即可。
这里贪心,用价格除以重量得出此船的价值。
然后价值从大到小排序,大的就放,最后特判一下剩余空间即可。

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int n,v,lx,ll,zzl,Ans[100001],ans=0;
float jz;
struct dfjh{
	float j;
	int l,bh;
} f[100001];
bool K(dfjh a,dfjh b){
    return a.j>b.j;
}
int main(){
	//freopen("lorry.in","r",stdin);
	//freopen("lorry.out","w",stdout);
	scanf("%d%d",&n,&v);
	for(int i=1;i<=n;++i){
		scanf("%d%f",&lx,&jz);
		f[i].l=lx;f[i].j=jz/lx;f[i].bh=i; 
		//l:船的类型 j:价值 bh:第几搜船(用于输出哪艘船)
	}
	sort(f+1,f+1+n,K); //排序
	lx=v;ll=1;zzl=0;
	while(lx>0&&ll<=n){
		if(lx>=f[ll].l){  //能放的下
			lx-=f[ll].l;         //放,剩余空间减。
			zzl+=f[ll].l*f[ll].j;  //载重量
			Ans[++ans]=f[ll].bh;  //记下顺序、编号。
		}
		ll++;
	}
	printf("%d\n",zzl);  //输出
/*	for(int i=1;i<=ans;++i)
	  printf("%d ",Ans[i]);*/ //题库上不用
	//fclose(stdin);
	//fclose(stdout);
}

相关文章:

  • 2022-12-23
  • 2021-08-03
  • 2022-12-23
  • 2021-11-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-18
  • 2021-11-05
  • 2021-04-21
  • 2021-07-06
  • 2021-08-01
  • 2021-12-24
相关资源
相似解决方案