1. #include <cstdio>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <cmath>
  5. using namespace std;
  6. struct station {
  7. double per_price;
  8. double distance;
  9. };
  10. bool cmp(const station &s1, const station &s2) {
  11. return s1.distance < s2.distance;
  12. }
  13. int main() {
  14. double tank_c, distance, unit_d;
  15. int n;
  16. while (scanf(" %lf%lf%lf%d", &tank_c, &distance, &unit_d, &n) != EOF) {
  17. double cost = 0;
  18. double current_dis = 0;
  19. double current_tank = 0;
  20. double MAX_LEN = tank_c * unit_d;
  21. station stations[n + 1];
  22. for (int i = 0; i < n; ++i) {
  23. scanf(" %lf %lf", &stations[i].per_price, &stations[i].distance);
  24. }
  25. station temp = {0, distance};
  26. stations[n++] = temp;
  27. sort(stations, stations + n, cmp);
  28. if (stations[0].distance != 0) {
  29. printf("The maximum travel distance = 0.00\n");
  30. break;
  31. }
  32. //当前站点
  33. int k = 0;
  34. while (current_dis < distance) {
  35. int nextK = k;
  36. int min_k = k;
  37. double min_price = 999;
  38. double max_distance = MAX_LEN + current_dis;
  39. if (stations[k + 1].distance - current_dis > MAX_LEN) break;
  40. bool flag = false;
  41. for (int i = k + 1; i < n && stations[i].distance <= max_distance; ++i) {
  42. if (stations[i].per_price < stations[k].per_price) {
  43. flag = true;
  44. nextK = i;
  45. break;
  46. }
  47. if (stations[i].per_price < min_price) {
  48. min_price = stations[i].per_price;
  49. min_k = i;
  50. }
  51. }
  52. double left = current_tank * unit_d;
  53. //
  54. if (flag) {
  55. cost += ((stations[nextK].distance -stations[k].distance - left) / unit_d) * stations[k].per_price;
  56. k = nextK;
  57. current_dis = stations[nextK].distance;
  58. current_tank = 0;
  59. } else {
  60. //加满
  61. cost += ((MAX_LEN -left) / unit_d) * stations[k].per_price;
  62. current_dis = stations[min_k].distance;
  63. current_tank = tank_c - (stations[min_k].distance - stations[k].distance)/unit_d;
  64. k = min_k;
  65. }
  66. }
  67. if (current_dis == distance)
  68. printf("%.2lf\n", cost);
  69. else
  70. printf("The maximum travel distance = %.2lf\n", stations[k].distance + MAX_LEN);
  71. }
  72. return 0;
  73. }

问题 C: To Fill or Not to Fill的更多相关文章

  1. 1033. To Fill or Not to Fill (25)

     题目链接:http://www.patest.cn/contests/pat-a-practise/1033 题目: 1033. To Fill or Not to Fill (25) 时间限制 1 ...

  2. 1033 To Fill or Not to Fill

    PAT A 1033 To Fill or Not to Fill With highways available, driving a car from Hangzhou to any other ...

  3. 【贪心】PAT 1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  4. 1033 To Fill or Not to Fill (25 分)

    1033 To Fill or Not to Fill (25 分) With highways available, driving a car from Hangzhou to any other ...

  5. PAT甲级1033. To Fill or Not to Fill

    PAT甲级1033. To Fill or Not to Fill 题意: 有了高速公路,从杭州到任何其他城市开车很容易.但由于一辆汽车的坦克容量有限,我们不得不在不时地找到加油站.不同的加油站可能会 ...

  6. PAT 1033 To Fill or Not to Fill[dp]

    1033 To Fill or Not to Fill(25 分) With highways available, driving a car from Hangzhou to any other ...

  7. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  8. pat1033. To Fill or Not to Fill (25)

    1033. To Fill or Not to Fill (25) 时间限制 10 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 ZHANG, Gu ...

  9. PAT 甲级 1033 To Fill or Not to Fill (25 分)(贪心,误以为动态规划,忽视了油量问题)*

    1033 To Fill or Not to Fill (25 分)   With highways available, driving a car from Hangzhou to any oth ...

  10. PAT_A1033#To Fill or Not to Fill

    Source: PAT A1033 To Fill or Not to Fill (25 分) Description: With highways available, driving a car ...

随机推荐

  1. 14-Response

    今日知识 1. response 2. ServletContext对象 response * 功能:设置响应消息 1. 设置响应行 1. 格式:HTTP/1.1 200 ok 2. 设置状态码:se ...

  2. node -- express框架

    express node的一个框架 安装express cnpm install express -S 引入 const express = require("express"); ...

  3. 兄弟连 企业shell笔试题 16-31

    企业实践题16:企业案例:写网络服务独立进程模式下rsync的系统启动脚本 例如:/etc/init.d/rsyncd{start|stop|restart} .要求:1.要使用系统函数库技巧.2.要 ...

  4. 非对称加密 秘钥登录 https

    非对称加密简介: 对称加密算法在加密和解密时使用的是同一个秘钥:而非对称加密算法需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(public key,简称公钥)私有密钥(private key,简 ...

  5. 细读jsr中的yield语义,或者不是我们想象中的那样

    这只是我的个人观点,如有错误,希望你可以指出. 首先英文原版 中文译版 我觉得“不需要”还会让人产生误解,应该译为不一定要. 很多时候会被断章取义地理解,我们一定要有“不一定”,“可能”的意识,下面给 ...

  6. js将已有数组重新分组(将数组每10项分成一组)

    项目中碰到的一个小需求:分页请求数据,一次请求60条,需要将后台返回的数组每10条分成一组渲染一个表格(表格使用的是ant-design-vue的table) 实现逻辑: var chunk = 10 ...

  7. 「C++ 」借来的资源,何如还的潇洒?

    前言 本文的内容将专门对付内存管理,培养起有借有还的好习惯,方可消除资源管理的问题. 正文 所谓的资源就是,一旦用了它,将来必须还给系统.如果不是这样,糟糕的事情就会发生. C++ 程序内常见的资源: ...

  8. 在Django中连接MySQL数据库(Python3)

    我的环境:      python3.6,      Django2.1.5,      MySQL8.0.15,      win10,      PyCharm, 要求:已经安装了MySQL数据库 ...

  9. java循环示例

    用while循环计算100之内的奇数和偶数和 public class Test{ public static void main(String[] args){ int sum=0; int num ...

  10. css常用样式对文本的处理演练

    CSS文本属性可定义文本的外观,这是毫无疑问的,其次css可以通过以下属性改变文字的排版,比方说letter-spacing实现字符间距text-indent: 2em;完成首行缩进2字符word-s ...