HDU 1260 tickets (simple DP)

Source: Internet
Author: User

[Description ]:

Input:

2220 254018 
Output:


The data here is represented in sequence as follows: the first 2 represents two groups of data, and the second represents two people. If you buy a ticket, the first person will spend 20 s, the other person will spend 25 s. If the two buy together, it will take 40 s (note that the two buy together must be two matching talents ), because the question is how long is the shortest time, enter 40 s. The specific time is:

08:00:40 am
The other one will not go into details.

[Analysis]: for the shortest time and the optimal solution, we can easily establish a state transition equation:

DP [I] = min (DP [I-1] + SS [I], DP [I-2] + dd [I-1]) // represents the time spent on a single purchase, respectively, the other is the time it takes to buy with others.



Code, is others' high quality code: http://www.cnblogs.com/Griselda/archive/2013/06/05/3118892.html


#include <stdio.h>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 2010;int main(){    int T, n;    int d[MAXN], s[MAXN], dp[MAXN] = {0};    int hh, mm, ss;    scanf("%d", &T);    while (T--) {        scanf("%d", &n);        for (int i = 1; i <= n; ++i)            scanf("%d", &s[i]);        for (int i = 2; i <= n; ++i)            scanf("%d", &d[i]);        dp[1] = s[1];        for (int i = 2; i <= n; ++i)            dp[i] = min(dp[i-1]+s[i], dp[i-2]+d[i]);        hh = dp[n]/3600;        mm = dp[n]%3600/60;        ss = dp[n]%60;        printf("%02d:%02d:%02d%s\n", (8+hh)%24, mm, ss, (hh+8)%24>12?" pm":" am");    }    return 0;}


HDU 1260 tickets (simple DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.