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. 

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

题解:

状态转移方程:

dp[i] = min(dp[i-1]+one[i],dp[i-2]+two[i])

AC代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<map>
#define INF 0x3f3f3f3f
#define rep(i,a,n) for(int i=a;i<n;i++)
#define per(i,a,n) for(int i=n-1;i>=a;i--)
#define fori(x) for(int i=0;i<x;i++)
#define forj(x) for(int j=0;j<x;j++)
#define memset(x,y) memset(x,y,sizeof(x))
#define memcpy(x,y) memcpy(x,y,sizeof(y))
#define sca(x) scanf("%d", &x)
#define scas(x) scanf("%s",x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pris(x) printf("%s\n",x)
#define prl(x) printf("%lld\n",x)
//#include <bits/stdc++.h>

typedef long long ll;
const int maxn=1e6+7;
const int mod=1e9+7;
const double eps=1e-8;

using namespace std;

int one[maxn],two[maxn];
ll dp[maxn];
char time[2][5] = {"am","pm"};

void print(int temp)
{
    int s = temp%60;
    temp /= 60;
    int m = temp%60;
    temp /= 60;
    int h = 8 + temp;
    int flag = 0;
    if(h >12)
    {
      flag = 1;
      h %= 12;
    }
    printf("%02d:%02d:%02d %s\n",h,m,s,time[flag]);

}
int main()
{
    int t;
    sca(t);
    while(t--)
    {
      int n;
      sca(n);
      rep(i,0,n)
      {
        sca(one[i]);
      }
      rep(i,1,n)
      {
        sca(two[i]);
      }
      rep(i,0,n)
        dp[i] = INF;
      if(n==1)
        {
          print(one[0]);
          continue;
        }
      dp[0] = one[0];
      dp[1] = min(one[0]+one[1],two[1]);
      rep(i,2,n)
      {
        dp[i] = min(dp[i-1]+one[i],dp[i-2]+two[i]);
      }
      print(dp[n-1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Prince_NYing/article/details/88875897