A person has two attributes: S, B (1 ≤ Si, Bi ≤ 10 ^ 9 ), when the two attributes meet the requirements of S1 <S2 & B1 <B2 or S1> S2 & B1> B2, the two do not hate each other. This section describes the attributes of N people (2 ≤ n ≤ 100 000) and calculates the maximum number of people, so no one of them will hate each other.
Question link: http://acdream.info/problem? PID = 1, 1216
--> It is easy to think of a two-dimensional Lis model ..
Two-Dimensional Reduction of one dimension, control one-dimensional increments, find the LIS for the other one .. (In sorting, the first dimension is from small to large, and the second dimension is from large to small, after sorting, the result of finding lis on the second dimension will certainly not show that the first dimension corresponding to the elements in the first dimension is the same, because the second dimension corresponding to the same first dimension is decreasing, the second-dimension lis is strictly incrementing ..)
#include <cstdio>#include <algorithm>#include <cstring>using std::sort;using std::lower_bound;const int MAXN = 100000 + 10;const int INF = 0x3f3f3f3f;struct PERSON{ int id; int S; int B; bool operator < (const PERSON& e) const { return S < e.S || (S == e.S && B > e.B); }} person[MAXN];int N;int buf[MAXN];int lis[MAXN], who[MAXN], fa[MAXN], cnt;int LIS(){ int ret = 1; memset(lis, 0x3f, sizeof(lis)); memset(fa, -1, sizeof(fa)); who[0] = -1; for (int i = 1; i <= N; ++i) { int id = lower_bound(lis + 1, lis + 1 + N, buf[i]) - lis; lis[id] = buf[i]; who[id] = i; fa[i] = who[id - 1]; if (id > ret) { ret = id; } } return ret;}void Read(){ for (int i = 1; i <= N; ++i) { scanf("%d%d", &person[i].S, &person[i].B); person[i].id = i; }}void Init(){ sort(person + 1, person + 1 + N); for (int i = 1; i <= N; ++i) { buf[i] = person[i].B; }}void Output(int x){ if (fa[x] == -1) { printf("%d", person[x].id); return; } Output(fa[x]); printf(" %d", person[x].id);}void Solve(){ cnt = LIS(); printf("%d\n", cnt); Output(who[cnt]); puts("");}int main(){ while (scanf("%d", &N) == 1) { Read(); Init(); Solve(); } return 0;}
ACD-1216-beautiful people (two-dimensional LIS)