这个题和求最长递增序列的题类似,为了能输出一组可行的数据,我还用了一点儿链表的知识。

Description

Peter decided to wish happy birthday to his friend from Australia and send him a card. To make his present more mysterious, he decided to make a chain. Chain here is such a sequence of envelopes A = {a1,  a2,  ...,  an}, where the width and the height of the i-th envelope is strictly higher than the width and the height of the (i  -  1)-th envelope respectively. Chain size is the number of envelopes in the chain.

Peter wants to make the chain of the maximum size from the envelopes he has, the chain should be such, that he'll be able to put a card into it. The card fits into the chain if its width and height is lower than the width and the height of the smallest envelope in the chain respectively. It's forbidden to turn the card and the envelopes.

Peter has very many envelopes and very little time, this hard task is entrusted to you.

Input

The first line contains integers nwh (1  ≤ n ≤ 5000, 1 ≤ w,  h  ≤ 106) — amount of envelopes Peter has, the card width and height respectively. Then there follow n lines, each of them contains two integer numbers wi and hi — width and height of the i-th envelope (1 ≤ wi,  hi ≤ 106).

Output

In the first line print the maximum chain size. In the second line print the numbers of the envelopes (separated by space), forming the required chain, starting with the number of the smallest envelope. Remember, please, that the card should fit into the smallest envelope. If the chain of maximum size is not unique, print any of the answers.

If the card does not fit into any of the envelopes, print number 0 in the single line.

Sample Input

Input
2 1 1  2 2  2 2
Output
1  1 
Input
3 3 3  5 4  12 11  9 8
Output
3  1 3 2 
 
#include<iostream>
using namespace std;
struct envelope
{
int num,w,h;
bool valid;
envelope*last;
};
int main()
{
int n,w,h,i,j;
cin>>n>>w>>h;
envelope *env=new envelope[n];
int *seq=new int[n];
for(i=0;i<n;i++)
seq[i]=1;
for(i=0;i<n;i++)
{
cin>>env[i].w>>env[i].h;
env[i].num=i+1;
env[i].valid=true; //表示信封是否可用
env[i].last=NULL; //能装入的上一个信封
}
for(i=0;i<n-1;i++) //先对信封进行排序;
{
int k=i;
for(j=i+1;j<n;j++)
{
if(env[k].w>env[j].w||env[k].w==env[k].w&&env[k].h>env[k].h)
k=j;
}
swap(env[k],env[i]);
}
for(i=0;i<n;i++) //判断信封是否可用
{
if(!(env[i].w>w&&env[i].h>h))
{
env[i].valid=false;
seq[i]=0;
}
}
for(i=0;i<n;i++) //找出第一个可用的信封
if(env[i].valid==true)
break;
int t=i;
for(i=t+1;i<n;i++)
{
if(seq[i]==0) //如果信封不可用,跳过。开始这个没写,老是在第16组数据WA
continue;
for(j=t;j<i;j++)
if(env[i].w>env[j].w&&env[i].h>env[j].h)
{
int tem=seq[j]+1;
if(tem>seq[i])
{
seq[i]=tem;
env[i].last=&env[j]; //记录上一个信封的地址
}
}
}
int max=0,index=0;
for(i=0;i<n;i++) //找出能嵌套的最多的信封个数及其 中一个最大的信封的下标
{
if(seq[i]>max)
{
max=seq[i];
index=i;
}
}
cout<<max<<endl;
if(max==0)
return 0;
else //寻找内层的信封,并将其序号记录在ans数组中
{
int *ans=new int[n];
envelope *p=&env[index];
for(i=max;i>0;i--)
{
ans[i]=p->num;
p=p->last;
}
for(i=1;i<=max;i++)
{
cout<<ans[i];
if(i!=max)
cout<<' ';
}
cout<<endl;
}
}

