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

这题我服了,细节太多了,我逻辑跟不上了,转载一手ac代码以后再看吧,,小菜鸡的忧伤


转自

http://blog.csdn.net/shiwaigaoren12345/article/details/53053063


问题描述
  一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的)。给定两个城市之间的距离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


题解:

这一题你要是会做的话,实现起来其实就是一道模拟题。思路就是这样,你要脱离正常思维的限制,假设油是不会混在一起的,然后就是每次都要装满,如果遇到油箱里面有比当前的油贵的,那就是得替换掉,然后用当前的油把邮箱装满,用的时候就是用邮箱里面最便宜的油;开始的时候先判断是否会无法到达。然后就是模拟

代码不用看,就是要懂思路


AC代码:


[cpp]  view plain  copy
  1. # include <stdio.h>  
  2. # include <string.h>  
  3. # include <set>  
  4. # include <math.h>   
  5. using namespace std;  
  6. typedef long long int ll;  
  7. int del[100010], n;  
  8. double d[100010], v[100010], x[100010];    
  9. struct cmp{  
  10.     bool operator()(int a, int b)  
  11.     {  
  12.         return v[a]<v[b];  
  13.     }  
  14. };  
  15. set<int, cmp> s;  
  16. set<int, cmp> ::iterator it;  
  17. int judge(int c, int d2){  
  18.     for(int i=1; i<=n+1; i++){  
  19.         if(d[i]-d[i-1]>c*d2){  
  20.             return 0;  
  21.         }  
  22.     }  
  23.     return 1;  
  24. }  
  25. int main(){  
  26.     int i, j, k, cur;  
  27.     double d1, c, d2, p, sum;  
  28.     double ans=0;  
  29.     scanf("%lf%lf%lf%lf%d", &d1, &c, &d2, &p, &n);  
  30.     for(i=1; i<=n; i++){  
  31.         scanf("%lf%lf", &d[i], &v[i]);  
  32.     }  
  33.     d[n+1]=d1;  
  34.     d[0]=0;  
  35.     v[0]=p;  
  36.     if(!judge(c, d2)){  
  37.         printf("No Solution");  
  38.         return 0;  
  39.     }  
  40.     s.clear();  
  41.     for(i=0; i<=n; i++){  
  42.         sum=0.0;  
  43.         cur=0;  
  44.         for(it=s.begin(); it!=s.end(); it++){  
  45.             int no=*it;  
  46.             if(v[no]>v[i]){  
  47.                 del[cur++]=no;  
  48.                 x[no]=0;  
  49.             }  
  50.             else{  
  51.                 sum=sum+x[i];  
  52.             }  
  53.         }  
  54.         if(sum<c){  
  55.             x[i]=c-sum;  
  56.             s.insert(i);  
  57.         }  
  58.         for(j=0; j<cur; j++){  
  59.             s.erase(del[j]);  
  60.         }  
  61.         double dx=(d[i+1]-d[i])/d2;  
  62.         for(it=s.begin(); it!=s.end(); it++){  
  63.             int no=*it;  
  64.             cur=0;  
  65.             if(dx>x[no]){  
  66.                 dx=dx-x[no];  
  67.                 ans=ans+x[no]*v[no];  
  68.                 x[no]=0.0;  
  69.                 del[cur++]=no;  
  70.             }  
  71.             else{  
  72.                 x[no]=x[no]-dx;  
  73.                 ans=ans+dx*v[no];  
  74.                 if(fabs(x[no]-0)<0.000001){  
  75.                     x[no]=0.0;  
  76.                     del[cur++]=no;  
  77.                 }  
  78.                 break;  
  79.             }  
  80.         }  
  81.     }  
  82.     printf("%.2lf", ans);  
  83.     return 0;  
  84. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值