题解
按照第一维降序排序(第一维相同按第二维降序排列防止第一维相同贡献) 在第二维中找最长上升子序列 用p数组记录转移路径方便输出
AC代码
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e3 + 10;
struct node
{
int x, y, id;
bool operator < (const node &oth) const
{
if (this->x != oth.x)
return this->x > oth.x;
return this->y > oth.y; //按照从大到小排 防止同x的贡献
}
}a[MAXN];
int d[MAXN], p[MAXN]; //长度为i的最小结尾值 第i个数在d中的位置
int main()
{
#ifdef LOCAL
freopen("C:/input.txt", "r", stdin);
#endif
int x, y, n = 0;
while (cin >> x >> y)
a[++n] = node { x, y, n };
sort(a + 1, a + n + 1);
memset(d, 0x3f, sizeof(d));
for (int i = 1; i <= n; i++)
{
int j = lower_bound(d + 1, d + n + 1, a[i].y) - d;
d[j] = min(d[j], a[i].y);
p[i] = j; //记录
}
int m = lower_bound(d + 1, d + n + 1, INF) - d - 1;
cout << m << endl;
int cnt = m;
for (int i = n; i >= 1 && cnt; i--)
if (p[i] == cnt)
cout << a[i].id << endl, cnt--;
return 0;
}