Description

There is a worker who may lack the motivation to perform at his peak level of efficiency because he is lazy. He wants to minimize the amount of work he does (he is Lazy, but he is subject to a constraint that he must be busy when there is work that he can do.)

We consider a set of jobs 1, 2,..., n having processing times t1, t2,...,tn respectively. Job i arrives at time ai and has its deadline at time di. We assume that ti, ai, and di have nonnegative integral values. The jobs have hard deadlines, meaning that each job i can only be executed during its allowed interval Ii=[ai, di]. The jobs are executed by the worker, and the worker executes only one job at a time. Once a job is begun, it must be completed without interruptions. When a job is completed, another job must begin immediately, if one exists to be executed. Otherwise, the worker is idle and begins executing a job as soon as one arrives. You should note that for each job i, the length of Ii, di - ai, is greater than or equal to ti, but less than 2*ti.

Write a program that finds the minimized total amount of time executed by the worker.

Input

The input consists of T test cases. The number of test cases (T ) is given in the first line of the input file. The number of jobs (0<=n<=100) is given in the first line of each test case, and the following n lines have each job's processing time(1<=ti<=20),arrival time(0<=ai<=250), and deadline time (1<=di<=250) as three integers.

Output

Print exactly one line for each test case. The output should contain the total amount of time spent working by the worker.

Sample Input

3
3
15 0 25
50 0 90
45 15 70
3
15 5 20
15 25 40
15 45 60
5
3 3 6
3 6 10
3 14 19
6 7 16
4 4 11

Sample Output

50
45
15

思路:

1. 搜索

2. DP. dp[i] 表示从时间 i 到 endtime 之间工作的最小值

  dp[i] = min(dp[i+t[j]]+t[j]),  t[j] 表示第 j 个任务的执行时间

3. 由(2) 的状态转移方程看, 需要计算在时刻 t 都有哪些任务可做, 时间复杂度 o(m*n) m 是 endtime, n 是 工作数, 且根据题意, endtime < 250, n < 100

总结:

1. 按照思路 (2) 的状态转移方程来做的话, 需要防止一个任务被重复计算两次. 一个直接的应对方法是再加一维, 那一维可通过状态压缩的方法表示那些任务已经被计算过了

2. 这段代码曾忘掉

if(!job[i].size()) { // 没有任务可做
  dp[i] = dp[i+1];
  continue;
}

代码:

WA 到死

#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 1010;
const int INF = 0X3F3F3F3F;
int t[MAXN], a[MAXN], d[MAXN];
vector<int> job[MAXN];
int n, endTime, startTime;
int dp[MAXN];
int cases;
void pre_process() {
for(int i = 0; i < MAXN; i ++) {
job[i].clear();
} for(int i = startTime; i <= endTime; i ++) {
for(int j = 1; j <= n; j ++) {
if(i >= a[j] && i+t[j] <= d[j])
job[i].push_back(j);
}
} memset(dp, 0x3f, sizeof(dp));
dp[endTime] = 0;
} int mainFunc() { for(int i = endTime-1; i >= startTime; i --) { if(!job[i].size()) { // 没有任务可做
dp[i] = dp[i+1];
continue;
}
dp[i] = INF;
for(int j = 0; j < job[i].size(); j ++) {
int curJob = job[i][j];
int ti = t[curJob];
dp[i] = min(dp[i], dp[i+ti]+ti);
}
}
return dp[startTime];
}
int main() {
freopen("E:\\Copy\\ACM\\poj\\1337\\in.txt", "r", stdin); cin >> cases;
while(cases-- >= 1) {
endTime = 0;
startTime = 1000;
cin >> n;
for(int i = 1; i <= n; i ++) {
scanf("%d%d%d", &t[i], &a[i], &d[i]);
endTime = max(endTime, d[i]);
startTime = min(startTime, a[i]);
}
pre_process();
// mainFunc
cout << mainFunc() << endl;
}
return 0;
}

  

update 2014年3月15日17:05:31

进行预处理之后, 这道题就变成了常见的朴素 01 背包, 比如 Leetcode wordbreak 什么的

POJ 1337 A Lazy Worker(区间DP, 背包变形)的更多相关文章

  1. POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

    题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...

  2. POJ 3186Treats for the Cows(区间DP)

    题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...

  3. POJ 2955:Brackets(区间DP)

    http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = ...

  4. POJ 1191 棋盘分割(区间DP)题解

    题意:中文题面 思路:不知道直接暴力枚举所有情况行不行... 我们可以把答案转化为 所以答案就是求xi2的最小值,那么我们可以直接用区间DP来写.设dp[x1][y1][x2][y2][k]为x1 y ...

  5. Poj 1651 Multiplication Puzzle(区间dp)

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10010   Accepted: ...

  6. POJ 1651 Multiplication Puzzle (区间DP,经典)

    题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...

  7. POJ 1141 Brackets Sequence (区间DP)

    Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...

  8. poj 2955 Brackets 括号匹配 区间dp

    题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...

  9. poj 1390 Blocks (经典区间dp 方块消除)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4250   Accepted: 1704 Descriptio ...

随机推荐

  1. solr学习之一 搜索基本知识

    学习了一段时间的solr了,用自己的方式总结下目前学到的内容,这是个系列文章,这里面的有些说法可能不准确,也可能有问题 欢迎大家指正. 一.搜索引擎目的 搜索引擎在我们的生活中,已经无处不在,除了我们 ...

  2. C++基础学习-20120514

    1------指针与引用的区别:1:非空区别.一个引用必须指向某个对象,必须初始化.但是指针可以赋空值,但给指针赋值之前必须制定指针的地址.变量不许为空时必须把变量赋给引用:2:合法性区别.引用使用之 ...

  3. 一款纯html5实现的人跑步动画

    今天给大家分享一款纯html5实现的人跑步动画.这款动画中实现了人跑步的动画,且上面有三个按钮,分别是选择让这个跑步的拿什么武器,第一个是拿了一把剑,第二个是拿了一把斧头,第三个是不拿任保东西.效果图 ...

  4. Mac为nginx安装nginx-sticky-module

    Mac为nginx安装nginx-sticky-module nginx版本: nginx-1.9.8 nginx-sticky-module版本:nginx-sticky-module-ng *注意 ...

  5. IOS中摇一摇实现截屏(可实现问题反馈的功能)

    有一段时间没有更新博客了,今天更新一篇关于最近工作中用到的一个功能,先简单描述一下:我们知道,测试人员在测试客户端产品时,当出现问题或者BUG的时候,都得先对页面截图,然后从相册中选择截图,加上一段描 ...

  6. stm32独立看门狗

    转载:http://blog.sina.com.cn/s/blog_7f1dc53b01010mqa.html 实验现象: 开始LED1亮,LED2熄灭,若不隔时间按KEY1则发现LED2因独立看门狗 ...

  7. etl获取转换文件名

  8. 实现对DataGird控件的绑定操作

    //实现对DataGird控件的绑定操作 function InitGrid(queryData) { $('#grid').datagrid({ //定位到Table标签,Table标签的ID是gr ...

  9. selenium测试(Java)--元素操作(五)

    元素的操作有 1. 清除文本 2. 模拟按键输入 3. 单击元素 4. 返回元素尺寸 5. 获取文本 6. 获取属性值 7. 判断是否可见 8. 提交 下面通过操作新浪邮箱的注册界面的脚本来展示使用方 ...

  10. Linux之查看文件大小和数目

    1.查看当前文件大小du -sh ./ du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-size][--exclude=< ...