HDU-1160-FatMouseのスピード(DP、最長増加するシーケンス)

リンク:

https://vjudge.net/problem/HDU-1160

質問の意味:

FatMouseは太ったマウスは、より速く、それが実行されると考えています。これを反証するには、マウスのコレクションにデータを取得し、重みが増加しているように、配列中に可能な限り、このデータのような大きなサブセットを入れたいのですが、速度が減少しています。

アイデア:

Sは、その後、最長の増加部分列を並べ替えるには、nlogn N2必ずしも容易またはバグ

コード:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
//#include <memory.h>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <math.h>
#include <stack>
#include <string>
#include <assert.h>
#include <iomanip>
#include <iostream>
#include <sstream>
#define MINF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const LL MOD = 20090717;
const int MAXN = 1e3+10;

struct Node
{
    int w, s;
    int id;
    bool operator < (const Node& that) const
    {
        return this->s > that.s;
    }
}node[MAXN];
int Dp[MAXN], Pre[MAXN];
int n;

void Print(int p)
{
    if (p == -1)
        return;
    Print(Pre[p]);
    printf("%d\n", node[p].id);
}

int main()
{
    int l, r;
    while (~scanf("%d%d", &l, &r))
    {
        node[++n].w = l;
        node[n].s = r;
        node[n].id = n;
    }
    sort(node+1, node+1+n);
    memset(Pre, -1, sizeof(Pre));
    memset(Dp, 0, sizeof(Dp));
    for (int i = 1;i <= n;i++)
    {
        Dp[i] = 1;
        for (int j = 1;j < i;j++)
        {
            if (node[i].w > node[j].w && node[i].s < node[j].s)
            {
                if (Dp[j]+1 > Dp[i])
                {
                    Dp[i] = Dp[j]+1;
                    Pre[i] = j;
                }
            }
        }
    }
    int maxval = 1, maxpos = 1;
    for (int i = 1;i <= n;i++)
    {
        if (maxval < Dp[i])
        {
            maxval = Dp[i];
            maxpos = i;
        }
    }
    printf("%d\n", maxval);
    Print(maxpos);

    return 0;
}

おすすめ

転載: www.cnblogs.com/YDDDD/p/11666550.html