HDU 1257 最少拦截系统 (Dilworth定理)

今天做的这一题花了我不少时间,后来才知道用Dilworth定理做其实很简单,,,简直吐血。

下面上原题

U - 最少拦截系统

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Input

Output

Sample Input

Sample Output

Hint

Description

A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, ..., xm> another sequence Z = <z1, z2, ..., zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.

Input

abcfbc abfcab
programming contest 
abcd mnp

Output

4
2
0

Sample Input

abcfbc abfcab
programming contest 
abcd mnp

Sample Output

4
2
0

Hint

Description

某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.

Input

输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)

Output

对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.

Sample Input

8 389 207 155 300 299 170 158 65

Sample Output

2

思路:刚开始以为只要一遍一遍的找最长非递增数列就行了,第一遍找出来最长的,然后把这一列里面的数字去掉,在剩下来的里面继续前面的操作就行了,直到所有元素都被去掉,然后发现碰到这种情况就不行了

9 5 1 8 4 2 第一次去掉9 5 4 2, 然后去掉1,最后去掉8,就是说要三次,其实只要两次 :第一次9 5 1 ,第二次8 4 2;

到网上一查发现有这个定理:

Dilworth定理:

<span style="margin: 0px; padding: 0px; font-size: 14pt; font-family: 楷体; color: rgb(255, 0, 0);">令(X,≤)是一个有限偏序集,并令m是反链的最大的大小。则X可以被划分成m个但不能再少的链。</span>
具体可以看 Dilworth定理链接

然后题目就变成了求最长上升子序列:

ac 代码:

//Li Wenjun
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
#include <algorithm>
using namespace std;
int N,sum=0;
int a[200010],dp[200010];
int main()
{
    cin.tie(0);
    cin.sync_with_stdio(false);
   // freopen("in.txt","r",stdin);
    while(cin>>N)
    {
      //  memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        int numbs=0;
        for(int i=1;i<=N;i++)
        {
            cin>>a[i];
        }
        for(int i=1;i<=N;i++)
        {
            for(int j=i-1;j>=0;j--)
            {
                if(a[i]>a[j])
                {
                    dp[i]=max(dp[i],dp[j]+1);
                }
            }
            if(numbs<dp[i])
                numbs=dp[i];
        }
        cout<<numbs<<endl;
    }
    return 0;
}

OK





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值