zoukankan      html  css  js  c++  java
  • poj1065&1548 dp,最长上升子序列,偏序与反偏序

    题目描述:
      给定n个数对(xi,yi),对这些数对进行划分,保证划分出来的每一堆都是一条单调的链,问最少可以划分成多少堆
      原题如下:
    Wooden Sticks
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 26409   Accepted: 11423

    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

    3 
    5 
    4 9 5 2 2 1 3 5 1 4 
    3 
    2 2 1 1 2 2 
    3 
    1 3 2 2 3 1 
    

    Sample Output

    2
    1
    3

    Source

    poj1548原理相同,不放了
      首先我们要求单调链,把样例中的点画在坐标系中,我们便可看出,要求出单调链,要对数据排序(二维降一维)
      poj讨论区有较为清楚的描述(本人理解但是无法用语言描述)
    第一次贪心:其实并非每次都要求一个最长的序列,然后删除,只要选出的序列符合"极大"即可。这里的极大和函数的极值是一个概念。
    例如,(1,4) (4,4) (5,3) (9,4) 其中最大(最长)的一个序列为 (1,4)->(4,4)->(9,4)
    极大的序列 为 (5,3)->(9,4)
    证明:很简单,因为所有的木头都需要被加工,并且与加工的先后顺序无关,那么该木头一定
    需要在某个序列中,而我们每次选择的序列是极大的,故每次操作都没有遗漏可加入的木头,因此总的操作数是最少的。
     
      然后我们就要开始求单调链的数量,这里用到一个定理:(这个定理我不知道啊。。。QAQ)
      对于Dilworth定理,给出如下解释:
      一种Dilworth定理的等价表述是:在有穷偏序集中,任何反链最大元素数目等于任何将集合到链的划分中链的最小数目。一个关于无限偏序集的理论指出,在此种情况下,一个偏序集具有有限的宽度w,当且仅当它可以划分为最少w条链。
      可知,对于已经排序好的数据,上升单调链条数=下降单调链的最大长度(任何反链最大元素数目等于任何将集合到链的划分中链的最小数目)
      所以这道题最终转化成了求最长下降子序列的问题
      代码如下:
      
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 using namespace std;
     5 struct node
     6 {
     7     int l,w;
     8 }wood[10000];
     9 bool cmp(node a,node b)
    10 {
    11     if(a.l==b.l) return a.w<b.w;
    12     return a.l<b.l;
    13 }
    14 int dp[10000];
    15 int T,n;
    16 void solve()
    17 {
    18     for(int i=1;i<=n;i++)
    19     {
    20         dp[i]=1;
    21         for(int j=1;j<=i;j++)
    22         {
    23             if(wood[j].w>wood[i].w)
    24                 dp[i]=max(dp[i],dp[j]+1);
    25         }
    26         //printf("now:%d
    ",dp[i]);
    27     }
    28 }
    29 int main()
    30 {
    31     //freopen("in.txt","r",stdin);
    32     cin>>T;
    33     for(int i=1;i<=T;i++)
    34     {
    35         cin>>n;
    36         for(int j=1;j<=n;j++)
    37         {
    38             scanf("%d %d",&wood[j].l,&wood[j].w);
    39         }
    40         sort(wood+1,wood+1+n,cmp);
    41         //for(int j=1;j<=n;j++) printf("%d %d
    ",wood[j].l,wood[j].w);
    42         solve();int ans=0;
    43         for(int j=1;j<=n;j++) ans=max(ans,dp[j]);
    44         printf("%d
    ",ans);
    45     }
    46     return 0;
    47 }
    View Code
  • 相关阅读:
    Haproxy的安装与配置
    keepalived工作原理和配置说明
    服务器集群与负载均衡基础知识
    Linux磁盘分区与格式化
    第12章 在.NET中操作XML
    第16章 多线程
    第10章 网络编程
    第8章 流和序列化
    关于引用类型作为参数加上ref与不加ref的区别
    第3章 C#中的委托和事件
  • 原文地址:https://www.cnblogs.com/codeoosacm/p/10028326.html
Copyright © 2011-2022 走看看