正文
增加 19 行删除 19 行
67 行未改动686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311467 行未改动while(cc < '0' || cc > '9') { if( cc == '-' ) flus = -flus; cc = getchar(); }while(cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();return cn * flus;}const int N = 1e6 + 5 ;int n, top, Ans ;int a[N], b[N], ch[2][N], dis[N] ;struct node {int rt, l, r, sz, val ;} s[N];int merge( int x, int y ) {if( !x || !y ) return x + y ;if( a[x] < a[y] ) swap( x, y ) ;rs(x) = merge( rs(x), y ) ;if( dis[rs(x)] > dis[ls(x)] ) swap( rs(x), ls(x) ) ;dis[x] = dis[rs(x)] + 1 ; return x ;}int Del( int x ) { return merge( ls(x), rs(x) ) ; }signed main(){n = read() ; dis[0] = -1 ;rep( i, 1, n ) a[i] = read() - i;rep( i, 1, n ) {s[++ top] = (node){ i, i, i, 1, a[i] } ;while( top != 1 && s[top - 1].val > s[top].val ) {-- top ; s[top].rt = merge( s[top].rt, s[top + 1].rt ) ;s[top].sz += s[top + 1].sz ; s[top].r = s[top + 1].r ;while( s[top].sz > ( s[top].r - s[top].l + 2 ) / 2 ) { //此处可行性//合并的前提是左平均数小于右平均数,把左全部丢入右,平均数变大-- s[top].sz, s[top].rt = Del( s[top].rt ) ;}s[top].val = a[s[top].rt] ;}}int cnt = 1 ;rep( i, 1, n ) {if( s[cnt].r < i ) ++ cnt ;Ans += abs( s[cnt].val - a[i] ) ;}printf("%lld\n", Ans ) ; cnt = 1 ;rep( i, 1, n ) {if( s[cnt].r < i ) ++ cnt ;printf("%lld ", s[cnt].val + i ) ;}return 0;}```67 行未改动686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311467 行未改动while(cc < '0' || cc > '9') { if( cc == '-' ) flus = -flus; cc = getchar(); }while(cc >= '0' && cc <= '9') cn = cn * 10 + cc - '0', cc = getchar();return cn * flus;}const int N = 1e6 + 5 ;int n, top, Ans ;int a[N], b[N], ch[2][N], dis[N] ;struct node {int rt, l, r, sz, val ;} s[N];int merge( int x, int y ) {if( !x || !y ) return x + y ;if( a[x] < a[y] ) swap( x, y ) ;rs(x) = merge( rs(x), y ) ;if( dis[rs(x)] > dis[ls(x)] ) swap( rs(x), ls(x) ) ;dis[x] = dis[rs(x)] + 1 ; return x ;}int Del( int x ) { return merge( ls(x), rs(x) ) ; }signed main(){n = read() ; dis[0] = -1 ;rep( i, 1, n ) a[i] = read() - i;rep( i, 1, n ) {s[++ top] = (node){ i, i, i, 1, a[i] } ;while( top != 1 && s[top - 1].val > s[top].val ) {-- top ; s[top].rt = merge( s[top].rt, s[top + 1].rt ) ;s[top].sz += s[top + 1].sz ; s[top].r = s[top + 1].r ;while( s[top].sz > ( s[top].r - s[top].l + 2 ) / 2 ) { //此处可行性//合并的前提是左平均数小于右平均数,把左全部丢入右,平均数变大-- s[top].sz, s[top].rt = Del( s[top].rt ) ;}s[top].val = a[s[top].rt] ;}}int cnt = 1 ;rep( i, 1, n ) {if( s[cnt].r < i ) ++ cnt ;Ans += abs( s[cnt].val - a[i] ) ;}printf("%lld\n", Ans ) ; cnt = 1 ;rep( i, 1, n ) {if( s[cnt].r < i ) ++ cnt ;printf("%lld ", s[cnt].val + i ) ;}return 0;}```