博客
关于我
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/

    你可能感兴趣的文章
    Oracle 11g数据库成功安装创建详细步骤
    查看>>
    Oracle 11g超详细安装步骤
    查看>>
    Oracle 12c中的MGMTDB
    查看>>
    Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)...
    查看>>
    Oracle 9i数据库管理教程
    查看>>
    ORACLE Active dataguard 一个latch: row cache objects BUG
    查看>>
    oracle avg、count、max、min、sum、having、any、all、nvl的用法
    查看>>
    Oracle BEQ方式连接配置
    查看>>
    oracle Blob保存方式,oracle 存储过程操作blob
    查看>>
    Oracle BMW Racing sailing vessel帆船图
    查看>>
    ORACLE Bug 4431215 引发的血案—原因分析篇
    查看>>
    Oracle Business Intelligence Downloads
    查看>>
    Oracle cmd乱码
    查看>>
    Oracle Corp甲骨文公司推出Oracle NoSQL数据库2.0版
    查看>>
    【Docker知识】将环境变量传递到容器
    查看>>
    uniapp超全user-agent判断 包括微信开发工具 hbuilder mac windows 安卓ios端及本地识别
    查看>>
    Oracle DBA课程系列笔记(20)
    查看>>
    oracle dblink 创建使用 垮库转移数据
    查看>>
    oracle dblink结合同义词的用法 PLS-00352:无法访问另一数据库
    查看>>
    Oracle dbms_job.submit参数错误导致问题(ora-12011 无法执行1作业)
    查看>>