AC代码:
#include<bits/stdc++.h> using namespace std; #define maxn 10005 struct node { int x; int y; } temp[maxn]; bool cmp(node a,node b) { return a.x<b.x; } int dp_in[maxn]; int dp_de[maxn]; int n; void s_dp() { memset(dp_in,0,sizeof(dp_in)); memset(dp_de,0,sizeof(dp_de)); int maxx1=-1; int maxx2=-1; for(int i=1; i<=n; i++) { dp_in[i]=1; dp_de[i]=1; for(int j=1; j<i; j++) { if(temp[j].y<temp[i].y) { dp_in[i]=max(dp_in[i],dp_in[j]+1); if(maxx1<dp_in[i]) maxx1=dp_in[i]; } if(temp[j].y>temp[i].y) { dp_de[i]=max(dp_de[i],dp_de[j]+1); if(maxx2<dp_de[i]) maxx2=dp_de[i]; } } } printf("%d\n",max(maxx1,maxx2)); } int main() { int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1; i<=n; i++) { scanf("%d %d",&temp[i].x,&temp[i].y); } sort(temp+1,temp+n+1,cmp); s_dp(); } }