• 【动态规划】 HDU1260


    Tickets

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 923    Accepted Submission(s): 467


    Problem Description

    Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.
    A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time.
    Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help.
     

    Input
    There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:
    1) An integer K(1<=K<=2000) representing the total number of people;
    2) K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;
    3) (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.
     

    Output
    For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm.
     

    Sample Input
    2

    2

    20 25

    40

    1

    8
     

    Sample Output
    08:00:40 am

    08:00:08 am

    简单一维动规题。大概意思是k个人买票,可以一个人买,也可以相邻的两个人一起买。现在给出每个人单独买票的时间和每两个相邻的人买票的时间,求最少买票时间。

    思路:

    用one[i]表示第i个人单独买票的时间,two[i]表示第i个人和他前面的人(第i-1个人)一起买票的时间。

    关键就是写出状态方程:dp[i] = min(dp[i-1] + one[i], dp[i-2] + two[i])

    下面给出AC代码,有参考

     1 #include <iostream>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 
     7 int main(){
     8     int n,k;
     9     int one[2001],two[2001],dp[2001];
    10     cin>>n;  //有几组
    11     while(n--){
    12         memset(one,0,sizeof(one));  //每次开始新的一轮时初始化
    13         memset(two,0,sizeof(two));
    14         memset(dp,0,sizeof(dp));
    15         cin>>k;  //总人数
    16         for(int i=1;i<=k;i++)
    17             cin>>one[i];
    18         for(int i=2;i<=k;i++)  //如果只有一个人买票,则不用输入two[i],此处巧妙!
    19             cin>>two[i];
    20         dp[0] = 0,dp[1] = one[1];   //给dp[0]赋值0,则可直接从dp[2]开始循环,此处巧妙!
    21         for(int i=2;i<=k;++i)
    22             dp[i] = min(dp[i-1]+one[i],dp[i-2]+two[i]);
    23         int h = dp[k]/3600 +8;
    24         int m = dp[k]/60 % 60;
    25         int s = dp[k]%60;
    26         if(h<12)
    27             printf("%02d:%02d:%02d am
    ",h,m,s);
    28         else
    29             printf("%02d:%02d:%02d pm
    ",h,m,s);
    30     }
    31 
    32     return 0;
    33 }
  • 相关阅读:
    python自省函数getattr的用法
    python-mysqldb安装
    weblogic部署脚本
    netcat使用
    ssh批量互信脚本
    yum安装出错
    centos文件误删除恢复
    lamp php的ssl,ssh支持
    ssh免密码登陆
    python 学习笔记 四 条件, 循环以及其他
  • 原文地址:https://www.cnblogs.com/Aikoin/p/10080029.html
Copyright © 2020-2023  润新知