zoukankan      html  css  js  c++  java
  • 1033. To Fill or Not to Fill (25)

    #include<stdio.h>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int C,D,Davg,N;
    double totDis = 0.0;
    double totCost = 0.0;
    double longDis = 0.0;
    double leftCap = 0.0;
    typedef struct NODE{
    	double price;
    	int dis;
    }Node;
    bool cmp(Node &a,Node &b){
    	return a.dis<b.dis;
    }
    int main()
    {
    	cin>>C>>D>>Davg>>N;
    	vector<Node>station(N+1);
    	for(int i=0;i<N;i++){
    		cin>>station[i].price>>station[i].dis;
    	}
    	station[N].price = 0;
    	station[N].dis = D;
    	sort(station.begin(),station.end(),cmp);
    	longDis = C * Davg;
    	int index = 0;
    	if(station[0].dis != 0 ) {
    			printf("The maximum travel distance = 0.00");
    			return 0;
    	}
    	while(index <N){
    		double minPri = station[index+1].price;
    		double nowDis = station[index].dis ;
    		double nexIndex;
    		if(station[index + 1].dis - station[index].dis>longDis){
    			printf("The maximum travel distance = %.2f",nowDis+longDis);
    			return 0;
    		}
    		for(int j = index+1 ;j<=N && (station[j].dis - nowDis)<=longDis;j++){		
    			if(station[j].price >= station[index].price){
    				if(station[j].price <= minPri ){
    					nexIndex = j;
    					minPri  = station[j].price;
    				}				
    			}
    			else{
    				nexIndex = j;
    				minPri = station[j].price;
    				break;
    			}
    		}
    		if(minPri >station[index].price){
    			totCost += (C-leftCap) * station[index].price;
    			leftCap = C - (station[nexIndex].dis - nowDis)/Davg;
    		}
    		else{
    			double tmp  = (station[nexIndex].dis - nowDis)/Davg;
    			if(tmp > leftCap){
    				totCost +=(tmp - leftCap) * station[index].price;
    				leftCap = tmp;
    			}
    			leftCap -= tmp;
    		}
    		totDis = station[nexIndex].dis;
    		index  = nexIndex;
    	}
    	printf("%.2f",totCost);
    	return 0;
     } 
    

      题意;从起点经过多个加油站到达终点,求最小花费,不能到达,求最远距离;

      总结:

          贪心策略,找能到达的首个油价比当前油价低的加油站,如果都没有,找能到达的最便宜的;

         初始邮箱为空,如果起点没有加油站,那么哪都去不了

  • 相关阅读:
    ACwing(基础)--- 01背包和完全背包、多重背包问题
    Python --- 实战一
    Python --- 正则表达式
    Python --- 网络爬虫
    Python --- 异常处理
    Python --- 文件操作
    Python --- 模块
    Python --- 基础语法
    securecrt颜色设置
    Tomcat启动排查
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/12127538.html
Copyright © 2011-2022 走看看