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;
     } 
    

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

      总结:

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

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

  • 相关阅读:
    HDU4112
    HDU1059 二进制拆分优化多重背包
    HDU1087
    HDU1978How Many Ways 记忆化dfs+dp
    HDU1160FatMouse's Speed
    HDU1503Advanced Fruits
    CF337C
    337BRoutine Problem
    【★★★★★模板专区★★★★★】
    【水】Jam计数法
  • 原文地址:https://www.cnblogs.com/zxzmnh/p/12127538.html
Copyright © 2011-2022 走看看