HDU 4381 Grid
来源:互联网 发布:至尊妖孽软件下载 编辑:程序博客网 时间:2024/05/22 00:12
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<=ai<=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<=ai<=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
15 22 3 31 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, rusing 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;}
0 0
- HDU 4381 Grid
- HDU 4381 Grid 简单DP
- hdu 4381 Grid(背包 + 思维)
- HDU 4381 Grid【0-1背包】
- HDU 4255 A Famous Grid
- HDU 4255 A Famous Grid
- HDU 4255 A Famous Grid
- hdu 4255 A Famous Grid
- 【HDU 5471】Count the Grid
- hdu 1992 Tiling a Grid With Dominoes
- HDU 4225 A Famous Grid bfs
- HDU 4255A Famous Grid(裸BFS)
- hdu 1992 Tiling a Grid With Dominoes
- hdu 1992 Tiling a Grid With Dominoes
- hdu 4225 A Famous Grid(bfs)
- HDU 1992 Tiling a Grid With Dominoes
- HDU:4255 A Famous Grid(构造+BFS)
- HDU ACM 4255 A Famous Grid
- MySQL数据表列属性操作
- Linux命令之iconv
- php——读写文件操作
- STS配置Maven
- HDU 1004 Let the Balloon Rise
- HDU 4381 Grid
- java基础概念
- angular定义promise异步处理值方式(超简单)
- AlertDialog 攻略 自定义宽度,改变灰色背景透明度
- 暑期dp46道(23)HDOJ 1059 Dividing 多重背包+二进制优化
- 前方车辆检测的常用方法
- LeetCode_OJ【85】 Maximal Rectangle
- 回文串(Manacher)算法粗略讲解
- POJ 3984 迷宫问题