A Lazy Worker - POJ 1337 dp

本文探讨了一位希望最小化工作量的工人如何在给定的任务集合中进行有效的工作安排。每个任务有自己的处理时间、到达时间和截止时间,且必须在其允许的时间段内完成。文章提供了一个算法解决方案,通过动态规划来确定工人工作的最短总时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A Lazy Worker
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1062 Accepted: 360

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

题意:一个工人在有活干的时候就尽量去干活,没活干的时候可以休息,然后每个工作都有持续时间,开始时间和结束时间,问他最少可以干多少天的活。

思路:这道题的题意应该这样理解,把工作开始和结束的天数当做一个点,每工作一天是从a到a+1之间的时间。然后dp[i]表示在第i天这个点的时候空闲的最少工作时间。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int op[110][3],vc[260][110],dp[260];
int main()
{
    int t,n,i,j,k;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;i<=250;i++)
        {
            vc[i][0]=0;
            dp[i]=10000;
        }
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&op[i][0],&op[i][1],&op[i][2]);
            op[i][2]=op[i][2]-op[i][0];
            for(j=op[i][1];j<=op[i][2];j++)
            {
                vc[j][0]++;
                vc[j][vc[j][0]]=i;
            }
        }
        dp[0]=0;
        for(i=0;i<=250;i++)
           if(dp[i]!=10000)
           {
               if(vc[i][0]==0)
                 dp[i+1]=min(dp[i+1],dp[i]);
               else
               {
                   for(j=1;j<=vc[i][0];j++)
                      dp[i+op[vc[i][j]][0]]=min(dp[i+op[vc[i][j]][0]],dp[i]+op[vc[i][j]][0]);
               }
           }
        printf("%d\n",dp[250]);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值