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

Tickets

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

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
 
 
题解:
 
 
原始做法:
1.dp[i][j][status]表示:第i个人属于第j个组合,他的状态是status(是跟前一个人组队还是自己买)的最少花费时间。
2.状态转移方程:
dp[i][j][] = dp[i-][j][] - a[i-] + b[i-];  //跟上一个人组队
dp[i][j][] = min(dp[i-][j-][], dp[i-][j-][]) + a[i]; //自己买
 
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int dp[MAXN][MAXN][], a[MAXN], b[MAXN]; int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++) scanf("%d", &a[i]);
for(int i = ; i<n; i++) scanf("%d", &b[i]); for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
dp[i][j][] = dp[i][j][] = INF/; dp[][][] = ;
for(int i = ; i<=n; i++)
for(int j = (i+)/; j<=i; j++) //最少应该属于第(i+1)/2个组合
{
dp[i][j][] = dp[i-][j][] - a[i-] + b[i-]; //跟上一个人组队
dp[i][j][] = min(dp[i-][j-][], dp[i-][j-][]) + a[i]; //自己买
} int time = INF;
for(int i = (n+)/; i<=n; i++)
time = min(time, min(dp[n][i][], dp[n][i][]) ); int second = time%;
int minute = (time/)%;
int hour = time/ + ; int id = ;
if(hour>){
id = ;
hour = hour-;
}
printf("%02d:%02d:%02d %s\n", hour, minute, second, id?"pm":"am");
}
}
 
 
改进:
1.后来发现规划第i个人属于第几个集合时多余的,所以: dp[i][status]表示:第i个人的状态是status的最少花费时间。
2.状态转移方程:
dp[i][] = dp[i-][] - a[i-] + b[i-];    //跟上一个人组队
dp[i][] = min(dp[i-][], dp[i-][]) + a[i]; //自己买
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int dp[MAXN][], a[MAXN], b[MAXN]; int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++) scanf("%d", &a[i]);
for(int i = ; i<n; i++) scanf("%d", &b[i]); for(int i = ; i<=n; i++)
dp[i][] = dp[i][] = INF/; dp[][] = ;
for(int i = ; i<=n; i++)
{
dp[i][] = dp[i-][] - a[i-] + b[i-];
dp[i][] = min(dp[i-][], dp[i-][]) + a[i];
}
int time = min(dp[n][], dp[n][]); int second = time%;
int minute = (time/)%;
int hour = time/ + ; int id = ;
if(hour>){
id = ;
hour = hour-;
}
printf("%02d:%02d:%02d %s\n", hour, minute, second, id?"pm":"am");
}
}
 
 
再改进:
1.再后来发现,前面两份代码都要用status来标记第i个人是否自己买,但是实际可以不用标记:如果第i个人自己买, 那么可以直接:dp[i] = dp[i-1] + a[i],如果跟别人组队买,那么 dp[i] = dp[i-2] + b[i-1],直接跳到前两个,免去了考虑前一个是否自己买。
2.状态转移方程:
dp[i] = min(dp[i-]+a[i], dp[i-]+b[i-]);
  
代码如下:
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 2e3+; int dp[MAXN], a[MAXN], b[MAXN]; int main()
{
int T, n;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i = ; i<=n; i++) scanf("%d", &a[i]);
for(int i = ; i<n; i++) scanf("%d", &b[i]); dp[] = ; dp[] = a[];
for(int i = ; i<=n; i++)
dp[i] = min(dp[i-]+a[i], dp[i-]+b[i-]); int second = dp[n]%;
int minute = (dp[n]/)%;
int hour = dp[n]/ + ; int id = ;
if(hour>){
id = ;
hour = hour-;
}
printf("%02d:%02d:%02d %s\n", hour, minute, second, id?"pm":"am");
}
}

HDU1260 Tickets —— DP的更多相关文章

  1. kuangbin专题十二 HDU1260 Tickets (dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. HDU-1260.Tickets(简单线性DP)

    本题大意:排队排票,每个人只能自己单独购买或者和后面的人一起购买,给出k个人单独购买和合买所花费的时间,让你计算出k个人总共花费的时间,然后再稍作处理就可得到答案,具体格式看题意. 本题思路:简单dp ...

  3. HDU1260(KB12-H DP)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  4. hdu1260(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 分析:简单dp,dp[i]=min(dp[i-1]+a[i],dp[i-2]); #includ ...

  5. H - Tickets dp

    题目链接: https://cn.vjudge.net/contest/68966#problem/H AC代码; #include<iostream> #include<strin ...

  6. HDU 1260 Tickets DP

    http://acm.hdu.edu.cn/showproblem.php?pid=1260 用dp[i]表示处理到第i个的时候用时最短. 那么每一个新的i,有两个选择,第一个就是自己不和前面的组队, ...

  7. 「kuangbin带你飞」专题十二 基础DP

    layout: post title: 「kuangbin带你飞」专题十二 基础DP author: "luowentaoaa" catalog: true tags: mathj ...

  8. 【HDU - 1260 】Tickets (简单dp)

    Tickets 搬中文 Descriptions: 现在有n个人要买电影票,如果知道每个人单独买票花费的时间,还有和前一个人一起买花费的时间,问最少花多长时间可以全部买完票. Input 给出 N(1 ...

  9. HDU 1260 Tickets(简单dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

随机推荐

  1. wps左侧显示目录

    单击视图----文档结构图,在下拉选项中选择靠左即可,如图所示

  2. Go map基础

    package main import "fmt" //Map //创建:make(map[string]int) //获取元素: m[key] //key不存在时,获得value ...

  3. CCF 201712-4 90分

    90分,不知道错在哪里了,dijkstra算法,用一个数组的d[i]表示以i点结尾的小路的长度,以i点为中心扩展时,若下一点为k,如果i->k是小路,则 d[j] = d[k]+M[k][j]; ...

  4. Couriers(bzoj 3524)

    Description 给一个长度为n的序列a.1≤a[i]≤n.m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. ...

  5. 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

    题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes onl ...

  6. 【转载】在Javascript中 声明时用"var"与不用"var"的区别

    原文链接:http://www.2cto.com/kf/201204/128406.html[侵删]   Javascript声明变量的时候,虽然用var关键字声明和不用关键字声明,很多时候运行并没有 ...

  7. Swift--错误集:Class controller has not initializers

    bug错误图 解决方法: 如下图所示,visitor这个属性并没有拆包处理,及将UIViewController的子类中的变量全部进行拆包处理,就是在变量声明的时候加一个?号,在使用的时候拆包处理,加 ...

  8. 洛谷—— P3372 【模板】线段树 1

    P3372 [模板]线段树 1 题目描述 如题,已知一个数列,你需要进行下面两种操作: 1.将某区间每一个数加上x 2.求出某区间每一个数的和 输入输出格式 输入格式: 第一行包含两个整数N.M,分别 ...

  9. Unique Paths II (dp题)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  10. Spring中实现自定义事件

    原理: 通过扩展ApplicationEvent,创建一个事件类CustomEvent.这个类必须定义一个默认的构造函数,它应该从ApplicationEvent类中继承的构造函数. 一旦定义事件类, ...