C - Mysterious Present

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.

Examples

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<bits/stdc++.h>
using namespace std; int n, w, h;
int maxn;//信封数
int p;//最小信封的下标
int listp[];
int dp[];
struct env{
int w, h, num;
}node[]; bool comp(env a,env b){
if(a.w != b.w)
return a.w > b.w;
return a.h > b.h;//信封从大到小排序
} int main(){
scanf("%d %d %d",&n, &w, &h); for(int i=; i<n; i++){
scanf("%d %d",&node[i].w, &node[i].h);
node[i].num = i+;//记录信封原下标
}
sort(node, node+n, comp);//信封按宽从小到大排序
for(int i=; i<n; i++){//初始化
dp[i] = ;
listp[i] = -;
}
maxn = ;
p = -; for(int i=; i<n; i++){
if(node[i].w>w && node[i].h>h ){//i可以放信
dp[i] = ;
if(p==-){
p = i;
maxn = ;
}
} if(dp[i] > ) {//i可以放信
for(int j=; j<i; j++){
if(node[j].w>node[i].w && node[j].h>node[i].h)//j可以放i
if(dp[i] < dp[j] + ){
dp[i] = dp[j] + ;
listp[i] = j;//i放进j
if(dp[i] > maxn){
maxn = dp[i];
p = i;
}
}
}
} }
printf("%d\n",maxn);
for(int i=p; maxn;){
if(i==-) break;
printf("%d ",node[i].num);
i = listp[i];
}
}

  代码不需要太多的注释,结构体node用来存放每个信封的宽和高以及它的初始位置(后用到排序,但又需要输出是第几个信封,便在结构体内标记),这里注意di一下写的comp函数,用来作为结构体排序的标准。此题难道我的是这个最长子序列到底如何记录,我最初的想法,就是再用一个数组list,list[i] = j,用来表示第node[i].num个信封可以放到第node[j].num个信封里,但由于我一开始定义的是从小到大的函数,所以信封也是从小指到大的;而在这类dp题中,我只能在最后找到那个最长子序列的时候,记录下最大信封的下标,而这个从大返回指小的过程,真的把我难倒了,我有尝试再用一个数组反向指,都失败了。但其实我后来的改进很简单,只要将这些信封,从大到小排序,从最大的,一直排到最小的,并且是能放入卡片的序列最长的信封,再配合一开始想到的list的信封值向信封的方法,最后可实现从小信封,输出到大信封


												

dp--C - 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. D. Mysterious Present DAG dp

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

  4. D - Mysterious Present

    这个题和求最长递增序列的题类似,为了能输出一组可行的数据,我还用了一点儿链表的知识. Description Peter decided to wish happy birthday to his f ...

  5. Codeforces4D - Mysterious Present(LIS)

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

  6. 【Codeforces 4D】Mysterious Present

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

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

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

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

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

  9. Codeforces 4D Mysterious Present

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

随机推荐

  1. Case Study - 预测肺癌

    Problem 肺癌是发病率和死亡率增长最快,对人群健康和生命威胁最大的恶性肿瘤之一.近50年来许多国家都报道肺癌的发病率和死亡率均明显增高,男性肺癌发病率和死亡率均占所有恶性肿瘤的第一位,女性发病率 ...

  2. AI数据标注行业面临的5大发展困局丨曼孚科技

    根据艾瑞咨询发布的行业白皮书显示,2018年中国人工智能基础数据服务市场规模为25.86亿元,预计2025年市场规模将突破113亿元,行业年复合增长率达到了23.5%.​ 作为人工智能产业的基石,数据 ...

  3. pyqt5-字体,颜色选择对话框设置label标签字体颜色样式

    1.采用实例方法,先创建2个dialog对象,采用该对象的信号触发相应的操作 import sys from PyQt5.Qt import * class MyWidget(QWidget): de ...

  4. 安卓平台SQLite数据库基础操作总结

    最近学了一些安卓开发,在这里分享一下SQLite数据库的使用相关部分,我使用的工具为Android Studio,后台语言为java: 首先,需要创建一个数据库辅助类DataBaseHelper,用于 ...

  5. openlayers编辑区域

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  6. mybatis及其与hibernate的对比

    1.mybatis是支持普通SQL查询.存储过程.和高级映射的优秀持久层框架 它消除了JDBC代码和参数的手工设置,以及对结果集的检索 2.mybatis使用简单的XML或注解用于配置和原始映射,将接 ...

  7. 【你不知道的javaScript 中卷 笔记1】javaScript中的类型与值

    一.类型与值 1.0 javaScript 七种内置类型: 空值(null) 未定义(undefined) 布尔值( boolean) 数字(number) 字符串(string) 对象(object ...

  8. 题解【POJ1062】昂贵的聘礼

    题面 比较复杂的最短路模型转换. 我们考虑一种建图方式: 建立一个超级源点 \(S\),它向每一个节点连一条权值为那一个节点物品价值的边,表示直接购买那一个物品: 对于每一个节点,由它每一个可用的替代 ...

  9. 1级搭建类103-Oracle 12c 单实例 FS(12.2.0.1+RHEL 7)公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  10. 【笔记0-开篇】面试官系统精讲Java源码及大厂真题

    背景 开始阅读 Java 源码的契机,还是在第一年换工作的时候,被大厂的技术面虐的体无完肤,后来总结大厂的面试套路,发现很喜欢问 Java 底层实现,即 Java 源码,于是我花了半年时间,啃下了 J ...