POJ1337---A Lazy Worker(dp)

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

Source
Taejon 2002

dp[i] 表示到時刻i時,最少的執行時間
然後暴力枚舉可以轉移到的工作
然而我們不需要擔心如果選入某個工作j,是否存在之前轉移過來的狀態裏已經有工作j,因爲某個工作可以執行的時間<2*t ,所以不會被選入第二次

/*************************************************************************
    > File Name: POJ1337.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年06月01日 星期一 21時50分17秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

int dp[330];
struct node {
    int ti, ai, di;
}Jobs[110];

int cmp(node a, node b) {
    return a.ai < b.ai;
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        int n, LongTime = 0;
        int ti, ai, di;
        scanf("%d", &n);
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d%d", &ti, &ai, &di);
            ++ai;
            ++di;
            Jobs[i].ti = ti;
            Jobs[i].ai = ai;
            Jobs[i].di = di;
            LongTime = max(LongTime, di);
        }
        sort(Jobs + 1, Jobs + 1 + n, cmp);
        memset(dp, inf, sizeof(dp));
        dp[0] = 0;
        for (int j = 0; j <= LongTime; ++j) {
            bool flag = 0;
            for (int i = 1; i <= n; ++i) {
                if (dp[j] == inf) {
                    continue;
                }
                if (j >= Jobs[i].ai && j + Jobs[i].ti <= Jobs[i].di) {
                    dp[j + Jobs[i].ti] = min(dp[j + Jobs[i].ti], dp[j] + Jobs[i].ti);
                    flag = 1;
                }
            }
            if (!flag) {
                dp[j + 1] = min(dp[j + 1], dp[j]);
            }
        }
        printf("%d\n", dp[LongTime]);
    }
    return 0;
}
發佈了700 篇原創文章 · 獲贊 5 · 訪問量 50萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章