PAT(甲级)1033

论坛 期权论坛 脚本     
已经匿名di用户   2022-5-29 19:05   1901   0

1033. To Fill or Not to Fill (25)

时间限制
10 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
ZHANG, Guochuan

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,...N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print "The maximum travel distance = X" where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:

The maximum travel distance = 1200.00

///
//TRY IT AGAIN NEXT TIME BY REWRITE FIND_NEXT
//
///
//
#include <iostream>
#include <algorithm>

using namespace std;
const int NUM =505;
struct Gas{
 float price;
 float distance;
}gastation[NUM];

bool cmp(const Gas &g1,const Gas &g2){
 return g1.distance < g2.distance;
}

float Cmax,D,Davg,total_price=0;
float maxdis = 0;
int N;
 

//ACTUALLY, RESERVE LAST ITERATOR CAN MAKE THIS QUERY FASTER!!,TRY NEXT TIME
/
int find_next(int start,int end){
 if(start == end)       //arrive at last gas station
     return -1;
 float poss_dis = Cmax*Davg;
 if(start+1 <= N && gastation[start].distance + poss_dis < gastation[start+1].distance)  // can not arrive at next stop
     return 0;
 int index = start+1;
 for(int i= start+1;i<=end && gastation[i].distance -gastation[start].distance <= poss_dis ;i++)
     if(gastation[i].price <= gastation[index].price)
         index = i; 
    return index;
}

int main(){
// freopen("test.txt","r",stdin);
 scanf("%f%f%f%d",&Cmax,&D,&Davg,&N);
 for(int i=1;i<=N;i++){
  float price;
  float distance;
  scanf("%f%f",&price,&distance);
  gastation[i].price = price;
  gastation[i].distance = distance;
 }
 
 sort(gastation+1,gastation+N+1,cmp);
 if(gastation[1].distance != 0){
  printf("The maximum travel distance = 0.00\n");
  return 0;
 }
 bool flag =false;
 float remain = 0;
 for(int i=1;i<=N;i++){
  int next = find_next(i,N);
  if(next == -1){
   if(gastation[N].distance + Cmax*Davg >= D){
    total_price += ((D-gastation[N].distance)/Davg - remain)*gastation[N].price;
    printf("%.2f\n",total_price);
   }else
       printf("The maximum travel distance = %.2f\n",gastation[N].distance + Cmax*Davg );
   return 0;
  }else if(!next){
            printf("The maximum travel distance = %.2f\n",gastation[i].distance + Cmax*Davg );
            return 0;
  }else{
   if(gastation[i].price > gastation[next].price){ 
    int cur = i;
    for(int j = i;j<=next;j++)    //when it arrives at this station, the tank must be empty
        if(gastation[j].price <= gastation[cur].price){
         float need = (gastation[j].distance - gastation[cur].distance)/Davg;
         if(remain >= need)    //do not need fill gas
             remain -= need;
         else{
          total_price += (need - remain)*gastation[cur].price;
          remain = 0;
      }           
         cur = j;
     }
   }else{
    if(gastation[i].distance+ Cmax*Davg >=D ){
     total_price += (D- gastation[i].distance - remain)/Davg*gastation[i].price;
     break;
    }
    total_price += (Cmax - remain)*gastation[i].price;   //fill full in current gastation
    remain = Cmax - (gastation[next].distance - gastation[i].distance)/Davg;
   }
   i = next-1;
  }    
 }
 
 printf("%.2f\n",total_price);
 return 0;
 
}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:81
帖子:4969
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP