POJ 1337 A Lazy Worker(区间DP, 背包变形)
Description
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
Output
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, 背包变形)的更多相关文章
- POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)
题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思 ...
- POJ 3186Treats for the Cows(区间DP)
题目链接:http://poj.org/problem?id=3186 题目大意:给出的一系列的数字,可以看成一个双向队列,每次只能从队首或者队尾出队,第n个出队就拿这个数乘以n,最后将和加起来,求最 ...
- POJ 2955:Brackets(区间DP)
http://poj.org/problem?id=2955 题意:给出一串字符,求括号匹配的数最多是多少. 思路:区间DP. 对于每个枚举的区间边界,如果两边可以配对成括号,那么dp[i][j] = ...
- POJ 1191 棋盘分割(区间DP)题解
题意:中文题面 思路:不知道直接暴力枚举所有情况行不行... 我们可以把答案转化为 所以答案就是求xi2的最小值,那么我们可以直接用区间DP来写.设dp[x1][y1][x2][y2][k]为x1 y ...
- Poj 1651 Multiplication Puzzle(区间dp)
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10010 Accepted: ...
- POJ 1651 Multiplication Puzzle (区间DP,经典)
题意: 给出一个序列,共n个正整数,要求将区间[2,n-1]全部删去,只剩下a[1]和a[n],也就是一共需要删除n-2个数字,但是每次只能删除一个数字,且会获得该数字与其旁边两个数字的积的分数,问最 ...
- POJ 1141 Brackets Sequence (区间DP)
Description Let us define a regular brackets sequence in the following way: 1. Empty sequence is a r ...
- poj 2955 Brackets 括号匹配 区间dp
题意:最多有多少括号匹配 思路:区间dp,模板dp,区间合并. 对于a[j]来说: 刚開始的时候,转移方程为dp[i][j]=max(dp[i][j-1],dp[i][k-1]+dp[k][j-1]+ ...
- poj 1390 Blocks (经典区间dp 方块消除)
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4250 Accepted: 1704 Descriptio ...
随机推荐
- 命令运行带参数的jar
一.打包(此处用的是eclipse) 代码如下,此如引用了某博主的代码,因忘记地址,如博主发现此文,可私信我 package com.example.Open; import java.io.File ...
- python 列表的内容赋值
l1 = '20180201 b4b8e187-d59d-33fb-addc-ef189aca3712 com.ss.android.article.news' l2 = re.split('[ ]+ ...
- eclipse ant 的自动部署(autobulid)
在写项目中,经常需要在 ctrl+s的时候自动编译并且把这个改动的文件copy至某个目录,除了eclipse自带 java脚本的web自动部署到项目中配置的目录下,但是不能随心所欲copy,比如其他文 ...
- 【C++程序员学 python】python 之helloworld
我学习C语言之后才学的C++,所以这里写一个简单的helloworld程序. #coding:utf-8 def main(): print "hello world" if __ ...
- 配置信息写入到.ini文件中的方法
在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 一.将信息写入.INI文件中 1.所用的WINAP ...
- 有关一道printf 的面试题
今天有个学生面试,面试题目里面有一道有关 printf 的输出问题 源代码如下: #include <stdio.h> int main(void) { int a = 10, b = 2 ...
- strerror() 和perror()函数
在linux编程中,strerror()是个好东东,因为一个孤零零的errno看不出个所以然,然而strerror()返回的错误描述已经给我们解决问题提供了80%的成功率.但从安全性的角度来讲,str ...
- yarn是什么?为什么会产生yarn,它解决了什么问题?以及yarn的执行流程
yarn是什么?为什么会产生yarn,它解决了什么问题? 答:yarn是作业调度和集群资源管理的一个框架. 首先对之前的Hadoop 和 MRv1 简单介绍如下: Hadoop 集群可从单一节点 ...
- Oracle编译器警告
Compiler Warnings 编译器警告 Oracle 10g allows you to enable compile-time warnings that are useful to ide ...
- IE6、IE7、IE8、Firefox兼容性
整理关于IE6.IE7.IE8.Firefox兼容性CSS HACK问题 1.区别IE和非IE浏览器CSS HACK代码 #divcss5{background:blue; /*非IE 背景藍色*/b ...