题目链接: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. 会跳舞的树(只用HTML+CSS)(转)

    效果如下. 共有1022个<div>元素. See the Pen wKvrMa by moyu (@MoYu1991) on CodePen.

  2. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  3. Go常量与枚举类型

    package main import ( "math" "fmt" ) //常量与枚举 //const数值可作为各种类型使用 func consts() { ...

  4. [luoguP1186] 玛丽卡(spfa)

    传送门 因为要随机删除一条边,而枚举所有边肯定会超时,经过发现,先求出一遍最短路,而要删除的边肯定在最短路径上,删除其他的边对最短路没有影响. 所以可以先求出最短路,再枚举删除最短路上的每一条边再求最 ...

  5. POJ 3620 Avoid The Lakes

    http://poj.org/problem?id=3620 DFS 从任意一个lake出发 重置联通的lake 并且记录 更新ans #include <iostream> #inclu ...

  6. 【2018 Multi-University Training Contest 5】

    01: 02:https://www.cnblogs.com/myx12345/p/9436953.html 03: 04: 05:https://www.cnblogs.com/myx12345/p ...

  7. Perforce share workspace between linux and windows

    p4 workspace 跨平台共享 (linux 和 window 共享) 用来存放代码的目录: linux存放代码目录: /home/username/ windows 上map network ...

  8. java学习(4)——动手动脑

    根据ppt所给的例子,运行的结果如下所示: ppt中出现的第二个动手动脑如下: 代码如下: 其运行结果如下: 作出简单的分析如下:有点类似于if 和else的关系,总是对应try和catch同层中的异 ...

  9. 分析Linux文件rwx属性的含义

    Linux上的文件以.开头的文件被系统视为隐藏文件,仅用ls命令是看不到他们的,而用ls -a除了显示 一般文件名外,连隐藏文件也会显示出来. ls -l(这个参数是字母L的小写,不是数字1) 这个命 ...

  10. SQL Server 2008 R2 安装时提示“Reporting Services目录数据库文件存在”

    打开MSSQL数据库管理系统的安装目录,例如: X:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA. 其中 X:\ ...