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. Linux五种I/O模型性能分析

    转载自:http://blog.csdn.net/jay900323/article/details/18141217/ socket阻塞与非阻塞,同步与异步 作者:huangguisu 1. 概念理 ...

  2. JVM致命错误日志(hs_err_pid.log)分析(转载)

    当jvm出现致命错误时,会生成一个错误文件 hs_err_pid<pid>.log,其中包括了导致jvm crash的重要信息,可以通过分析该文件定位到导致crash的根源,从而改善以保证 ...

  3. hdu1429(bfs+状态压缩)

    思路:有十个门,有十把钥匙,每把钥匙对应一个门,相同的门可以有多个.这样,我们就得按照状态来搜索,用0000000001代表第一个门有钥匙了,1000000000代表第十个门钥匙有了.......一次 ...

  4. Java中数据库连接池原理机制的详细讲解

    连接池的基本工作原理 1.基本概念及原理 由上面的分析可以看出,问题的根源就在于对数据库连接资源的低效管理.我们知道,对于共享资源,有一个很著名的设计模式:资源池(Resource Pool).该模式 ...

  5. C++基础学习-20120518

    1---------printf中的%f会自动转化成double 2---------static_cast的应用实际上static_cast真正用处并不在指针和引用上,而在基础类型和对象的转换上 s ...

  6. 线程池 ManualResetEvent

    线程池: “线程池”是可以用来在后台执行多个任务的线程集合.(有关背景信息,请参见使用线程处理.)这使主线程可以自由地异步执行其他任务. 线程池通常用于服务器应用程序.每个传入请求都将分配给线程池中的 ...

  7. web应用中文乱码问题的原因分析

    为了让使用Java语言编写的程序能在各种语言的平台下运行,Java在其内部使用Unicode字符集来表示字符,这样就存在Unicode字符集和本地字符集进行转换的过程.当在Java中读取字符数据的时候 ...

  8. 信号值大于-75dBm时

    dBm是一个表示功率绝对值的值,是以1mw为0dbm,公式dbm=10×lg(毫瓦数/1).所以,为负值 这个值越大,表示信号越好,如-70dbm信号比-90dbm好  信号值大于-75dBm时,说明 ...

  9. Hive Tuning(四) 从查询计划看hive.auto.convert.join的好处

    今天我们来讲一下如何看懂Hive的查询计划. hive的执行计划包括三部分 – Abstract syntax tree – 可以直接忽略  – Stage dependencies – 依赖 – S ...

  10. SQL Server查询某个字段存在哪些表中

    一.查询SQL Server中所有的表 SQL语句:SELECT * FROM sys.tables name列表示所有的表名. 二.查询SQL Server中所有的列 SQL语句:SELECT * ...