HDU - 4381 - Grid(01背包)

Grid

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 971    Accepted Submission(s): 327


Problem 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.
 

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 iindicating 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
 

Author
WHU
 

Source
 


题意:有n个编号从1到n的箱子,m个操作,操作共有如下两种

1、1 ai xi 表示将在区间[1, ai]内的xi个黑箱子涂白

2、2 ai xi 表示将在区间[ai, n]内的xi个黑箱子涂白

当区间内的黑箱子的个数不足xi个时,不能执行操作


因为两个操作分别是从前往后和从后往前的连续区间,只有1操作时若[1,a]区间内最多有k个被涂白 则[1,k]一定可以被涂白,因为可以往前不能往后,只有2操作时只能往后不能往前

所以操作1和操作2是可以分开来的,即操作1涂白的箱子之间不会有操作2涂白的箱子,有的话就是重合了,这时候就只能二选一

要知道[1,n]最多能有几个被涂白 可以遍历 i ,区间为[1, i] 只用操作1最多可以涂白的数 + [i+1, n] 只用操作2最多可以涂白的数

我们可以用[1,k1]全涂白来代替 一段 a ≤ i ≤ b 区间内的最多可以涂白的数,操作2也是一样

然后就可以用从头开始全涂满的加上不重合的从后开始全涂满的个数来代替之前的遍历的操作

就变成了求前k个全涂白的最小次数和后k个全涂白的最小次数,就是利用01背包,要先拿范围小的,因为范围小的可以被范围大的给覆盖


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 1e3 + 10;
int T,n,m,s,a,x,dp[3][N],cas;
struct node{int s,a,x;}p[N];
bool cmp(node a,node b){return a.a<b.a;}
int main()
{
    for(scanf("%d",&T), cas = 0;T--;){
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) dp[1][i] = dp[2][i] = N;
        for(int i=1;i<=m;i++){
            scanf("%d%d%d",&p[i].s,&p[i].a,&p[i].x);
            if(p[i].s==2){
                p[i].a = n - p[i].a + 1;
            }
        }
        dp[1][0] = dp[2][0] = 0;
        sort(p+1,p+1+m,cmp);
        for(int i=1;i<=m;i++){
            for(int j=p[i].a;j>=p[i].x;j--){
                dp[p[i].s][j] = min(dp[p[i].s][j], dp[p[i].s][j-p[i].x]+1);
            }
        }
        int ans = 0, ti = 0, pos = n;
        for(int i=0;i<=n;i++){
            for(;pos>=0;pos--){
                if(dp[2][pos]<N){
                    if(ans<pos){
                        ans = pos; ti = dp[2][pos];
                    }else if(ans==pos && ti>dp[2][pos]){
                        ti = dp[2][pos];
                    }
                    if(pos+i>n) continue;
                    else break;
                }
            }
            if(dp[1][i]<N){
                if(ans<pos+i){
                    ans = pos+i; ti = dp[1][i] + dp[2][pos];
                }else if(ans==pos+i && ti > dp[1][i]+dp[2][pos]){
                    ti = dp[1][i] + dp[2][pos];
                }
            }
        }
        printf("Case %d: %d %d\n",++cas,ans,ti);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值