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.
The first line contains integers n, w, h (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).
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.
2 1 1 2 2 2 2
1 1
3 3 3 5 4 12 11 9 8
3 1 3 2
先将给出的信封按长为关键字排序,之后找出最小能装下信的第一个信封。
从这个信封的位置开始进行操作,可得状态转移方程为f[i]=max(f[j])+1 j属于1~i;
路径的话可以开一个数组记录当前状态是由之前的哪一个状态转移过来的。
#include<stdio.h> #include<algorithm> using namespace std; struct p { int ww; int hh; int num; int stat; int co; }a[5005]; int record[5005]; bool cmp(p aa, p bb) { if (aa.ww==bb.ww) return aa.hh<bb.hh; else return aa.ww<bb.ww; } bool cmp2(p aa,p bb) { return aa.num<bb.num; } int main() { int n,w,h; int rec[5005]; int rec2[5005]; scanf("%d%d%d",&n,&w,&h); for (int i=0;i<n;i++) { scanf("%d%d",&a[i].ww,&a[i].hh); a[i].num=i; a[i].stat=1; a[i].co=-1; rec[i]=1; } sort(a,a+n,cmp); int pos=-1; int cou=0; for (int i=0;i<n;i++) { if (a[i].ww>w&&a[i].hh>h) {pos=i;break;} else a[i].stat=0; } int co=pos; for (int i=pos+1;i<n;i++) { int maxxn=1; for (int j=pos;j<i;j++) { if (a[i].ww>a[j].ww&&a[i].hh>a[j].hh&&a[j].ww>w&&a[j].hh>h) { if (a[j].stat+1>maxxn) {maxxn=a[j].stat+1;a[i].co=j;} } } a[i].stat=maxxn; rec[i]=co; } rec[pos]=pos; int ans=0; int r; for (int i=0;i<n;i++) { if (a[i].stat>ans) {ans=a[i].stat;r=i;} } if (pos==-1) printf("0\n"); else { printf("%d\n",ans); //for (int i=0;i<n;i++) printf("%d ",a[i].stat); //printf("\n"); rec[0]=r; while (a[r].co!=-1) { cou++; rec[cou]=a[r].co; r=a[r].co; } for (int i=cou;i>=0;i--) { printf("%d ",a[rec[i]].num+1); } } }