【PAT甲级】1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)

题意:

输入四个正数C,DIS,D,N(C<=100,DIS<=50000,D<=20,N<=500),分别代表油箱容积,杭州到目标城市的距离,每升汽油可以行驶的路程,加油站数量。接下来输入N行数据,每行包括一个小数代表该加油站每升汽油的价格和该加油站距离杭州的距离。输出杭州到该城市的最小加油花费,如果到不了的话输出离开杭州的最远路程。

思路:

每次到一个加油站时都把油箱加满,如果加油站i油箱加满所能行驶的范围内有加油站j油价比i便宜,那么j位置的花费就为i位置的花费减去(油箱加满时的最大行驶范围减去加油站i与j之间的距离)这段路程所需要消耗的汽油的花费再加上一整箱油的花费;如果最大行驶范围内没有比i便宜的加油站,就找一个加油站k是范围内最便宜的,那么k位置的花费就为i位置的花费加上加油站i与k之间的距离这段路程所需要消耗的汽油的花费。中途如果在加油站x加满油后最大路程超过目标城市,就更新ans使得等于当前加油站x的花费减去这箱油跑完超出目标城市距离的油的花费,如果x加油站后有油价更便宜的加油站就继续寻找,如果没有的话直接break,已经是最优解了,后面油价贵的加油站加油后的价格会超出在x加油后直接跑到目标城市的价格。核心想法是贪心,另一种解法是根据该题DIS最大只有30000,可以以加油站的油价从高到低排序,计算出每公里所加油最便宜的价格,以公里为坐标从后向前扫如果有断点(该公里无油可加,前不着村后不着店,则直接输出当前所在公里位置),无断点则将1~DIS每一公里所需的油价相加即为答案,我认为这样贪心需要考虑的地方较少,处理起来比较清晰明了,代码有空再整一个附上。。。

trick:

第1个测试点包含了多个加油站坐标相同的数据。

第2个测试点包含了起点没有加油站的数据。

第4个测试点包含了有多个加油站距离目标城市的距离小于等于邮箱最大容积的行驶距离且距离目标城市越近的加油站油越贵。

第0,6个测试点包含了有多个加油站距离目标城市的距离小于等于邮箱最大容积的行驶距离且距离目标城市越近的加油站油越便宜。

以上数据来自我错误代码的猜测,希望能对卡数据点2或者4(划重点)的朋友有所帮助。

代码:

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

原文地址:https://www.cnblogs.com/ldudxy/p/11515967.html

时间: 2024-08-05 00:06:21

【PAT甲级】1033 To Fill or Not to Fill (25 分)(贪心,思维可以做出简单解)的相关文章

PAT 甲级 1067 Sort with Swap(0, i) (25 分)(贪心,思维题)*

1067 Sort with Swap(0, i) (25 分) Given any permutation of the numbers {0, 1, 2,..., N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may ap

【PAT甲级】1079 Total Sales of Supply Chain (25 分)

题意: 输入一个正整数N(<=1e5),表示共有N个结点,接着输入两个浮点数分别表示商品的进货价和每经过一层会增加的价格百分比.接着输入N行每行包括一个非负整数X,如果X为0则表明该结点为叶子结点接着输入一个整数表示该零售商进货的数量,X不为零则接着输入X个正整数表示它的下级经销商是哪些结点.输出所有零售商进货的总价.(结点从0~N-1,0为根节点即供应商) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using name

【PAT甲级】1097 Deduplication on a Linked List (25 分)

题意: 输入一个地址和一个正整数N(<=100000),接着输入N行每行包括一个五位数的地址和一个结点的值以及下一个结点的地址.输出除去具有相同绝对值的结点的链表以及被除去的链表(由被除去的结点组成的链表). AAAAAccepted code: 1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 int nex[100007],val[100007]; 5 vector<

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

1105 Spiral Matrix (25分) This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The

PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. 建立结构体节点,包括起始地址addr,下一个地址to,值value.链表数组索引为地址,接下来就是模拟链表的操作了,并且建立一个flag数组标记对应值K是否出现,若出现则flag[k]=addr,未出现则为-1,注意这里不能为0因为地址值存在为0的情况.最后的输出地址前面要补0,如果是链尾的话,-

PAT 甲级 A1082 (2019/02/14)13/25

#include<cstdio> #include<cstring> char strnumber[16][5] = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "Ge", &

PAT 甲级 A1016 (2019/02/23)0/25

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1010; int toll[25]; struct Record { char name[25]; int month, dd, hh, mm; bool status; } rec[maxn], temp; bool cmp(Record a, Record b) { int

PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequence

PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, ..., N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if