ACM: 搜索题 poj 1…
2016-05-19 23:26
375 查看
A Lazy
Worker
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
题意: 有一位工人, 如果在当前时刻有工作他会执行, 不会休息, 但是他会偷懒, 现在又n个任务,
有执行花费时间t, 开始时间a[i], 最后期限d[i]; 每一项工作一旦被执行不可以被打断.
现在要求你算出工人最少的工作时间.(如果不满足条件的工作,
丢弃.)
解题思路:
1. 以时间为关键, 设g[i][j]: 在时刻i时, 满足a[j]>=i
&& t[j]+i<=d[j].
2. 将全部满足上述条件的[i]建立关系, 时间顺序搜索即可.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define MAX 255
const int INF = (1<<29);
int n;
int t[MAX], a[MAX], d[MAX];
vector g[MAX];
int result;
inline int min(int a, int b)
{
return a < b ? a : b;
}
void dfs(int curTime, int sumTime)
{
while(g[curTime].size() == 0
&& curTime <=
250)
curTime++;
if(curTime > 250)
{
result = min(result,
sumTime);
return ;
}
for(int i = 0; i <
g[curTime].size(); ++i)
{
int v = g[curTime][i];
dfs(curTime+t[v],
sumTime+t[v]);
}
}
int main()
{
int i, j;
// freopen("input.txt", "r", stdin);
int caseNum;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d",
&n);
for(i = 0; i <
MAX; ++i)
g[i].clear();
for(i = 1; i <=
n; ++i)
scanf("%d %d
%d", &t[i], &a[i],
&d[i]);
for(i = 0; i
< MAX; ++i)
{
for(j = 1; j
<= n; ++j)
{
if(i
>= a[j] && i+t[j]
<= d[j])
g[i].push_back(j);
}
}
result = INF;
dfs(0, 0);
printf("%d\n", result);
}
return 0;
}
Worker
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
题意: 有一位工人, 如果在当前时刻有工作他会执行, 不会休息, 但是他会偷懒, 现在又n个任务,
有执行花费时间t, 开始时间a[i], 最后期限d[i]; 每一项工作一旦被执行不可以被打断.
现在要求你算出工人最少的工作时间.(如果不满足条件的工作,
丢弃.)
解题思路:
1. 以时间为关键, 设g[i][j]: 在时刻i时, 满足a[j]>=i
&& t[j]+i<=d[j].
2. 将全部满足上述条件的[i]建立关系, 时间顺序搜索即可.
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
#define MAX 255
const int INF = (1<<29);
int n;
int t[MAX], a[MAX], d[MAX];
vector g[MAX];
int result;
inline int min(int a, int b)
{
return a < b ? a : b;
}
void dfs(int curTime, int sumTime)
{
while(g[curTime].size() == 0
&& curTime <=
250)
curTime++;
if(curTime > 250)
{
result = min(result,
sumTime);
return ;
}
for(int i = 0; i <
g[curTime].size(); ++i)
{
int v = g[curTime][i];
dfs(curTime+t[v],
sumTime+t[v]);
}
}
int main()
{
int i, j;
// freopen("input.txt", "r", stdin);
int caseNum;
scanf("%d", &caseNum);
while(caseNum--)
{
scanf("%d",
&n);
for(i = 0; i <
MAX; ++i)
g[i].clear();
for(i = 1; i <=
n; ++i)
scanf("%d %d
%d", &t[i], &a[i],
&d[i]);
for(i = 0; i
< MAX; ++i)
{
for(j = 1; j
<= n; ++j)
{
if(i
>= a[j] && i+t[j]
<= d[j])
g[i].push_back(j);
}
}
result = INF;
dfs(0, 0);
printf("%d\n", result);
}
return 0;
}
相关文章推荐
- ACM: 动态规划题 黑书-…
- ACM: 计算几何题 poj 2066
- ACM: 动态规划题 poj&nb…
- ACM: 华东师范oj 1600&n…
- ACM: 金华赛区题目 水题…
- ACM: 动态规划题 poj&nb…
- ACM: 动态规划题 《黑书…
- ACM: 动态规划题 sicily…
- ACM: 图论题 poj 1…
- ACM: 图论题 poj 1466 最大独立团
- ACM: 图论题 poj 1…
- 图论: 差分约束系统
- ACM: 图论题 poj 1275 差分约束题
- ACM: 图论题 poj 1236 强连通
- ACM: 图论题 poj 1149 网络流问题
- ACM: 图论题 poj 1201 差分约束
- ACM: 搜索题 poj 1020
- ACM: 图论题 poj 3308 最大流问题
- Android的自定义控件起步
- ACM: 图论题 poj 3177 同 3352