HDU

来源:互联网 发布:网络直播社会现象 编辑:程序博客网 时间:2024/06/06 01:41

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 ai xi :You can choose any xi black boxes in interval [1,ai], and color them white;
2 ai xi :You can choose any xi black boxes in interval [ai,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 si(1<=si<=2) , ai and xi (0 <= xi <= N,1<=ai<=N), si indicating the type of this operation, ai and xiindicating that the interval is [1,ai] or [ai,n](depending on si), and you can choose xi 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
 

Author
WHU
 

Source
2012 Multi-University Training Contest 9 
 


题意:有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 longconst 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;}