题目链接: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");
}
}

最新文章

  1. 手把手教你玩转nginx负载均衡(二)----安装虚拟机操作系统
  2. UVALive-4839 HDU-3686 Traffic Real Time Query System 题解
  3. onclick标签变成小手状
  4. bootstrap-markdown编辑器引入
  5. Node.js使用fs.renameSync报cross-device link not permitted错误
  6. 安卓开发_慕课网_ViewPager与FragmentPagerAdapter实现Tab实现Tab(App主界面)
  7. 根据存放位置数据的链表P打印链表L的元素
  8. contest7.20(暴力专练)
  9. resumable.js —— 基于 HTML 5 File API 的文件上传组件 支持续传后台c#实现
  10. redis在spring中的配置及java代码实现
  11. Cordova 使用经验
  12. Oracle 修改序列的初始值
  13. Android studio - Failed to find target android-18
  14. Scrapy基础02
  15. GET &#39;https://dl.google.com/dl/android/maven2/com/android/tools/build/gradle/3.1.2/gradle-3
  16. Oracle EBS 查询客户报错 查询已超出 200 行。可能存在更多的行,请限制查询。
  17. python项目通过配置文件方式配置日志-logging
  18. AC日记——绿豆蛙的归宿 codevs 2488
  19. json教程系列(4)-optXXX方法的使用
  20. 2017.11.15 JavaWeb的学生体质管理系统

热门文章

  1. 【转】反向AJAX
  2. jquery怎么获得ul中li的个数
  3. Python+selenium(Autolt实现上传)
  4. NYOJ 239 月老的难题
  5. Leetcode 241.为运算表达式设计优先级
  6. Linux Notes:Linux下的远程登录协议及软件
  7. ASP.NET MVC WebAPI请求
  8. hihoCoder#1133 二分&#183;二分查找之k小数
  9. WKWebView的了解
  10. Springmvc 前台传递参数到后台需要数据绑定