描述


http://poj.org/problem?id=1065

木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次).

Wooden Sticks
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21277   Accepted: 9030

Description

There is a pile of n wooden sticks. The length and weight of each stick are known in advance. The sticks are to be processed by a woodworking machine in one by one fashion. It needs some time, called setup time, for the machine to prepare processing a stick. The setup times are associated with cleaning operations and changing tools and shapes in the machine. The setup times of the woodworking machine are given as follows:
(a) The setup time for the first wooden stick is 1 minute.

(b) Right after processing a stick of length l and weight w ,
the machine will need no setup time for a stick of length l' and
weight w' if l <= l' and w <= w'. Otherwise, it will need 1
minute for setup.

You are to find the minimum setup time to process a given pile of n
wooden sticks. For example, if you have five sticks whose pairs of
length and weight are ( 9 , 4 ) , ( 2 , 5 ) , ( 1 , 2 ) , ( 5 , 3 ) ,
and ( 4 , 1 ) , then the minimum setup time should be 2 minutes since
there is a sequence of pairs ( 4 , 1 ) , ( 5 , 3 ) , ( 9 , 4 ) , ( 1
, 2 ) , ( 2 , 5 ) .

Input

The
input consists of T test cases. The number of test cases (T) is
given in the first line of the input file. Each test case consists of
two lines: The first line has an integer n , 1 <= n <=
5000 , that represents the number of wooden sticks in the test
case, and the second line contains 2n positive integers l1 , w1
, l2 , w2 ,..., ln , wn , each of magnitude at most 10000 ,
where li and wi are the length and weight of the i th wooden
stick, respectively. The 2n integers are delimited by one or more
spaces.

Output

The output should contain the minimum setup time in minutes, one per line.

Sample Input

  1. 3
  2. 5
  3. 4 9 5 2 2 1 3 5 1 4
  4. 3
  5. 2 2 1 1 2 2
  6. 3
  7. 1 3 2 2 3 1

Sample Output

  1. 2
  2. 1
  3. 3

Source

分析


神原理...

要求最少停多少次,就是要求单调不降的子序列的个数 x 最多为多少(每次停完都是一个单调不降的子序列),问题转化为求 x 的最小值.

我们现将木棍按照其中一种属性升序(不降)排序,这时另一种属性的最长下降子序列的长度记为 L .可以证明 x >=L.(鸽笼原理).

详细题解:

http://www.hankcs.com/program/cpp/poj-1065-wooden-sticks.html

注意:

