CodeForces 4D

I looked at most of the problem solution is in accordance with the \ (\ mathcal {DP} \ ) to do it.

However, the present goose fundamentally weak \ (\ mathscr {DP} \ ) thumbs.

So, if you \ (\ mathcal {YY} \ ) Another Qing Qi approach.

Every time we meet the conditions of the two point \ (x (i, j) , y (i, j) (x_i <y_i, x_j <y_j) \) to build an edge

From \ (X \) points \ (Y \) .

Obviously all the points after the addition, the presence of at least one zero-degree point.

So long as we topological sorting, the longest current value of each record, and finally \ (dfs \) output program just fine.

It is worth noting: This question requires a relatively large space. So we have to use the adjacency matrix.

Because the adjacency list is the adjacency matrix space \ (2 \) doubly \ (N \) (one dot is recorded twice, \ (head \) is \ (N \) )

Secondly to use the adjacency matrix type \ (BOOL \) , otherwise still \ (\ mathcal {MLE} \ )

The following is a \ (\ mathcal {\ color { green} {AC}} \) Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
using namespace std;
#define ri register int

bool mat[5002][5002];
int len[5002], pre[5002], id[5002];
int wf[5002], hf[5002], in[5002];
int n, w, h, cnt, cnte, ans, fin;

inline void topo()
{
    queue<int> bf;
    for (ri i = 1; i <= cnt; ++i)
        if (in[i] == 0) bf.push(i), len[i] = 1;
    while (!bf.empty())
    {
        int now = bf.front();
        bf.pop();
        if (ans < len[now])
        {
            ans = len[now];
            fin = now;
        }
        for (ri i = 1; i <= cnt; ++i)
        {
            if (i == now || !mat[now][i]) continue;
            --in[i];
            if (len[i] < len[now] + 1)
                len[i] = len[now] + 1, pre[i] = now;
            if (in[i] == 0)
                bf.push(i);
        }
    }
}

void dfs(int p)
{
    if (p == 0) return;
    if (pre[p]) dfs(pre[p]);
    printf("%d ", id[p]);
}

int main()
{
    scanf("%d%d%d", &n, &w, &h);
    for (ri i = 1; i <= n; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        if (x <= w || y <= h) continue;
        wf[++cnt] = x, hf[cnt] = y;
        id[cnt] = i;
    }
    if (cnt == 0)
    {
        printf("0\n");
        return 0;
    }
    for (ri i = 1; i < cnt; ++i)
        for (ri j = i + 1; j <= cnt; ++j)
        {
            if (wf[i] < wf[j] && hf[i] < hf[j])
                mat[i][j] = 1, ++in[j];
            else if (wf[i] > wf[j] && hf[i] > hf[j])
                mat[j][i] = 1, ++in[i];
        }
    topo();
    printf("%d\n", ans);
    dfs(fin);
    return 0;
}

Guess you like

Origin www.cnblogs.com/leprechaun-kdl/p/11839426.html