CodeForces 4D. Mysterious Present

D. Mysterious Present
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output

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 ≤ 50001 ≤ 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 test(s)
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 

1、对所有数据排序,w优先排序(从小到大),h辅助排序(从大到小)。//特别注意 h 不能从小到大排序,否则求出来的数列不满足 w 严格递增

2、用求最长上升子序列的方法更新二维数组 ans ,同时在 ans[i][j] 的pre指向该数列的上一个元素。//之所以用二维数组,是因为此题不仅要求长度,还要求最终的数列

3、有了指针指向就好办了,直接从ans最后一列中随便选一个元素往前推就行了。


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;
struct D{
    D *pre;
    int w,h,num;
};
bool operator < (D a,D b){
    if(a.w!=b.w) return a.w<b.w;
    else return a.h>b.h;
}
D data[5004];
D ans[5004][500];
int res[5004];
int sum[5004];
int main(){
    int n,w0,h0;
    while(cin>>n>>w0>>h0){
        for(int i=0;i<n;i++){
            cin>>data[i].w>>data[i].h;
            data[i].num=i+1;
        }
        sort(data,data+n);
        memset(sum,0,sizeof(sum));
        int s=0;
        while(!(w0<data[s].w&&h0<data[s].h)) {s++;if(s==n) break; }
        if(s==n) {printf("0\n");continue;}

        ans[0][0] = data[s];
        ans[0][0].pre = NULL;
        sum[0]=1;
        int ansCur=1;

        for(int i=s+1;i<n;i++){
            if(w0>=data[i].w||h0>=data[i].h) continue;
            if(data[i].h>ans[sum[ansCur-1]-1][ansCur-1].h) {
                ans[0][ansCur] = data[i];
                ans[0][ansCur].pre = &ans[sum[ansCur-1]-1][ansCur-1];
                sum[ansCur]=1;
                ansCur++;
            }
            else {
                int L=0,R=ansCur-1,Line;
                while(L<=R){
                    int mid = (L+R)/2;
                    Line = sum[mid]-1;
                    if(ans[Line][mid].h==data[i].h)  {R=-2;break;}
                    else if(ans[Line][mid].h>data[i].h) R = mid-1;
                    else if(ans[Line][mid].h<data[i].h) L = mid+1;
                }
                if(R==-2) continue;
                ans[sum[R+1]][R+1] = data[i];

                if(R>-1) ans[sum[R+1]][R+1].pre = &ans[sum[R]-1][R];
                else ans[sum[R+1]][R+1].pre = NULL;
                sum[R+1]++;
            }
        }
        printf("%d\n",ansCur);
        D* T = &ans[sum[ansCur-1]-1][ansCur-1];
        int cur=0;
        while(T!=NULL){
            res[cur++]=T->num;
            T=T->pre;
        }
        for(int i=cur-1;i>=0;i--){
            printf("%d ",res[i]);
        }
        printf("\n");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值