1.二分的边界.在找满足 a [ k ] <= -1 的 k 的最小值时,可能 dp 数组中已经没有 -1 了,也就是 n 个位置全部被占满了,也就是整个序列就是一个下降序列,此时会找到 n 的位置,再 -1 答案就错误了,所以开始的时候将 1 ~ n + 1 都赋为 -1 ,之后 dp 时查找在 1 ~ n 查找,因为 dp 结束之前最多是 n - 1 个,不会把 dp 数组填满,数组中一定还有 -1 ,就一定存在满足 a [ k ] <= v (v>0) 的 k ,计算总长度时在 1~n+1 查找,确保有满足 a [ k ] <= -1 的 k .

  1. #include<cstdio>
  2. #include<algorithm>
  3. #define read(a) a=getnum()
  4. #define for1(i,a,n) for(int i=(a);i<=(n);i++)
  5. using namespace std;
  6.  
  7. const int maxn=;
  8. struct node {int l,w;}wood[maxn];
  9. int q,n;
  10. int dp[maxn];
  11.  
  12. inline int getnum(){ int r=,k=;char c;for(c=getchar();c<''||c>'';c=getchar()) if(c=='-') k=-;for(;c>=''&&c<='';c=getchar()) r=r*+c-''; return r*k; }
  13.  
  14. bool comp(node x,node y) { return x.l<y.l; }
  15.  
  16. int bsearch(int l,int r,int v)
  17. {
  18. while(l<r)
  19. {
  20. int m=l+(r-l)/;
  21. if(dp[m]<=v) r=m;
  22. else l=m+;
  23. }
  24. return l;
  25. }
  26.  
  27. void solve()
  28. {
  29. sort(wood+,wood+n+,comp);
  30. for1(i,,n+) dp[i]=-;
  31. for1(i,,n)
  32. {
  33. int idx=bsearch(,n,wood[i].w);
  34. dp[idx]=wood[i].w;
  35. }
  36. int ans=bsearch(,n+,-)-;
  37. printf("%d\n",ans);
  38. }
  39.  
  40. void init()
  41. {
  42. read(q);
  43. while(q--)
  44. {
  45. read(n);
  46. for1(i,,n) { read(wood[i].l); read(wood[i].w); }
  47. solve();
  48.  
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. #ifndef ONLINE_JUDGE
  55. freopen("wood.in","r",stdin);
  56. freopen("wood.out","w",stdout);
  57. #endif
  58. init();
  59. #ifndef ONLINE_JUDGE
  60. fclose(stdin);
  61. fclose(stdout);
  62. system("wood.out");
  63. #endif
  64. return ;
  65. }

POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)的更多相关文章

  1. HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场

    题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...

  2. Gym 100851G Generators (vector+鸽笼原理)

    Problem G. Generators Input file: generators.in Output file: generators.outLittle Roman is studying li ...

  3. 非 动态规划---LIS

    题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度.(见动态规划---LIS) /* 题目:一个序列有N个数:A[1],A[2],…,A[N],求出最长非降子序列的长度 ...

  4. poj 3370 鸽笼原理知识小结

    中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...

  5. poj 2356鸽笼原理水题

    关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...

  6. UVA 10620 - A Flea on a Chessboard(鸽笼原理)

    UVA 10620 - A Flea on a Chessboard 题目链接 题意:给定一个跳蚤位置和移动方向.如今在一个国际象棋棋盘上,左下角为黑格,一个格子为s*s,推断是否能移动到白格子.问要 ...

  7. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)

    题目链接  Points Inside A Polygon 题意  给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...

  8. 1393 0和1相等串 鸽笼原理 || 化简dp公式

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...

  9. Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

    To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...

随机推荐

  1. MVC中使用jquery的浏览器缓存问题

    jquery在浏览器ajax调用的时候,对缓存提供了很好的支持,POST方式不能被缓存,使用POST的原因,明确了数据不能被缓存,或者避免JSON攻击(JSON返回数据的时候可以被入侵) jquery ...

  2. java中collection、map、set、list简介 (转)

    Collection接口  Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements).一些Collection允许相同的元 ...

  3. 学习笔记_Java_day14—编码实战___一个注册页面的完整流程

  4. Oracle数据库导入imp命令导入时1659错误处理

    今天在自己的电脑上在给数据库导入表结构及数据时报1659错误,错误如下: IMP-00017:由于oracle错误1659,以下语句失败: “create table “T_TELETE” ..... ...

  5. select random item with weight 根据权重随机选出

    http://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/ 类似俄罗斯轮盘赌

  6. iOS开发——真机调试证书—发布证书

    (最近准备考试,空闲整理成博客……)

  7. xcode 最近打开文件列表显示为空或不显示最近打开的项目或(no recent projects)解决办法

    如果使用的是10.10 系统,打开系统设置-->进入通用-->在最下面的"最近使用的项目"中将0改为你可以接受的选项 如果不是10.10,那么就从系统偏好设置---&g ...

  8. swift-03-数据类型转换

    //  main.swift //  05-数据类型的转换 // //  Created by wanghy on 15/8/9. //  Copyright (c) 2015年 wanghy. Al ...

  9. map容器对象插入数据的4种方式

    #include <string> #include <iostream>  #include <map>  #include <utility>  u ...

  10. 解决UIScrollView 的点击事件

    目前有两种方法 第一种 通过 Category 扩展 UIScrollView 对象,添加触摸事件,(不建议,后续扩展不方便)代码如下 @implementation UIScrollView (Ex ...