Codeforces 4D Mysterious Present 求数对的最长上升序列 dp

D. Mysterious Present
time limit per test
2 seconds
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 


题目大意:

和之前做过的一道poj题目类似。

给给一个数n,和一个标准的数对w,h

接下来是n个数对w,h

要求求出最长的严格上升数对长度,和这个序列,且这个数列的第一个数对严格大于标准数对


解题思路:

非常好想,先按照第一个数排序,然后对排序后的数对的第二个数走一遍dp


反省:

WA了好几次。

主要是因为

(1)没有考虑, 由于标准数对的存在所产生的,诸如所有没有合法数对等错误的边界情况。

(2)dp中没有对w进行判断(题目要求h和w都是严格上升,原来以为只要对w排过序就不用管w了)

(3)其他错误



#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#define maxn 5005

using namespace std;
struct P{
    int w,h;
    int index;
}env[maxn];
int n,w,h;
int dp[maxn];
int pre[maxn];
int ans[maxn];

int cmp(P a,P b)
{
    return a.w<b.w;
}

int main()
{
    scanf("%d %d %d",&n,&w,&h);
    int trueN=0,ans1=0;
    for(int i=0;i<n;i+=1){
        int tmpw,tmph;
        scanf("%d %d",&tmpw,&tmph);
        if(tmpw>w&&tmph>h){
            env[trueN].w=tmpw;
            env[trueN].h=tmph;
            env[trueN].index=i+1; //这里的i曾曾经写成了trueN
            ans1=i+1;
            trueN+=1;
        }
    }
    if(!trueN){
        //这里漏讨论
        printf("0\n");
        return 0;
    }
    else if(trueN==1){
        //这里漏讨论
        printf("1\n%d\n",ans1);
        return 0;
    }

    sort(env,env+trueN,cmp);
    memset(dp,0,sizeof(dp));
    memset(pre,-1,sizeof(pre));
    int Max=0,MaxIndex;
    for(int i=0;i<trueN;i+=1){
        dp[i]=1;
        for(int j=0;j<i;j+=1){
            if(env[i].h>env[j].h&&env[i].w>env[j].w&&dp[j]+1>dp[i]){
            //这里曾经有错,没有w那一句判断
                dp[i]=dp[j]+1;
                pre[i]=j;
            }
        }
        if(dp[i]>Max) Max=dp[i],MaxIndex=i;
    }

    printf("%d\n",dp[MaxIndex]);
    int ind=0;
    while(MaxIndex!=-1){
        ans[ind++]=env[MaxIndex].index;
        MaxIndex=pre[MaxIndex];
    }
    while(1){
        printf("%d",ans[--ind]);
        if(ind==0){
            printf("\n");
            break;
        }
        else printf(" ");
    }

    return 0;
}

/*

5 2 2
3 10
10 3
9 4
5 9
8 5

*/










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值