标签:des style blog http io ar color os sp
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 323 Accepted Submission(s): 90
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <stack> #include <algorithm> using namespace std; #define root 1,n<<1|1,1 #define lr rt<<1 #define rr rt<<1|1 #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define X first #define Y second typedef long long LL; typedef pair<int,int> pii; const int N = 1010 ; const int M = 2010 ; const int inf = 1e9+7; int n , m , a[N] , b[N] ; //---------------------------- int date[N][M<<2]; void Up( int id , int rt ) { date[id][rt] = max( date[id][lr] , date[id][rr] ) ; } void Build( int id , int l , int r , int rt ) { date[id][rt] = 0 ; if( l == r ) return ; int mid = (l+r)>>1; Build(id,lson),Build(id,rson); Up(id,rt); } void Update( int id , int l , int r , int rt , int x , int val ) { if( l == r ) { date[id][rt] = max( date[id][rt] , val ) ; return ; } int mid = (l+r)>>1; if( x <= mid ) Update(id,lson,x,val); else Update(id,rson,x,val); Up(id,rt); } int Query( int id , int l , int r , int rt , int L , int R ) { if( l == L && r == R ) { return date[id][rt]; } int mid = (l+r)>>1; if( R <= mid ) return Query(id,lson,L,R); else if( L > mid ) return Query(id,rson,L,R); else return max( Query(id,lson,L,mid) , Query(id,rson,mid+1,R) ); } //---------------------------------- struct node { int x , id , xx ; }e[N<<2]; bool cmp1( const node &a , const node &b ) { return a.x < b.x ; } bool cmp2( const node &a , const node &b ) { return a.id < b.id ; } void Read() { cin >> n >> m ; for( int i = 0 ; i < 2 * n ; ++i ){ cin >> e[i].x ; e[i].id = i ; } sort( e , e + 2 * n , cmp1 ); e[0].xx = 2 ; for( int i = 1 ; i < 2 * n ; ++i ){ e[i].xx = ( e[i].x == e[i-1].x ? e[i-1].xx : e[i-1].xx + 1 ); } sort( e , e + 2 * n , cmp2 ); int tot = 0 ; for( int i = 1 ; i <= n ; ++i ) a[i] = e[tot++].xx , b[i] = e[tot++].xx ; } vector<pii>A,B; void Run() { int ans = 1 ; for( int i = 0 ; i <= m ; ++i ) Build( i , root ); for( int i = 1 ; i <= n ; ++i ) { A.clear() , B.clear(); for( int j = 0 ; j <= min( i , m ) ; ++j ) { int tmpa = Query( j , root , 1 , a[i] - 1 ) + 1 ; ans = max( ans , tmpa ) ; A.push_back(pii(j,tmpa)); if( !j ) continue ; int tmpb = Query( j - 1 , root , 1 , b[i] - 1 ) + 1 ; ans = max( ans , tmpb ) ; B.push_back(pii(j,tmpb)); } for( int j = 0 ; j < A.size() ; ++j ) Update( A[j].X ,root , a[i] , A[j].Y ); for( int j = 0 ; j < B.size() ; ++j ) Update( B[j].X ,root , b[i] , B[j].Y ); } cout << ans << endl ; } int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL ios::sync_with_stdio(false); int _ ; cin >> _ ; while( _-- ) Read() , Run() ; }
标签:des style blog http io ar color os sp
原文地址:http://www.cnblogs.com/hlmark/p/4134066.html