标签:codeforces dp 线段树
给出一个程序,就是冒泡排序,每次如果发现相邻两个数前一个大于后一个,交换位置后建边,问最后得到的这个图中的最大独立集,这个最大独立集定义为所有的点都不相邻的最大点的集合的规模。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100007
using namespace std;
int n,a[MAX];
struct Tree
{
int l,r,maxn;
}tree[MAX<<2];
void push_up ( int u )
{
tree[u].maxn = max ( tree[u<<1].maxn , tree[u<<1|1].maxn );
}
void build ( int u , int l , int r )
{
tree[u].l = l;
tree[u].r = r;
int mid = l+r>>1;
tree[u].maxn = 0;
if ( l == r ) return;
build ( u<<1 , l , mid );
build ( u<<1|1 , mid+1 , r );
}
void update ( int u , int x , int v )
{
int l = tree[u].l;
int r = tree[u].r;
int mid = l+r>>1;
if ( l == r )
{
tree[u].maxn = v;
return;
}
if ( x > mid ) update ( u<<1|1 , x , v );
else update ( u<<1 , x , v );
push_up ( u );
}
int query ( int u , int left , int right )
{
int l = tree[u].l;
int r = tree[u].r;
int mid = l+r>>1;
if ( left <= l && r <= right )
return tree[u].maxn;
int ret = 0;
if ( left <= mid && right >= l )
ret = max ( ret , query ( u<<1 , left , right ) );
if ( left <= r && right > mid )
ret = max ( ret , query ( u<<1|1 , left , right ) );
return ret;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
for ( int i = 1 ; i <= n ; i++ )
scanf ( "%d" , &a[i] );
build ( 1 , 1, n );
int x,ans=1;
update ( 1 , a[1] , 1 );
for ( int i = 2; i <= n ; i++ )
{
x = query ( 1 , 1 , a[i] );
ans = max ( ans , x+1 );
update ( 1 , a[i] , x+1 );
}
printf ( "%d\n" , ans );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 340D D. Bubble Sort Graph(dp+线段树)
标签:codeforces dp 线段树
原文地址:http://blog.csdn.net/qq_24451605/article/details/48469741