您的位置:首页 > 其它

算法提高 快乐司机

2017-03-13 19:42 225 查看
算法提高 快乐司机

时间限制:1.0s 内存限制:256.0MB

问题描述
  "嘟嘟嘟嘟嘟嘟
  喇叭响
  我是汽车小司机
  我是小司机
  我为祖国运输忙
  运输忙"
  这是儿歌“快乐的小司机”。话说现在当司机光有红心不行,还要多拉快跑。多拉不是超载,是要让所载货物价值最大,特别是在当前油价日新月异的时候。司机所拉货物为散货,如大米、面粉、沙石、泥土......
  现在知道了汽车核载重量为w,可供选择的物品的数量n。每个物品的重量为gi,价值为pi。求汽车可装载的最大价值。(n<10000,w<10000,0<gi<=100,0<=pi<=100)
输入格式
  输入第一行为由空格分开的两个整数n w
  第二行到第n+1行,每行有两个整数,由空格分开,分别表示gi和pi
输出格式
  最大价值(保留一位小数)
样例输入
5 36
99 87
68 36
79 43
75 94
7 35
样例输出
71.3
解释:
先装第5号物品,得价值35,占用重量7
再装第4号物品,得价值36.346,占用重量29
最后保留一位小数,得71.3

注意特殊值!!

#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#define FOR(i,x,n) for(long i=x;i<n;i++)
#define ll long long int
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAX_N 50005

using namespace std;

struct node{int g,p;double unitPrice;};
node d[10005];

int cmp(node a,node b){
return a.unitPrice>b.unitPrice;
}

int main()
{
//freopen("input1.txt", "r", stdin);
//freopen("data.out", "w", stdout);
int N,W;
scanf("%d %d",&N,&W);
if(N==0&&W==0){
printf("0");
}
int capacity=W;
double value=0;
FOR(i,0,N){
scanf("%d %d",&d[i].g,&d[i].p);
d[i].unitPrice=d[i].p*1.0/d[i].g;
}
sort(d,d+N,cmp);
FOR(i,0,N){
if(capacity-d[i].g>0||capacity-d[i].g==0&&capacity==0){
capacity-=d[i].g;
value+=d[i].p;
}else if(capacity-d[i].g==0&&capacity!=0){
capacity-=d[i].g;
value+=d[i].p;
printf("%.1lf",value);
return 0;
}else if(capacity-d[i].g<0){
value+=d[i].unitPrice*capacity;
capacity=0;
printf("%.1lf",value);
return 0;
}
}
printf("%.1lf",value);
//fclose(stdin);
//fclose(stdout);
return 0;
}
/*
3 0
0 1
0 2
1 1

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: