1033. To Fill or Not to Fill (25)
来源:互联网 发布:淘宝卖家怎么加入花呗 编辑:程序博客网 时间:2024/06/04 19:45
1033. To Fill or Not to Fill (25)
时间限制10 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者ZHANG, GuochuanWith 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 86.00 12507.00 6007.00 1507.10 07.20 2007.50 4007.30 10006.85 300Sample Output 1:749.17Sample Input 2:50 1300 12 27.10 07.00 600Sample Output 2:The maximum travel distance = 1200.00油箱容量Cmax 要到距离的D的地方 每单位油能行Davg单位 这一路上有N个加油站
油单价 距离起始点
……
一开始没有油,所以距离出发点 0 一定要有加油站;
sort按照由近到远距离排序;
排除出发点没有加油站以后;
对于当前站点index来说,
如果在有油~满油的范围内,有站点,在这些加油站中
如果存在单价比当前少的站点中取最靠近当前站index的,那么当就只要加油到能够到达这一站choiceIndex就行;
否则在单价比当前多的站点中去单价最少的mIndex,那么考虑一下当前站加满能否到达终点,能,直接到达终点,
否则,加满油,到下一站mIndex
评测结果
时间 结果 得分 题目 语言 用时(ms) 内存(kB) 用户 8月03日 23:14答案正确251033C++ (g++ 4.7.2)1384datrilla测试点
测试点 结果 用时(ms) 内存(kB) 得分/满分 0答案正确130812/121答案正确11803/32答案正确12482/23答案正确11802/24答案正确13082/25答案正确11803/36答案正确13841/1
#include<iostream> #include<algorithm>#define NOFind -1#define NOprice -1.fusing namespace std;struct Stations{ float price; float distance;};bool STACmp(const Stations&A,const Stations&B){ return A.distance < B.distance;}void Starealn(Stations*Sta,int N){ for (int index = 0; index < N; index++) scanf("%f%f", &Sta[index].price, &Sta[index].distance); }void Drive(Stations*Sta, int N, float Cmax, float D, float Davg){ int index, mIndex, choiceIndex, i; bool Gameover= true; float Total, mPrice, oil, maxDrive; Total = 0; index = 0; maxDrive = Cmax*Davg; oil = 0; while (Gameover&& index <N)/*0~N-1有N个加油站,把最后一个N置为目的地*/ { choiceIndex = mIndex= NOFind; mPrice = NOprice; for (i = index + 1; i <= N&&Sta[i].distance - Sta[index].distance <= maxDrive; i++) { if (NOprice == mPrice || mPrice>Sta[i].price) { mPrice = Sta[i].price; mIndex = i; } if (NOFind == choiceIndex&&Sta[index].price > Sta[i].price) choiceIndex = i; } if (NOFind == mIndex) { Gameover = false;/*满油也到不了下一站咯,离终点无望了*/ } else if (NOFind != choiceIndex) { Total += ((Sta[choiceIndex].distance - Sta[index].distance) / Davg - oil)*Sta[index].price; oil = 0; index = choiceIndex;/*下一站choiceIndex的单价比当前index低,显然加够从index到choiceIndex的油就好*/ } else { if (Sta[index].distance + maxDrive < D) { Total += (Cmax - oil)*Sta[index].price; oil = Cmax - (Sta[mIndex].distance - Sta[index].distance) / Davg; index = mIndex;/*从index加油能走到的下面几站的单价最少的还是比较贵,而即使加满油也不能到达目的地,所以就要加满油*/ } else { Total += ((D - Sta[index].distance) / Davg - oil)*Sta[index].price; index = N;/*从index加满油能到达目的地,所以就要够到达目的地的油就好了*/ } } } Sta[0].price = Total; Sta[0].distance = Sta[index].distance == D ? D : Sta[index].distance + maxDrive; }int main(){ int N; float Cmax, D, Davg; Stations*Sta; scanf("%f%f%f%d", &Cmax, &D,&Davg,&N); Sta = new Stations[N+1]; Sta[N].price = 0; Sta[N].distance = D; Starealn(Sta, N); sort(Sta, Sta + N, STACmp); if (0 == Sta[0].distance) { Drive(Sta, N, Cmax, D, Davg); if (Sta[0].distance<D)printf("The maximum travel distance = %.2f\n",Sta[0].distance); else printf("%.2f\n", Sta[0].price); } else printf("The maximum travel distance = 0.00\n"); delete[]Sta; system("pause"); return 0;}
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- *1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill
- 1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)-PAT
- 【PAT】1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)
- PAT A 1033. To Fill or Not to Fill (25)
- PAT 1033. To Fill or Not to Fill (25)(贪心)
- 小白关于keil4调试仿真的几个问题:一直停在systeminit中的do,while;程序一直停在启动文件中的BX R0;void HardFault_Handler(void),
- CSS3 -webkit-transform(元素变换)
- hd2553 N皇后问题
- HDU 4975
- _DataStructure_C_Impl:顺序栈
- 1033. To Fill or Not to Fill (25)
- NOJ 2079 Prime (莫比乌斯反演)
- 应用洪峰应对规范
- UVA - 12532 Interval Product
- js中的内部属性与delete操作符
- _DataStructure_C_Impl:共享栈
- ROS学习--(十四)编写简单的Service和Client
- BootStrap导航的创建2
- shuoj-小6的多米诺骨牌-双向dp