[PAT] Class 1033 To Fill or Not to Fill (25 points) (greedy, thinking you can make a simple solution)

Meaning of the questions:

Four positive input C, DIS, D, N (C <= 100, DIS <= 50000, D <= 20, N <= 500), representing the tank volume, distance to the target Hangzhou city, per liter of gasoline can the distance traveled, the number of gas stations. The next input data N rows, each row comprising a liter of petrol stations decimal representing the price of the gas station and the distance from Hangzhou. Hangzhou output to the minimum fuel cost the city, if not to the words of the output farthest from Hangzhou.

Ideas:

Every time to fill the tank when a gas station, gas station prices cheaper than i j i fill the tank if gas stations can travel range, then take on the position as i j position minus the cost (fuel tank the maximum travel range of fill subtracting the distance between stations i and j) take this journey required consumption of gasoline plus the cost of a whole box of oil; if not within the maximum range of travel is cheaper than i refueling station, to find a gas station k is within the scope of the cheapest, then the cost is spent on the position k i positions plus the cost of gasoline this journey from the need to consume between stations i and k. If, after the middle of the gas station to fill up the maximum distance x exceeds the target city, making it updated ans equal to the current cost of gas stations x minus oil finish this box to spend beyond oil target distance of the city, there is a gas station prices if x cheaper gas stations continue to look, if not directly break, is already the optimal solution, and after the price of oil back to your gas station will go beyond the direct target price x city after refueling. The core idea is greedy, another solution to the problem is based on only the largest DIS 30000, can be sorted from high to low oil prices of the gas station, calculated per kilometer of the cheapest fuel prices in kilometers coordinates sweep from back to front If there is a breakpoint (the km no fuel, before and after the middle of nowhere, the direct current output position where km), no breakpoint will be 1 ~ DIS oil needed for adding each kilometer is the answer, I think this place needs to be considered less greedy, processing them more clear, the code free and then attach a whole. . .

trick:

The first test point comprising a plurality of stations with the same coordinate data.

The second test point data includes the starting point of no gas station.

The first four test points have included the distance from the target gas stations more or less with the city's largest mail volume and the distance from the target cities closer gas station oil more expensive.

The first test point contains 0,6 have more distance from the target city gas station less than or equal with the largest volume of mail and the distance from the target cities closer gas station oil more expensive.

I guess from the above data error code, hoping to help a friend card data point 2 or 4 (designated focus) a.

Code:

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
double c,dis,d;
int n;
pair<double,double>a[507];
double sum[507];
int main(){
cin>>c>>dis>>d>>n;
for(int i=1;i<=n;++i)
cin>>a[i].second>>a[i].first;
sort(a+1,a+1+n);
if(a[1].first>0){
cout<<"The maximum travel distance = 0.00";
return 0;
}
sum[1]=c*a[1].second;
double ans=2e18;
for(int i=1;i<=n;++i){
double mn=2e18;
int pos=0;
for(int j=i+1;j<=n;++j){
if(a[j].first-a[i].first>c*d)
break;
if(a[j].second<a[i].second){
pos=j;
break;
}
if(a[j].second<mn){
mn=a[j].second;
pos=j;
}
}
if(!pos&&i<n){
cout<<"The maximum travel distance = ";
printf("%.2f",a[i].first+c*d);
return 0;
}
if(a[i].first+c*d>=dis){
ans=min(ans,sum[i]-(a[i].first+c*d-dis)/d*a[i].second);
int flag=0;
for(int j=i+1;j<=n;++j)
if(a[j].second<a[i].second)
flag=1;
if(!flag)
break;
}
if(a[pos].second<a[i].second)
sum[pos]=sum[i]-(c*d-(a[pos].first-a[i].first))/d*a[i].second+a[pos].second*c;
else
sum[pos]=sum[i]+(a[pos].first-a[i].first)/d*a[pos].second;
if(i<n)
i=pos-1;
//cout<<i<<" "<<pos<<" "<<sum[pos]<<" "<<ans<<"\n";
}
if(a[n].first+c*d<dis){
cout<<"The maximum travel distance = ";
printf("%.2f",a[n].first+c*d);
}
else
printf("%.2f",ans);
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11515967.html