蓝桥杯 算法训练 旅行家的预算

来源:互联网 发布:苹果mac怎么激活 编辑:程序博客网 时间:2024/05/04 00:44


问题描述
  一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的)。给定两个城市之间的距离D1、汽车油箱的容量C(以升为单位)、每升汽油能行驶的距离D2、出发点每升汽油价格P和沿途油站数N(N可以为零),油站i离出发点的距离Di、每升汽油价格Pi(i=1,2,……N)。计算结果四舍五入至小数点后两位。如果无法到达目的地,则输出“No Solution”。
输入格式
  第一行为4个实数D1、C、D2、P与一个非负整数N;
  接下来N行,每行两个实数Di、Pi。
输出格式
  如果可以到达目的地,输出一个实数(四舍五入至小数点后两位),表示最小费用;否则输出“No Solution”(不含引号)。
样例输入
275.6 11.9 27.4 2.8 2
102.0 2.9
220.0 2.2
样例输出
26.95



#include <stdio.h>#include <string.h>#include <iostream>#include<functional>#include <queue>#include <string>#include <algorithm>using namespace std;const int maxn = 1035;const int inf = 1<<30;int n;double d1,c,d2,p;double pos[maxn],val[maxn];int main(){#ifndef ONLINE_JUDGE  freopen("data.txt","r",stdin);  #endif scanf("%lf%lf%lf%lf%d",&d1,&c,&d2,&p,&n);for( int i = 1; i <= n; i ++ )scanf("%lf%lf",&pos[i],&val[i]);pos[0] = 0; val[0] = p;pos[n+1] = d1;for( int i = 1; i <= n+1; i ++ ){if( pos[i] - pos[i-1] > c*d2 ){puts("No Solution");return 0;}}int k = 0;double ans = 0;for( int i = 1; i <= n+1; i ++ ){double d = pos[i] - pos[i-1];   //从i-1点到i点的距离while( d ){while( pos[i] - d - pos[k] >= c*d2 )  //找到油箱中可能存在的来自最远的位置kk ++;for( int j = k; j < i; j ++ )  //从来k到i-1中找出最便宜的价格位置kif( val[j] < val[k] )k = j;double Max = c*d2 - ( pos[i] - pos[k] - d );   // 由k点加满油开到i点剩余的距离if( Max > d )Max = d;            //只要开正好开到i点的油量d -= Max;ans += Max/d2*val[k];}}printf("%.2lf\n",ans);return 0;}


0 0