现在nlogn过不去吗/kk
查看原帖
现在nlogn过不去吗/kk
550957
Anonymely楼主2023/2/6 18:45
#include<bits/stdc++.h>
using namespace std;

#define ull unsigned long long
#define eps 0.00000001
#define ll long long
#define orz puts("--------------------------")
#define pf(x) printf("%s",x);
#define pii pair<int,int>
#define pb push_back
#define mk make_pair
#define newline puts("")
#define newspace putchar(' ')
#define lowbit(x) (x&(-x))
#define md(l,r) ((l+r)>>1)
#define lson(p) (p<<1)
#define rson(p) ((p<<1)|1)
#define fi first
#define se second
#define inf 2147483647
#define mod 1000000007
#define N 1000010

namespace fastIO{
	template<typename T> void read(T &x){
		x=0;
		char ch=getchar();T fl=1;
		while(ch<'0'||ch>'9'){if(ch=='-')fl=-1;ch=getchar();};
		while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();};
		x=x*fl;
	}
	template<typename T, typename ...T1> void read(T &x, T1 &...x1){
		read(x);
		read(x1...);
	}
	template<typename T> void write(T x){
		if(x<0){
			x=-x;putchar('-');
		}
		if(x/10)write(x/10);
		putchar(x%10+'0');
	}
	template<typename T> void writer(T x){
		write(x);
		putchar(' ');
	}
	template<typename T> void writen(T x){
		write(x);
		putchar('\n');
	}
}

using namespace fastIO;

struct Tree {
	int ls, rs;
	int key;
	int dis;
} t[N];

int cnt;

int add(int x) {
	t[++cnt].key = x;
	t[cnt].ls = t[cnt].rs = 0;
	t[cnt].dis = 0;
	return cnt;
}

struct Node {
	int rt, l, r, siz, val;
} s[N];
int top;

int merge(int x, int y) {
	if (!x || !y) return x + y;
	if (t[x].key < t[y].key) swap(x, y);
	t[x].rs = merge(t[x].rs, y);
	if (!t[x].ls || t[t[x].ls].dis > t[t[x].rs].dis) swap(t[x].ls, t[x].rs);
	if (t[x].rs) t[x].dis = t[t[x].rs].dis + 1;
	return x;
}

int del(int rt) {
	return merge(t[rt].ls, t[rt].rs);
}

int a[N];
int n; 

signed main() {
	read(n);
	for (int i = 1; i <= n; i++) read(a[i]), a[i] -= i;
	for (int i = 1; i <= n; i++) {
		top++;
		s[top].l = s[top].r = i;
		s[top].siz = 1, s[top].val = a[i];
		s[top].rt = add(a[i]);
		while (top > 1 && s[top - 1].val > s[top].val) {
			top--;
			s[top].rt = merge(s[top].rt, s[top + 1].rt);
		//	cout << s[top].rt << ' ';
			s[top].r = s[top + 1].r;
			s[top].siz += s[top + 1].siz;
			//cout << s[top].siz << endl;
			while (s[top].siz > (s[top].r - s[top].l + 2) / 2) {
			//	cout << s[top].rt << ' ';
				s[top].siz--;
				s[top].rt = del(s[top].rt);
			}
			s[top].val = t[s[top].rt].key;
			//cout << s[top].val << endl;
		}
	}
	int now = 1;
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		ans += abs(a[i] - s[now].val);
		if (s[now].r == i) now++;
	} 
	writen(ans);
	return 0;
}


rt,可能是常数大了一点,只有50pts

2023/2/6 18:45
加载中...