Description
There are n boxes in one line numbered 1 to n, at the beginning, all boxes are black. Two kinds of operations are provided to you:
1 a i x i :You can choose any x i black boxes in interval [1,a i], and color them white;
2 a i x i :You can choose any x i black boxes in interval [a i,n], and color them white;
lcq wants to know if she use these operations in optimal strategy, the maximum number of white boxes she can get, and if she get maximum white boxes, the minimum number of operations she should use.
Tips:
1. It is obvious that sometimes you can choose not to use some operations.
2. If the interval of one operation didn’t have enough black boxes, you can’t use this operation.
1 a i x i :You can choose any x i black boxes in interval [1,a i], and color them white;
2 a i x i :You can choose any x i black boxes in interval [a i,n], and color them white;
lcq wants to know if she use these operations in optimal strategy, the maximum number of white boxes she can get, and if she get maximum white boxes, the minimum number of operations she should use.
Tips:
1. It is obvious that sometimes you can choose not to use some operations.
2. If the interval of one operation didn’t have enough black boxes, you can’t use this operation.
Input
The first line contains one integer T, indicating the number of test case.
The first line of each test case contains two integers N (1 <= N <= 1000) and M (1<=M<=1000), indicating that there are N grids and M operations in total. Then M lines followed, each of which contains three integers s i(1<=s i<=2) , a i and x i (0 <= x i <= N,1<=a i<=N), si indicating the type of this operation, a i and x i indicating that the interval is [1,a i] or [a i,n](depending on s i), and you can choose x i black boxes and color them white.
The first line of each test case contains two integers N (1 <= N <= 1000) and M (1<=M<=1000), indicating that there are N grids and M operations in total. Then M lines followed, each of which contains three integers s i(1<=s i<=2) , a i and x i (0 <= x i <= N,1<=a i<=N), si indicating the type of this operation, a i and x i indicating that the interval is [1,a i] or [a i,n](depending on s i), and you can choose x i black boxes and color them white.
Output
For each test case, output case number first. Then output two integers, the first one is the maximum boxes she can get, the second one is the minimum operations she should use.
Sample Input
1 5 2 2 3 3 1 3 3
Sample Output
Case 1: 3 1
有两种操作,分开来考虑,1操作是从1开始的,如果我们根据ai从小到大排个序,那么这个问题可以看做是01背包。
我们可以求出L[i]来代表1到i都被填满的最少步数,同理右边也可以这么做。
然后只要枚举一下两边,看看最多能覆盖多少,并记录最小步数就好了。
#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
#define lson x << 1, l, mid
#define rson x << 1 | 1, mid + 1, r
using namespace std;
typedef long long LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF / 3;
const int mod = 1e9 + 7;
const int N = 1e3 + 10;
const int read()
{
char ch = getchar();
while (ch<'0' || ch>'9') ch = getchar();
int x = ch - '0';
while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';
return x;
}
int T, n, m, cas = 0;
int t[N], a[N], b[N], c[N], f[2][N];
bool cmp(int x, int y)
{
return t[x] != t[y] ? t[x] < t[y] : t[x] == 1 ? a[x]<a[y] : a[x] > a[y];
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
memset(f, 1, sizeof(f));
f[0][0] = f[1][n + 1] = 0;
rep(i, 1, m) scanf("%d%d%d", &t[i], &a[i], &b[i]), c[i] = i;
sort(c + 1, c + m + 1, cmp);
rep(j, 1, m)
{
int s = t[c[j]] - 1, x = a[c[j]], y = b[c[j]];
if (s == 0) per(i, x, y) f[s][i] = min(f[s][i], f[s][i - y] + 1);
if (s == 1) rep(i, x, n - y + 1) f[s][i] = min(f[s][i], f[s][i + y] + 1);
}
int x = 0, y = 0;
rep(i, 0, n)
{
if (f[0][i] > m) continue;
rep(j, i + 1, n + 1)
{
if (f[1][j] > m) continue;
if (x < i + n - j + 1)
{
x = i + n - j + 1;
y = f[0][i] + f[1][j];
}
else if (x == i + n - j + 1)
{
y = min(y, f[0][i] + f[1][j]);
}
}
}
printf("Case %d: %d %d\n", ++cas, x, y);
}
return 0;
}