可以将x轴的先有序化,也可将y轴的先有序化。
比如样例以x轴有序化后: -3 3 ; -2 2; -1 1; 4 5;
现在以x轴从小到大枚举。 只需求出比当前 y轴数值大的 以前的y 所对应的包含个数的最大值即可。我是用线段树来寻找最大值。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <string>
using namespace std;
typedef pair<int,int> pii;
typedef long long ll;
typedef double dl;
typedef vector<pii> vii;
typedef set<pii> sii;
/*sim_operarion*/
#define sfint(x) scanf("%d",&x)
#define sfint2(x,y) scanf("%d%d",&x,&y)
#define sfint3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sfstr(c) scanf("%s",c)
#define sfdl(x) scanf("%lf",&x)
#define pfint(x) printf("%d\n",x)
#define fr(i,s,n) for(int i=s;i<n;++i)
#define _fr(i,n,s) for(int i=n-1;i>=s;--i)
#define cl(a) memset(a,0,sizeof(a))
/*CONST VALUE*/
const dl pi=acos(-1.0);
const dl eps=1e-6;
const int N = 100010;
int n;
struct P {
int x,y,id;
void read() {
sfint2(x,y);
x+=10000;
y+=10000;
}
bool operator < (const P a) const {
if (x==a.x) return y>a.y;
return x<a.x;
}
} p[N];
struct SEGTREE {
int l,r,mx,id;
} tree[20010*4];
void build(int root,int l,int r) {
tree[root].l = l;
tree[root].r = r;
tree[root].mx = 0;
tree[root].id = -1;
if(l == r) {
return;
}
int mid = (l+r)>>1;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
}
SEGTREE query(int root,int l,int r) {
if(tree[root].l >= l && tree[root].r <= r) {
return tree[root];
}
int mid = (tree[root].l + tree[root].r)>>1;
if(r <= mid)
return query(root<<1,l,r);
else if(l >mid)
return query(root<<1|1,l,r);
else {
SEGTREE a = query(root<<1,l,r);
SEGTREE b = query(root<<1|1,l,r);
if (a.mx>b.mx) return a;
return b;
}
}
int id,x;
void edit(int root ,int l,int r) {
if(tree[root].l == l && tree[root].r == r) {
if (x>tree[root].mx) {
tree[root].mx = x;
tree[root].id = id;
}
return ;
}
int mid = (tree[root].l + tree[root].r)>>1;
if(r <= mid) edit(root<<1,l,r);
else if (l>mid) edit(root<<1|1,l,r);
if (tree[root<<1].mx > tree[root<<1|1].mx){
tree[root].mx = tree[root<<1].mx;
tree[root].id = tree[root<<1].id;
}else{
tree[root].mx = tree[root<<1|1].mx;
tree[root].id = tree[root<<1|1].id;
}
}
P ch[N];
int ans[N],pre[N],num;
int main() {
#ifdef local
fi;
#endif
sfint(n);
fr(i , 0,n) p[i].read(),p[i].id = i;
sort(p,p+n);
build(1,0,20001);
int cnt = 1;
num = p[0].x;
ans[0] = 1;pre[0] = -1;
ch[0] .x = 1;ch[0].y = p[0].y;ch[0].id = 0;
SEGTREE u;
fr(i , 1 ,n) {
if (num != p[i].x) {
num = p[i].x;
fr(i , 0 ,cnt){
x = ch[i].x;
id = ch[i].id;
edit(1,ch[i].y,ch[i].y);
}
cnt = 0;
}
u = query(1,p[i].y+1,20001);
ch[cnt].x = u.mx+1;
ch[cnt].y = p[i].y;ch[cnt++].id = i;
ans[i] = u.mx+1;
pre[i] = u.id;
}
int best = 0,bp;
fr(i , 0 ,n){
if (best<ans[i]){
best = ans[i],bp = i;
}
}
pfint(best);
printf("%d",p[bp].id+1);
bp = pre[bp];
while(bp!=-1){
printf(" %d",p[bp].id+1);
bp = pre[bp];
}
puts("");
return 0;
}