https://pintia.cn/problem-sets/994805342720868352/problems/994805458722734080
#include<cstdio>
#include<algorithm>
using namespace std
;
struct gas
{double price
;double km
;
}g
[520];
bool cmp(gas a
,gas b
)
{return a
.km
<b
.km
;
}
int main(void)
{double max_oil
;double km
;double speed
;int n
;int i
;double maxkm
=0; bool flag
=true; double x
=0;scanf("%lf%lf%lf%d",&max_oil
,&km
,&speed
,&n
); maxkm
=max_oil
*speed
;for(i
=0;i
<n
;i
++){scanf("%lf%lf",&g
[i
].price
,&g
[i
].km
);}g
[n
].price
= 0;g
[n
].km
= km
;sort(g
,g
+n
,cmp
);if(g
[0].km
!=0){flag
=false;printf("The maximum travel distance = 0.00\n");return 0;}else{ for(i
=1;i
<n
;i
++){if(maxkm
< (g
[i
].km
-g
[i
-1].km
) ){x
=g
[i
-1].km
+maxkm
;flag
=false;break;}x
=g
[i
].km
;}if(i
==n
){if(maxkm
< ( km
-g
[n
-1].km
) ){flag
=false;x
=g
[n
-1].km
+maxkm
;}} }if(flag
){int now
=0;double now_oil
=0;double money
=0;while(now
<n
){int minPrice_index
=-1;double minPrice
=100000;for(i
=now
+1;i
<=n
&&(g
[i
].km
-g
[now
].km
)<=maxkm
;i
++){if(g
[i
].price
<minPrice
){minPrice
=g
[i
].price
;minPrice_index
=i
;if(minPrice
<g
[now
].price
)break;}}double need
=(g
[minPrice_index
].km
-g
[now
].km
)/speed
;if (minPrice
< g
[now
].price
){money
+= (need
- now_oil
)*g
[now
].price
;now_oil
= 0;}else{money
+= (max_oil
- now_oil
)*g
[now
].price
;now_oil
= max_oil
- need
;}now
= minPrice_index
;}printf("%.2lf\n",money
);}elseprintf("The maximum travel distance = %.2lf\n",x
);return 0;
}
總結(jié)
以上是生活随笔為你收集整理的1033 To Fill or Not to Fill (25 分)【难度: 难 / 知识点: 模拟 贪心】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。