题意:给一个长方形大小,现在有n个高宽确定的长方形,要求组成序列,满足:1.宽严格单调递增而高严格单调递增 2.组成序列的最小的长方形要比给定的长方形大。
问最长序列及其编号。
有点类似于lis,我们为了方便打印结果所以选择n平方的dp算法。
令dp[i]为以i结尾的最长上升序列大小,那么dp[i]=max(dp[i],dp[j]+1|dp[j]<dp[i])
即当前dp能被所有小于他的转移到。再另开一个pre数组,记录前项是哪一个。
(wa了一发再自定义运算符那里,<=直接按照了常规写,比较第一个再比较第二个,但这里是共同大小,即w和h都不能<=,所应该用||)
ps:一开始以为这道题是个模拟水题,,写到一半发现8太行hhhh
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b) for(int i=a;i<b;i++)
#define dw(i,a,b) for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
char ch = getchar(); int x = 0, f = 1;
while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
int n, w, h;
const int N = 5005;
int pre[N];
int dp[N];
struct node { int w, h, pos; bool operator<(const node a)const { return w == a.w ? h < a.h : w < a.w; }
bool operator<=(const node a)const { return w <= a.w||h <= a.h; }
}vec[N];
int main()
{
n = read(), w = read(), h = read();
int x, y;
up(i, 0, n) { x = read(), y = read(); vec[i] = node{ x,y,i }; }
sort(vec, vec + n);
memset(pre, -1, sizeof(pre));
up(i, 0, n)
{
if (vec[i] <= node{ w,h,0 })continue;
dp[i] = 1;
up(j, 0, i)
{
if (vec[i] <= vec[j])continue;
if (vec[j] <= node{ w,h,0 })continue;
if (dp[i] < dp[j] + 1)
{
dp[i] = dp[j] + 1;
pre[i] = j;
}
}
}
int ans = -1; int pos =-1;
up(i, 0, n)
{
if (ans < dp[i])ans = dp[i], pos = i;
}
if (ans == 0) { cout << "0" << endl; }
else
{
vector<int>ansvec;
for (int i = pos; ~i; i = pre[i])
{
ansvec.push_back(i);
}
reverse(ansvec.begin(), ansvec.end());
cout << ansvec.size() << endl;
for (auto k : ansvec)cout << vec[k].pos+1 << " ";
}
return 0;
}