题目链接(Mysterious Present)
题意
有N个信封,每个信封的大小宽为 w i w_i wi高为 h i h_i hi。Peter决定用这些信封装贺卡。贺卡大小为 w × h w×h w×h。Peter有一个要求,他要求选 m m m个信封连成一个串A={ a 1 a_1 a1, a 2 a_2 a2,…, a m a_m am},使得 a i a_i ai的宽和高分别大于 a i − 1 a_{i-1} ai−1的宽和高。让你求最大的 m m m。
题解
很显然串A满足最长上升子序列性质。即:
if(a[i]>a[j])
dp[i] = max(dp[i], dp[j]+1);
这里只需要添加记录路径即可。假如 d p [ i ] dp[i] dp[i]是由 d p [ j ] + 1 dp[j]+1 dp[j]+1转为而来的,那么路径前驱 p r e [ i ] = j pre[i]=j pre[i]=j。所以这里排个序,然后跑带有记录路径的最长上升子序列就欧克!
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
#include <ctime>
#include <queue>
#include <list>
#define INF 0x3fffffff
//#define mod 10000007
#define FOR(i,n) for(i=0; i<n; ++i)
#define dbg(x) { cout << #x << " = " << x << endl; }
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eps 1e-9
#define MOD 176543
using namespace std;
typedef double db;
typedef long long LL;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
const LL mod = 1000000007;
const int MAXN = 1e5 + 100;
inline LL Abs(LL x) { return x < 0 ? -x : x; }
inline LL Ceil(LL x, LL y) { return x / y + (x % y ? 1 : 0); }
LL gcd(LL x, LL y) { return y == 0 ? x : gcd(y, x % y); }
struct Point
{
int x, y, id;
bool operator<(const Point& A)
{
if(x==A.x)
return y<A.y;
return x<A.x;
}
}a[MAXN];
bool isxiaoyu(const Point& A, const Point& B)
{
return A.x<B.x&&A.y<B.y;
}
int path[MAXN];
int pre[MAXN];
int dp[MAXN];
int pos, mx;
void getmax(int n)
{
pos = mx = 0;
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
if(isxiaoyu(a[0], a[i]))
{
dp[i] = 1;
for(int j = 0; j < i; j++)
{
if(isxiaoyu(a[j], a[i])&&dp[i]<dp[j]+1)
{
dp[i] = dp[j]+1;
pre[i] = j;
}
}
if(mx<dp[i])
{
mx = dp[i];
pos = i;
}
}
else
{
dp[i] = 0;
}
}
}
int main()
{
int n;
scanf("%d", &n);
scanf("%d%d", &a[0].x, &a[0].y);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
a[i].id = i;
}
sort(a+1, a+n+1);
getmax(n);
if(mx==0)
{
puts("0");
}
else
{
int cnt = 0;
while(pos!=0)
{
path[cnt++] = a[pos].id;
pos = pre[pos];
}
printf("%d\n", cnt);
for(int i = cnt-1; i > 0; i--)
{
printf("%d ", path[i]);
}
printf("%d\n", path[0]);
}
return 0;
}