标签:codeforces 线段树 dp
给出n个连续塔,每个塔有高度
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <set>
#define MAX 100007
using namespace std;
int n;
int l[MAX],r[MAX],h[MAX];
struct Tree
{
int l,r,minn,lazy;
}tree[MAX<<2];
void push_up ( int u )
{
tree[u].minn = min ( tree[u<<1].minn , tree[u<<1|1].minn );
}
void build ( int u , int l , int r )
{
tree[u].l = l;
tree[u].r = r;
tree[u].lazy = 0;
int mid = l+r>>1;
if ( l == r )
{
tree[u].minn = h[l];
return;
}
build ( u<<1 , l , mid );
build ( u<<1|1 , mid+1 , r );
push_up ( u );
}
void push_down ( int u )
{
int ll = tree[u].lazy;
tree[u].lazy = 0;
if ( ll )
{
tree[u<<1].lazy += ll;
tree[u<<1|1].lazy += ll;
tree[u<<1].minn += ll;
tree[u<<1|1].minn += ll;
}
}
void update ( 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 )
{
tree[u].minn += 1;
tree[u].lazy += 1;
return;
}
push_down ( u );
if ( left <= mid && right >= l )
update ( u<<1 , left , right );
if ( left <= r && right > mid )
update ( u<<1|1 , left , right );
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].minn;
push_down ( u );
int ret = 1<<29;
if ( left <= mid && right >= l )
ret = min ( ret , query ( u<<1 , left , right ) );
if ( left <= r && right > mid )
ret = min ( ret , query ( u<<1|1 , left , right ) );
return ret;
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
h[0] = h[n+1] = 0;
for ( int i = 1; i <= n ; i++ )
scanf ( "%d" , &h[i] );
build ( 1 , 0 , n+1 );
for ( int i = 1 ; i <= n ; i++ )
{
update ( 1 , 0 , i-1 );
l[i] = query ( 1 , 0 , i );
}
build ( 1 , 0 , n+1 );
for ( int i = n ; i > 0 ; i-- )
{
update ( 1 , i+1 , n+1 );
r[i] = query ( 1 , i , n+1 );
}
int ans = 0;
for ( int i = 1 ; i <= n ; i++ )
{
//cout << "YES : " << l[i] << " " << r[i] << endl;
ans = max ( ans , min ( l[i] , r[i] ));
}
printf ( "%d\n" , ans );
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
codeforces 573B B. Bear and Blocks(线段树+dp)
标签:codeforces 线段树 dp
原文地址:http://blog.csdn.net/qq_24451605/article/details/48323499