博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
1033. To Fill or Not to Fill (25)
阅读量:4151 次
发布时间:2019-05-25

本文共 2243 字,大约阅读时间需要 7 分钟。

加油事件的贪心选择模拟

#include
#include
#include
typedef struct Station{ double dis; double price; bool operator<(const Station& other) const { return dis < other.dis; }}Station;double unit_gas;int FindNeatestCheaperStation(int i, double gas, std::vector
& s){ int num = s.size(); int min_index = i; for(int j = i+1; j < num && s[j].dis-s[i].dis <= gas*unit_gas; ++j) { if(s[j].price < s[min_index].price) { min_index = j; break; } } return min_index;}int GetFastestStation(int i, double gas, std::vector
& s){ int num = s.size(); int max_index = i; for(int j = i+1; j < num && s[j].dis-s[i].dis <= gas*unit_gas; ++j) { max_index = j; } return max_index;}int main(){ double max_gas, total_dis; int n; while(scanf("%lf%lf%lf%d",&max_gas,&total_dis,&unit_gas,&n)!=EOF) { std::vector
sVec(n); for(int i = 0; i < n; ++i) scanf("%lf %lf",&sVec[i].price,&sVec[i].dis); //sort station by distance std::sort(sVec.begin(), sVec.end()); Station vir; vir.dis = total_dis;//add destination as a virtual station in the end sVec.push_back(vir); //algorithm core if(n==0 || sVec[0].dis > 0)//special case printf("The maximum travel distance = 0.00\n"); else//greedy now { double remain_gas = 0.0; double cur_money = 0.0; int i; for(i = 0; i < n; ) { int index; //1. using remain gas find nearest cheaper station index = FindNeatestCheaperStation(i, remain_gas, sVec); if(index != i)//find a station else {//get there remain_gas -= (sVec[index].dis-sVec[i].dis)/unit_gas; i = index; continue; } //2. using max_gas find nearest cheaper station index = FindNeatestCheaperStation(i, max_gas, sVec); if(index != i)//find a station else {//get there cur_money += ((sVec[index].dis-sVec[i].dis)/unit_gas-remain_gas)*sVec[i].price; remain_gas = 0.0; i = index; continue; } //3. get the max_gas and go as far as possible index = GetFastestStation(i, max_gas, sVec); if(index != i) {//can get some where cur_money += (max_gas-remain_gas)*sVec[i].price; remain_gas = max_gas-(sVec[index].dis-sVec[i].dis)/unit_gas; i = index; continue; } else { printf("The maximum travel distance = %.2lf\n", sVec[i].dis+max_gas*unit_gas); break; } }//end of greedy if(i == n) printf("%.2lf\n", cur_money); } } return 0; }

 

转载地址:http://caxti.baihongyu.com/

你可能感兴趣的文章
DeepLearning tutorial(5)CNN卷积神经网络应用于人脸识别(详细流程+代码实现)
查看>>
DeepLearning tutorial(6)易用的深度学习框架Keras简介
查看>>
DeepLearning tutorial(7)深度学习框架Keras的使用-进阶
查看>>
流形学习-高维数据的降维与可视化
查看>>
Python-OpenCV人脸检测(代码)
查看>>
python+opencv之视频人脸识别
查看>>
人脸识别(OpenCV+Python)
查看>>
6个强大的AngularJS扩展应用
查看>>
网站用户登录系统设计——jsGen实现版
查看>>
第三方SDK:讯飞语音听写
查看>>
第三方SDK:JPush SDK Eclipse
查看>>
第三方开源库:imageLoader的使用
查看>>
自定义控件:飞入飞出的效果
查看>>
自定义控件:动态获取控件的高
查看>>
第三方开源库:nineoldandroid:ValueAnimator 动态设置textview的高
查看>>
第三方SDK:百度地图SDK的使用
查看>>
Android studio_迁移Eclipse项目到Android studio
查看>>
JavaScript setTimeout() clearTimeout() 方法
查看>>
CSS border 属性及用border画各种图形
查看>>
转载知乎-前端汇总资源
查看>>