背包变形。

将操作分为了两类,可以分开处理。

可以dp处理出L[i]:L[i]=-1代表从左到右 i 长度不能被拼凑出来,L[i]!=-1表示从左到右 i 长度能被拼凑出,并且最小费用为L[i]。

反着来一次dp就可以处理出R[i]:R[i]=-1代表从右到左 n+1-i 长度不能被拼凑出来,R[i]!=-1表示从右到左 n+1-i 长度能被拼凑出,并且最小费用为R[i]。

由L[1---i] 与R[i+1---n]可以得出,以i为分割的最优解,于是,枚举 i 即可。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<vector>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<stack>
  11. #include<iostream>
  12. using namespace std;
  13. typedef long long LL;
  14. const double pi=acos(-1.0),eps=1e-;
  15. void File()
  16. {
  17. freopen("D:\\in.txt","r",stdin);
  18. freopen("D:\\out.txt","w",stdout);
  19. }
  20. inline int read()
  21. {
  22. char c = getchar(); while(!isdigit(c)) c = getchar();
  23. int x = ;
  24. while(isdigit(c)) { x = x * + c - ''; c = getchar(); }
  25. return x;
  26. }
  27.  
  28. const int maxn=+;
  29. int T,n,m;
  30. struct X { int op,a,x; }s[maxn];
  31. bool cmp1(X a,const X b) { return a.a<b.a; }
  32. bool cmp2(X a,const X b) { return a.a>b.a; }
  33. int L[maxn],R[maxn];
  34.  
  35. int main()
  36. {
  37. scanf("%d",&T); int cas=; while(T--)
  38. {
  39. scanf("%d%d",&n,&m);
  40. for(int i=;i<=m;i++) scanf("%d%d%d",&s[i].op,&s[i].a,&s[i].x);
  41. sort(s+,s++m,cmp1);
  42.  
  43. memset(L,-,sizeof L); L[]=;
  44. for(int i=;i<=m;i++)
  45. {
  46. if(s[i].op!=) continue;
  47. for(int j=n;j>=;j--)
  48. {
  49. if(L[j]==-) continue; if(j+s[i].x>s[i].a) continue;
  50. if(L[j+s[i].x]==-) L[j+s[i].x]=L[j]+;
  51. else L[j+s[i].x]=min(L[j+s[i].x],L[j]+);
  52. }
  53. }
  54.  
  55. memset(R,-,sizeof R); R[n+]=;
  56. sort(s+,s++m,cmp2);
  57. for(int i=;i<=m;i++)
  58. {
  59. if(s[i].op!=) continue;
  60. for(int j=;j<=n+;j++)
  61. {
  62. if(R[j]==-) continue; if(j-s[i].x<s[i].a) continue;
  63. if(R[j-s[i].x]==-) R[j-s[i].x]=R[j]+;
  64. else R[j-s[i].x]=min(R[j-s[i].x],R[j]+);
  65. }
  66. }
  67. int ans1=,ans2=;
  68. for(int i=;i<=n;i++)
  69. {
  70. int a1=,a2=,b1=,b2=;
  71. for(int j=i;j>=;j--) { if(L[j]==-) continue; a1=j,a2=L[j]; break; }
  72. for(int j=i+;j<=n+;j++) { if(R[j]==-) continue; b1=n+-j,b2=R[j]; break; }
  73. if(a1+b1>ans1) ans1=a1+b1, ans2=a2+b2;
  74. else if(a1+b1==ans1) ans2=min(ans2,a2+b2);
  75. }
  76. printf("Case %d: %d %d\n",cas++,ans1,ans2);
  77. }
  78. return ;
  79. }

HDU 4381 Grid的更多相关文章

  1. hdu 4381(背包变形)

    题意: 给定n个块,编号从1到n,以及m个操作,初始时n个块是白色. 操作有2种形式: 1 ai xi : 从[1,ai]选xi个块,将这些块涂白. 2 ai xi:从[ai,n]选xi个块,将这些块 ...

  2. hdu 4255 A Famous Grid

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4255 A Famous Grid Description Mr. B has recently dis ...

  3. HDU - 1705 Count the grid

    昨天吉老师讲了皮克定理 皮克定理用于计算点阵中顶点在格点上的多边形面积.对于一个顶点全部在格点上的多边形来说,它的面积计算有如下特点: 如果用a表示位于多边形内部的格点数,b表示位于多边形边界上的格点 ...

  4. HDU 1892 See you~ (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1892 See you~ Problem Description Now I am leaving h ...

  5. Match:Milking Grid(二维KMP算法)(POJ 2185)

    奶牛矩阵 题目大意:给定一个矩阵,要你找到一个最小的矩阵,这个矩阵的无限扩充的矩阵包含着原来的矩阵 思路:乍一看这一题确实很那做,因为我们不知道最小矩阵的位置,但是仔细一想,如果我们能把矩阵都放在左上 ...

  6. HDU 1505 City Game (hdu1506 dp二维加强版)

    F - City Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submi ...

  7. HDU 2841 Visible Trees 数论+容斥原理

    H - Visible Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. HDU 1078 FatMouse and Cheese(记忆化搜索)

    FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. HDU 4069 Squiggly Sudoku(DLX)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4069 Problem Description Today we play a squiggly sud ...

随机推荐

  1. MyBatis-防止Sql注入以及sql中#{}与${}取参数的区别

    #{}能够更安全的取出参数 ${}取出的参数不安全 尽量不要使用${}取参数 原因: A:select * from table where a = '10001' and b = ${paramet ...

  2. laravel利用subquery使左连接查询右表数据唯一查询

    如:表a,连接表b,b中有多条符合查询的记录 1.建立需要的子查询 $sub = DB::table('b')->select(['aid'])->selectRaw('max(id) a ...

  3. 数据结构之线性表的顺序存储结构的实现--C语言版

    #include <stdio.h> #include <stdlib.h> #include <time.h> #define INIT_SIZE 100 #de ...

  4. js行内式遇到的一些问题 DOM对象和jq对象转换的问题

    这两天给后台页面做页面,我的工作比较简单,只需要写结构和样式就行了,写好之后,后端大哥用ajax重写页面加载数据,顺便给标签添加选中事件,做选中后变色的处理,但是却遇到一个问题,一直选不到触发事件这个 ...

  5. form表单直接传文件

    <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8" ...

  6. java default使用

    我们都知道在Java语言的接口中只能定义方法名,而不能包含方法的具体实现代码.接口中定义的方法必须在接口的非抽象子类中实现.下面就是关于接口的一个例子: public interface Simple ...

  7. 【Unity】Unity中C#与Android中Java的互相调用遇到的一些问题

    1.有关调用的一些问题: (1).在C#中直接调用java中的代码,无返回值: 在java中: public static void setAge(Context context , int leve ...

  8. hdu_5790_Prefix(trie+主席树)

    题目链接:hdu_5790_Prefix 题意: 给你n个字符串,字符串总长度不超过10W,然后给你一个区间,问你这个区间的字符串不相同的前缀有多少个. 题解: 由于z与上一个答案有关,所以强制在线, ...

  9. deb

    1.APT方式 (1)普通安装:apt-get install softname1 softname2 …; (2)修复安装:apt-get -f install softname1 softname ...

  10. JVM基础(3)-多态性实现机制

    一.方法解析 Class 文件的编译过程中不包含传统编译中的连接步骤,一切方法调用在 Class 文件里面存储的都只是符号引用,而不是方法在实际运行时内存布局中的入口地址. 因此,想要使用这些符号引用 ...