HDU 1260 Tickets 简单DP

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1260

题目大意:小名早上八点整在电影业卖票,来了N个人买票。小明可以一次卖一张,也可以一次卖两张,但时间可能不同。现在给出N个人一次买一张票的时间以及i和i+1一次买票需要的时间,问最早小明什么时候能给这N个人卖完票?

解题思路:决策:一次处理一个人还是一次两个人都处理。那么我们可以设置两个状态:

dp[i][1]:=到第i个人的时候单独处理i所需的最短时间

dp[i][2]:=到第i个人的时候一块处理i和i-1所需的时间

状态转移方程:

dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + ti[i][1];
dp[i][2] = min(dp[i - 2][1], dp[i - 2][2]) + ti[i][2];

代码:

const int inf = 0x3f3f3f3f;
const int maxn = 2e3 + 5;
int n;
int ti[maxn][3];
int dp[maxn][3];

void solve(){
    memset(dp, 0x3f, sizeof(dp));
    dp[0][1] = dp[0][2] = 0;
    dp[1][1] = ti[1][1]; 
    for(int i = 2; i <= n; i++){
        dp[i][1] = min(dp[i - 1][1], dp[i - 1][2]) + ti[i][1];
        dp[i][2] = min(dp[i - 2][1], dp[i - 2][2]) + ti[i][2];
    }
    int ans = min(dp[n][1], dp[n][2]);
    int h = ans / 3600;
    ans %= 3600;
    int min = ans / 60;
    ans %= 60;
    if(h + 8 < 12) printf("%02d:%02d:%02d am\n", h + 8, min, ans);
    else printf("%02d:%02:%02d pm\n", h + 8, min, ans);
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &ti[i][1]);
        for(int i = 2; i <= n; i++) scanf("%d", &ti[i][2]);
        solve();
    }
}

题目:

Tickets

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


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
 

 

Source
posted @ 2017-08-15 16:28  EricJeffrey  阅读(154)  评论(0编辑  收藏  举报