zoukankan      html  css  js  c++  java
  • hdu-1260 tickets

    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. 

    InputThere 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. 
    OutputFor 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

    这题是道基础题,但让我对dp又有了更深的理解,所以记录一下。
    之前想这道题,想到是对每个人的状态进行判断,即两种情况,一是和上一个人组队,二是自己一个人排队。
    但是我想的是用dp数组记录每个数最佳的状态,但发现很难想,自己写试试,也写不下去。
    看了题解,发现题解都是用dp数组记录最小的时间和,设当前数为i,如果i上一个人组队为u,则dp[i]=dp[i-2]+nus[i];
    (nus数组记录的是两人组队的时间)如果i不和上一个人组队,自己单独排,则dp[i]=dp[i-1]+nu[i];
    这两种情况,选哪种取决于哪种时间最低。
    就是这么简单,我就没推出来 T T。
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 const int M = 2222;
     6 int nu[M],nus[M],dp[M];
     7 int main(){
     8     int n;
     9     scanf("%d",&n);
    10     while(n--){
    11         memset(dp,0,sizeof(dp));
    12         memset(nu,0,sizeof(nu));
    13         memset(nus,0,sizeof(nus));
    14         int k;
    15         scanf("%d",&k);
    16         for(int i=1;i<=k;i++){
    17             scanf("%d",&nu[i]);
    18         }
    19         for(int i=2;i<=k;i++){
    20             scanf("%d",&nus[i]);
    21         }
    22         dp[0]=0;dp[1]=nu[1];
    23         for(int i=2;i<=k;i++){
    24             dp[i]=min(dp[i-1]+nu[i],dp[i-2]+nus[i]);
    25         }
    26         int h=dp[k]/3600+8;
    27         int m=dp[k]/60%60;
    28         int s=dp[k]%60;
    29         printf("%02d:%02d:%02d am
    ",h,m,s);
    30     }
    31     return 0;
    32 }
    View Code


  • 相关阅读:
    UVA 679 Dropping Balls 由小见大,分析思考 二叉树放小球,开关翻转,小球最终落下叶子编号。
    2017-5-14 湘潭市赛 Similar Subsequence 分析+四维dp+一些简单优化
    Problem #3263 丽娃河的狼人传说 区间满足灯数,r排序后贪心。
    2017-5-14 湘潭市赛 Longest Common Subsequence 想法题
    2017-5-14 湘潭市赛 Parentheses 转化思想+贪心 使括号序列合法的最小花费。满足前面左括号的数量>=有括号的数量。
    deque双端队列用法
    Shell字符串截取
    keepAlived发生故障切换VIP—邮件通知方案2
    KeepAlived发生故障切换VIP—邮件通知方案1
    缺少依赖 libmysqlclient.so.18(64bit)的解决办法
  • 原文地址:https://www.cnblogs.com/zmin/p/7381130.html
Copyright © 2011-2022 走看看