Codeforces 4D. Mysterious Present

来源:互联网 发布:移动数据流量套餐退订 编辑:程序博客网 时间:2024/06/05 02:04

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 12 22 2
output
11 
input
3 3 35 412 119 8
output
31 3 2 

题意: 有几个信封, 小的信封可以放在大的信封的上面, 问最多能叠几个信封

思路:

1)转化为有向无环图, 求已知终点的最长路径(dp的方法)

2)先按宽度排序, 然后求的按长度的最长递增子序列

代码1

/*DAG的方法 数据太大的时候内存超出*/#include <stdio.h>#include <queue>#include <algorithm>#include <vector>using namespace std;const int MAXN = 5005;const int INF = 1 << 30;struct Node{    int w, h;    Node(){}    Node(int x, int y) : w(x), h(y){}    bool operator<(const Node &node)const    {        if(w < node.w && h < node.h)            return true;        return false;    }};vector<int> g[MAXN];Node node[MAXN];int dis[MAXN];int father[MAXN];void Init(int n){    for(int i = 0; i <= n; ++i)        dis[i] = -INF;}int DP(int s){    int &ans = dis[s];    if(ans >= 0) return ans;    ans = 0, father[s] = -1;    for(int i = 0; i < g[s].size(); ++i)    {        int v = g[s][i];        if(ans < DP(v) + 1)        {            ans = dis[v] + 1;            father[s] = v;        }    }    return ans;}void PrintPath(int s){    if(s == -1)        return;    printf("%d ", s);    PrintPath(father[s]);}int main(){#ifdef _LOCAL    freopen("F://input.txt", "r", stdin);#endif   int n, w, h;   scanf("%d%d%d", &n, &w, &h);   node[0] = Node(w, h);   for(int i = 1; i <= n; ++i)   {       scanf("%d%d", &node[i].w, &node[i].h);   }   for(int i = 0; i < n; ++i)   {       for(int j = i + 1; j <= n; ++j)       {           if(node[i] < node[j] && node[0] < node[j])               g[i].push_back(j);           else if(node[j] < node[i] && node[0] < node[i])               g[j].push_back(i);       }   }   Init(n);   int result = DP(0);   printf("%d\n", result);   if(result)       PrintPath(father[0]);}



代码2

#include <stdio.h>#include <vector>#include <algorithm>using namespace std;const int MAXN = 5005;struct Node{    int id, w, h;    bool operator< (const Node &node) const    {        return w < node.w;    }    Node(){};    Node(int x, int y, int z) : id(x), w(y), h(z){}};vector<Node> seq;int ans[MAXN];int pre[MAXN];int dp(int n){    int maxIndex = 1;    for(int i = 1; i <= n; ++i)    {        for(int j = i - 1; j >= 1; --j)        {            if(seq[j].w < seq[i].w && seq[j].h < seq[i].h && ans[i] < ans[j] + 1)            {                ans[i] = ans[j] + 1;                pre[i] = j;            }        }        maxIndex = ans[i] > ans[maxIndex]? i : maxIndex;    }    return maxIndex;}void PrintSeq(int s){    if(s == 0) return;    PrintSeq(pre[s]);    printf("%d ", seq[s].id);}int main(){#ifdef _LOCAL    freopen("F://input.txt", "r", stdin);#endif    int n, w, h;    scanf("%d%d%d", &n, &w, &h);    seq.push_back(Node(0, w, h));    int count = 0;    for(int i = 1; i <= n; ++i)    {        Node temp;        scanf("%d%d", &temp.w, &temp.h);        if(w < temp.w && h < temp.h)        {            ++count;            temp.id = i;            seq.push_back(temp);            ans[count] = 1;            pre[count] = 0;        }    }    if(count == 0)    {        printf("0");        return 0;    }    sort(seq.begin(), seq.end());    int maxIndex =  dp(count);    printf("%d\n", ans[maxIndex]);    PrintSeq(maxIndex);    return 0;}




0 0
原创粉丝点击