Sweety

Practice makes perfect

导航

HDU 1260 Tickets(DP OR 记忆化搜索)

Posted on 2017-04-17 20:10  蓝空  阅读(241)  评论(0编辑  收藏  举报


esus, 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



#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define maxn 2100
#define oo 0x3f3f3f3f
using namespace std;
int a[maxn], b[maxn], dp[maxn];

int main()
{
    int T, n;

    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);

        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);

        for(int i=2; i<=n; i++)
            scanf("%d", &b[i]);

        if(n == 1)
        {
            printf("08:00:%02d am\n", a[1]);
            continue;
        }

        memset(dp, 0, sizeof(dp));
        dp[0]=0;
        dp[1]=a[1];

        for(int i=2; i<=n; i++)
         dp[i]=min(dp[i-1]+a[i], dp[i-2]+b[i]);

         int k = dp[n];

         int h = (k/3600+8)%24;
         int m = k%3600/60;
         int s = k%3600%60;

         if(h<12) printf("%02d:%02d:%02d am\n", h, m, s);
         else printf("%02d:%02d:%02d pm\n", h-12, m, s);   ///测试数据有多水,不减12都能过,去掉else都能过!!

    }
    return 0;
}




这个题有个地方需要注意,也许是要考虑生活实际吧。

就是即使搞定这个人之后,这个人和后面相连的这个时间就不能用了!

我艹,平时这个都是当做trick卡人的。

我一直以为自己的dp方程错了。还有这题不卡0点时候的输出。不用纠结。

附上我的2个版本的dp。只要改掉前面说的都能过。。。

原文链接:here

/*
    author:ray007great
    version:1.0
*/
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<queue>
using namespace std;
typedef long long ll;
#pragma comment(linker, "/STACK:102400000,102400000")
/*  define */

#define sf(a) scanf("%d",&a)
#define sfs(a) scanf("%s",a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repd(i,a,b) for(int i=(a);i>=(b);i--)
#define rep1(i,a,b) for(int i=(a);i<(b);i++)
#define clr(a) memset(a,0,sizeof(a))
#define clr1(a) memset(a,-1,sizeof(a))
#define pfk  printf("fuck\n")

/*  define */
int n;
int a[2500],adj[2500],ans;
const int inf = 8880000;
int dp[2500][2];
int dfs(int now,int kind){
    if(~dp[now][kind]) return dp[now][kind];
    if(kind==1 && now==n) return 0;
    else if(kind==0 && now==n) return a[n];
    int res=0;
    if(kind==1) res+=(dfs(now+1,0));
    else res+=min(dfs(now+1,1)+adj[now],dfs(now+1,0)+a[now]);
    return dp[now][kind]=res;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        clr1(dp);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<n;i++)
            scanf("%d",&adj[i]);
        if(n==1) ans=a[1];
        else ans=min(dfs(2,0)+a[1],dfs(2,1)+adj[1]);
        int mm,ss,hh;
        ss=ans%60;
        mm=ans/60%60;
        hh=ans/(60*60);
        hh=(8+hh)%12;
        if(hh<=12)
            printf("%02d:%02d:%02d am\n",hh,mm,ss);
        else
            printf("%02d:%02d:%02d pm\n",hh,mm,ss);
    }
    return 0;
}
/*
20
2
20 25
40
1
8
5
1 1 10 19 1
10 8 9 3
*/