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

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
  1. 2 1 1 2 2 2 2
Output
  1. 1 1
Input
  1. 3 3 3 5 4 12 11 9 8
Output
  1. 3 1 3 2
 
  1. #include<iostream>
  2. using namespace std;
  3. struct envelope
  4. {
  5. int num,w,h;
  6. bool valid;
  7. envelope*last;
  8. };
  9. int main()
  10. {
  11. int n,w,h,i,j;
  12. cin>>n>>w>>h;
  13. envelope *env=new envelope[n];
  14. int *seq=new int[n];
  15. for(i=0;i<n;i++)
  16. seq[i]=1;
  17. for(i=0;i<n;i++)
  18. {
  19. cin>>env[i].w>>env[i].h;
  20. env[i].num=i+1;
  21. env[i].valid=true; //表示信封是否可用
  22. env[i].last=NULL; //能装入的上一个信封
  23. }
  24. for(i=0;i<n-1;i++) //先对信封进行排序;
  25. {
  26. int k=i;
  27. for(j=i+1;j<n;j++)
  28. {
  29. if(env[k].w>env[j].w||env[k].w==env[k].w&&env[k].h>env[k].h)
  30. k=j;
  31. }
  32. swap(env[k],env[i]);
  33. }
  34. for(i=0;i<n;i++) //判断信封是否可用
  35. {
  36. if(!(env[i].w>w&&env[i].h>h))
  37. {
  38. env[i].valid=false;
  39. seq[i]=0;
  40. }
  41. }
  42. for(i=0;i<n;i++) //找出第一个可用的信封
  43. if(env[i].valid==true)
  44. break;
  45. int t=i;
  46. for(i=t+1;i<n;i++)
  47. {
  48. if(seq[i]==0) //如果信封不可用,跳过。开始这个没写,老是在第16组数据WA
  49. continue;
  50. for(j=t;j<i;j++)
  51. if(env[i].w>env[j].w&&env[i].h>env[j].h)
  52. {
  53. int tem=seq[j]+1;
  54. if(tem>seq[i])
  55. {
  56. seq[i]=tem;
  57. env[i].last=&env[j]; //记录上一个信封的地址
  58. }
  59. }
  60. }
  61. int max=0,index=0;
  62. for(i=0;i<n;i++) //找出能嵌套的最多的信封个数及其 中一个最大的信封的下标
  63. {
  64. if(seq[i]>max)
  65. {
  66. max=seq[i];
  67. index=i;
  68. }
  69. }
  70. cout<<max<<endl;
  71. if(max==0)
  72. return 0;
  73. else //寻找内层的信封,并将其序号记录在ans数组中
  74. {
  75. int *ans=new int[n];
  76. envelope *p=&env[index];
  77. for(i=max;i>0;i--)
  78. {
  79. ans[i]=p->num;
  80. p=p->last;
  81. }
  82. for(i=1;i<=max;i++)
  83. {
  84. cout<<ans[i];
  85. if(i!=max)
  86. cout<<' ';
  87. }
  88. cout<<endl;
  89. }
  90. }

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. QT-- MainWindow外的cpp文件调用ui

    这几天在学习QT,想写一个类似VIM的小软件,刚开始不注重代码结构,全部实现都写在MainWindow文件中,导致MianWindow文件十分的长而且很难去阅读,就想着把函数按照功能分到不同的cpp文 ...

  2. Scrum团队成立,阅读《构建之法》第6~7章,并参考以下链接,发布读后感、提出问题、并简要说明你对Scrum的理解

    Scrum团队成立:  团队名称:神的孩子 团队目标:短期目标,完成O2O模式的第一个平台 团队口号:我们都不是神的孩子 团队照: 角色分配 产品负责人: 许佳仪.决定开发内容和优先级排序,最大化产品 ...

  3. hdu-----(1113)Word Amalgamation(字符串排序)

    Word Amalgamation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. Sum Root to Leaf Numbers [LeetCode]

    Problem description: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ Basic idea: To store ...

  5. 张艾迪(创始人):发明Global.World.224C的天才

    Eidyzhang: Genius.Founder.CEO.23 I 世界级最高级创始人.世界最高级FounderCEO 出生在亚洲中国.Eidyzhang 拥有黑色头发白色皮肤(20岁)大学辍学生. ...

  6. 超详细的Xcode代码格式化教程,可自定义样式。

    超详细的Xcode代码格式化教程,可自定义样式. 为什么要格式化代码 当团队内有多人开发的时候,每个人写的代码格式都有自己的喜好,也可能会忙着写代码而忽略了格式的问题.在之前,我们可能会写完代码后,再 ...

  7. 百度 WebUploader 简单入门示例

    首先一定要引入:jquery.js 然后是webuploader的一个 css和还一个js 三个必须引用. <!DOCTYPE html> <html xmlns="htt ...

  8. 弹出消息对话框ScriptManager

    //直接调用WebMessageBox方法 #region 弹出消息对话框 /// <summary> /// 弹出消息对话框 /// </summary> /// <p ...

  9. Java 中equals和toString()方法重写

    1,equals方法 (1)什么时候需要重写? 如果希望不同内存但相同内容的两个对象equals时返回true,则需要重写equals (2)怎么重写? class A { public int i; ...

  10. C++构造函数和析构函数调用虚函数时都不会使用动态联编

    先看一个例子: #include <iostream> using namespace std; class A{ public: A() { show(); } virtual void ...