博客
关于我
Tickets
阅读量:192 次
发布时间:2019-02-28

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

Joe需要尽早完成卖出所有电影票的任务。每位顾客可以单独买一张票或与旁边的顾客一起买两张票。目标是通过选择最优的购买方式,缩短总时间。

步骤解析:

  • 定义状态:设f[i]为卖出前i张票所需的最短时间。
  • 状态转移
    • 当第i张票单独买时,f[i] = f[i-1] + a[i]。
    • 当第i-1和i两张票一起买时,f[i] = f[i-2] + b[i-1]。
    • 取两种状态中的最小值。
  • 初始化
    • f[1] = a[1](只能单独买)。
    • f[2] = min(f[1] + a[2], b[1])。
  • 递推计算:从i=3到n,依次计算每个f[i]。
  • 时间转换:总时间转换为HH:MM:SS格式,根据每日8:00:00 am计算完成时间。
  • 代码实现:

    #include 
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    using namespace std;#define N 10005 // 最大N设为10005以防止溢出int main() { int t; // 测试用例数 cin >> t; while (t--) { int n; // 总人数 cin >> n; int a[N] = {0}; // 单独买一张的时间数组 int b[N] = {0}; // 买两张的时间数组 int f[N] = {0}; // 最短时间数组 for (int i = 1; i <= n; ++i) { cin >> a[i]; } for (int i = 1; i <= n - 1; ++i) { cin >> b[i]; } f[1] = a[1]; if (n >= 2) { f[2] = min(f[1] + a[2], b[1]); } for (int i = 3; i <= n; ++i) { f[i] = min(f[i-1] + a[i], f[i-2] + b[i-1]); } int total_time = f[n]; int h, m, s; h = (total_time / 3600) + 8; // 8点开始工作 total_time %= 3600; m = total_time / 60; s = total_time % 60; // 格式化输出时间 if (h < 10) { printf("0%d:", h); } else { printf("%d:", h); } if (m < 10) { printf("0%d:", m); } else { printf("%d:", m); } if (s < 10) { printf("0%d am\n", s); } else { printf("%d am\n", s); } } return 0;}

    运行结果:

    • 输入
      2220 254018
    • 输出
      08:00:40 am08:00:08 am

    代码解释:

    • 初始化:读取输入数据并初始化数组。
    • 递推计算:利用动态规划计算每个f[i]的最短时间。
    • 时间转换:将总时间转换为12小时制的HH:MM:SS格式,计算Joe的上班时间并输出结果。

    通过这种方法,Joe可以在最短的时间内完成所有票务工作,尽早回家。

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

    你可能感兴趣的文章
    Prometheus监控教程:使用PromQL查询监控数据(上篇)
    查看>>
    Prometheus监控教程:使用PromQL查询监控数据(下篇)
    查看>>
    Pytorch中关于forward函数的理解与用法
    查看>>
    Prometheus监控教程:安装部署
    查看>>
    Prometheus监控教程:配置介绍
    查看>>
    Pytorch中tqdm进度条的使用
    查看>>
    Prometheus(2):SpringBoot 2.X集成Prometheus
    查看>>
    Promise 原理解析与实现(遵循Promise/A+规范)
    查看>>
    PyTorch:传递 numpy 数组进行权重初始化
    查看>>
    PyTorch-Tutorials【pytorch官方教程中英文详解】- 8 Save and Load Model
    查看>>
    promise.all是并发执行吗_攻破面试灵魂拷问,解读Java并发编程的艺术,本文带你深入l理解...
    查看>>
    PyTorch-Tutorials【pytorch官方教程中英文详解】- 7 Optimization
    查看>>
    promise总结
    查看>>
    Propel项目改为基于TensorFlow.js
    查看>>
    PyTorch-Tutorials【pytorch官方教程中英文详解】- 6 Autograd
    查看>>
    properties出现中文乱码解决方法(万能)
    查看>>
    Property 'submit' of object #<HTMLFormElement> is not a function
    查看>>
    property--staticmethod--classmethod
    查看>>
    propertyGrid
    查看>>
    propertyPlaceholderConfigurer读取配置文件
    查看>>