There are two machines A and B. There are n tasks, namely task 1, task 2, ..., task n. You must assign each task to one machine to process it. There are some facts you must know and comply with:
You must assign each task to only one machine to process.
At any time, a machine can process at most one task.
Task i (0 < i < n) can be processed if and only if each task j (0 < j < i) has been processed or processing.
If a task is processing on one machine, it cannot be interrupted.
You want to do finish all the tasks as soon as possible.
Input
There are multiple test cases. The first line of the input is an integer T (0 < T < 1000) indicating the number of test cases. Then T test cases follow. Each test case starts with an integer n (0 < n < 100). The ith line of the next n lines contains two integers tA, tB (0 < tA, tB < 100), giving the time to process the ith task by machine A and machine B.
Output
For each test case, output the earliest time when all the tasks have been processed.
Sample Input
4
1
1 2
2
1 2
2 1
2
1 2
90 95
3
1 3
1 3
1 3
Sample Output
1
1
90
3
Hints
Test case 1: Let machine A process task 1.
Test case 2: Let machine A process task 1 and at the same time let machine B process task 2.
Test case 3: Let machine B process task 1 and at the same time let machine A process task 2.
Test case 4: Let machine A process all the tasks.
一类经典的双塔dp
dp[i][j]表示做完前i项任务,A和B时间差为j时,完成任务的最短时间
j会为负,递推时加上偏移量
j>=0, 说明A要忙,如果第i+1项任务由A来做的话,那么B会空闲到直到A开始做第i+1项任务,不然B无法做新的任务,此时为:
dp[i+1][ta[i+1]]=dp[i][j]+ta[i+1];
如果把第i+1项任务交给B来做
dp[i+1][j?tb[i+1]]=dp[i][j]+max(tb[i+1]?j,0);
j<0 情况类似
/*************************************************************************
> File Name: ZOJ3331.cpp
> Author: ALex
> Mail: [email protected]
> Created Time: 2015年04月14日 星期二 16时14分15秒
************************************************************************/
#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;
const int N = 120;
int dp[N][2 * N];
int ta[N], tb[N];
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
{
scanf("%d%d", &ta[i], &tb[i]);
}
memset(dp, inf, sizeof(dp));
dp[0][100] = 0;
for (int i = 0; i < n; ++i)
{
for (int j = -100; j <= 100; ++j)
{
if (j >= 0)
{
dp[i + 1][100 + ta[i + 1]] = min(dp[i + 1][100 + ta[i + 1]], dp[i][j + 100] + ta[i + 1]);
dp[i + 1][100 + j - tb[i + 1]] = min(dp[i + 1][100 + j - tb[i + 1]], dp[i][j + 100] + max(tb[i + 1] - j, 0));
}
else
{
dp[i + 1][-tb[i + 1] + 100] = min(dp[i + 1][-tb[i + 1] + 100], dp[i][j + 100] + tb[i + 1]);
dp[i + 1][100 + j + ta[i + 1]] = min(dp[i + 1][100 + j + ta[i + 1]], dp[i][j + 100] + max(ta[i + 1] + j, 0));
}
}
}
int ans = inf;
for (int i = -100; i <= 100; ++i)
{
ans = min(ans, dp[n][i + 100]);
}
printf("%d\n", ans);
}
return 0;
}
时间: 2024-11-07 01:27:46