解题思路
一开始用了排序+一种神奇的方法,然后ssloj上的不用输出步骤,所以0分,后来测是80分。
100分使用贪心:把每辆车的载重量除以空间然后,记录下空间,然后进行排序,能装进去的就装进去。
>代码
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct ooo
{
float c; int yy;
}a[100005];
int n,v,x,s,t;
long long ans; float y; //y也要用float不然会出错
bool lil(ooo aa,ooo bb) {return aa.c>bb.c;}
int main()
{
scanf("%d%d",&n,&v);
for(int i=1;i<=n;i++)
{
scanf("%d%f",&x,&y);
a[i].c=y/x; a[i].yy=x;
}
sort(a+1,a+1+n,lil);
s=v; t=1; //s是车的剩余空间,t模拟船的序号
while(s>0&&t<=n)
{
if(a[t].yy<=s) //能装就装
{
s-=a[t].yy;
ans+=a[t].c*a[t].yy;
}
t++;
}
printf("%lld",ans);
return 0;
}