这题我服了,细节太多了,我逻辑跟不上了,转载一手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代码:
- # include <stdio.h>
- # include <string.h>
- # include <set>
- # include <math.h>
- using namespace std;
- typedef long long int ll;
- int del[100010], n;
- double d[100010], v[100010], x[100010];
- struct cmp{
- bool operator()(int a, int b)
- {
- return v[a]<v[b];
- }
- };
- set<int, cmp> s;
- set<int, cmp> ::iterator it;
- int judge(int c, int d2){
- for(int i=1; i<=n+1; i++){
- if(d[i]-d[i-1]>c*d2){
- return 0;
- }
- }
- return 1;
- }
- int main(){
- int i, j, k, cur;
- double d1, c, d2, p, sum;
- double ans=0;
- scanf("%lf%lf%lf%lf%d", &d1, &c, &d2, &p, &n);
- for(i=1; i<=n; i++){
- scanf("%lf%lf", &d[i], &v[i]);
- }
- d[n+1]=d1;
- d[0]=0;
- v[0]=p;
- if(!judge(c, d2)){
- printf("No Solution");
- return 0;
- }
- s.clear();
- for(i=0; i<=n; i++){
- sum=0.0;
- cur=0;
- for(it=s.begin(); it!=s.end(); it++){
- int no=*it;
- if(v[no]>v[i]){
- del[cur++]=no;
- x[no]=0;
- }
- else{
- sum=sum+x[i];
- }
- }
- if(sum<c){
- x[i]=c-sum;
- s.insert(i);
- }
- for(j=0; j<cur; j++){
- s.erase(del[j]);
- }
- double dx=(d[i+1]-d[i])/d2;
- for(it=s.begin(); it!=s.end(); it++){
- int no=*it;
- cur=0;
- if(dx>x[no]){
- dx=dx-x[no];
- ans=ans+x[no]*v[no];
- x[no]=0.0;
- del[cur++]=no;
- }
- else{
- x[no]=x[no]-dx;
- ans=ans+dx*v[no];
- if(fabs(x[no]-0)<0.000001){
- x[no]=0.0;
- del[cur++]=no;
- }
- break;
- }
- }
- }
- printf("%.2lf", ans);
- return 0;
- }