D - Mysterious Present的更多相关文章

  1. D. Mysterious Present (看到的一个神奇的DP,也可以说是dfs)

    D. Mysterious Present time limit per test 2 seconds memory limit per test 64 megabytes input standar ...

  2. Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present 记忆化搜索

    D. Mysterious Present 题目连接: http://www.codeforces.com/contest/4/problem/D Description Peter decided ...

  3. dp--C - Mysterious Present

    C - Mysterious Present Peter decided to wish happy birthday to his friend from Australia and send hi ...

  4. codeforces mysterious present 最长上升子序列+倒序打印路径

    link:http://codeforces.com/problemset/problem/4/D #include <iostream> #include <cstdio> ...

  5. Codeforces4D - Mysterious Present(LIS)

    题目大意 给你一张宽为w,长为h的的贺卡,然后给你n个信封,每个信封宽为wi,长为hi,问你最多能在贺卡上嵌套多少个信封,如果某个信封i如果能够装在信封j里,当且仅当w[i]<w[j]& ...

  6. Codeforces 4D Mysterious Present

    http://codeforces.com/contest/4/problem/D 题目大意: 给出n个信封,这n个信封有长和宽,给出卡片的尺寸,求取能够装入卡片的最长的序列,序列满足后一个的长和宽一 ...

  7. 【Codeforces 4D】Mysterious Present

    [链接] 我是链接,点我呀:) [题意] 要求长度和宽度都严格递增(选择一个序列) 然后你一开始有一个长度和宽度 要求这个一开始所给的长度和宽度能接在你选择的一段连续的长度宽度的开头 (且保持原来的性 ...

  8. Codeforces Beta Round #4 (Div. 2 Only) D. Mysterious Present(LIS)

    传送门 题意: 现在我们有 n 个信封,然后我们有一张卡片,并且我们知道这张卡片的长和宽. 现给出这 n 个信封的长和宽,我们想形成一个链,这条链的长度就是这条链中所含有的信封的数量: 但是需要满足① ...

  9. D. Mysterious Present DAG dp

    https://codeforces.com/problemset/problem/4/D 这个题目比较简单,就是一个DAG模型,这个可以看看紫书学习一下, 我这次是用dp来写的,用记忆化搜索也许更好 ...

随机推荐

  1. LINUX 硬盘命令

    1. 查看硬盘情况fdisk -l 每个Disk 为一个硬盘2. 挂在新硬盘fdisk /dev/sdb #硬盘地址Command (m for help):n #新建立分区Command actio ...

  2. MVC之视图的布局

      1. RenderBody  布局在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.在这个页面中,会看到标签里有这样一条语句 ...

  3. Java 集合系列 16 HashSet

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  4. java下载网络图片

    import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IO ...

  5. JS比较好用的一些方法搜集

    JS比较好用的一些方法搜集 Math.ceil(x) -- 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入 Math.floor(x)--返回小于等于数字参数的最大整数,对数字进行下舍入 ...

  6. Eclipse中SVN的安装步骤(两种)和使用方法 [转]

    一.给Eclipse安装SVN,最常见的有两种方式:手动方式和使用安装向导方式.具体步骤如下: 方式一:手动安装 1.从官网下载site-1.6.9.zip文件,网址是:subclipse.tigri ...

  7. 二分图 最大权匹配 km算法

    这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...

  8. 用于主题检测的临时日志(452a49c2-4455-430f-a1cc-bbcd2d1944dd - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    这是一个未删除的临时日志.请手动删除它.(95c74eab-5822-4f4b-b0e5-009feb9cae8d - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

  9. 可伸缩的textview。

    在一些应用中,比如腾讯的应用市场APP应用宝,关于某款应用的介绍文字,如果介绍文字过长,那么不是全部展现出来,而是显示三四行的开始部分(摘要),预知全部的内容,用户点击展开按钮即可查阅全部内容. 这样 ...

  10. 两天以来对plsqldev操作的记忆

    frist,开机后,打开服务,打开服务,打开服务(重要的事情说三遍). and then, 打开plsqldev,输入TEST账户而不是SYS账户,否则你会被许多未接触到的内容淹没你刚刚创建好的表. ...