大佬求助!!!只有30分,剩下全RE
查看原帖
大佬求助!!!只有30分,剩下全RE
1490023
YZH_123456楼主2024/12/25 16:56
#include<bits/stdc++.h>
using namespace std;
#define rep( i, s, t ) for( register int i = s; i <= t; ++ i )
#define re register
#define ls(x) ch[0][x]
#define rs(x) ch[1][x]
#define int long long
int read() {
	char cc = getchar(); int cn = 0, flus = 1;
	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 = 1e3 + 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 = 0 ;
	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;
}
2024/12/25 16:56
加载中...