102202024-03-29 16:28:54111Egyengetőcpp17Time limit exceeded 40/1003.596s16280 KiB
#include <bits/stdc++.h>
using namespace std;

#include "grader.h"

#define int long long

struct F{
	int n;
	unordered_map<int,pair<int,int>>t;
	
	F(int n):n(n){
	}
	
	void update(int i){
		for(int j=i;j<n;j+=j&-j){
			int s=j-(j&-j)+1;
			int e=j;
			t[j].first+=s-i;
			t[j].second++;
		}
	}
	
	int query(int i){
		int x=0;
		for(int j=i;j;j-=j&-j){
			int s=j-(j&-j)+1;
			int e=j;
			x+=t[j].first+t[j].second*(i-s);
		}
		return x;
	}
};

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=getN(),K=getK(),M=5e8;
	F a(M+1),b(M+1);
	auto calc=[&](int x)->int{
		return a.query(x)+b.query(M/2-(x+K));
	};
	for(int i=0;i<N;i++){
		int y=Data();
		a.update(y);
		b.update(M/2-y);
		int l=-1e8,h=2e8;
		while(h-l>=3){
			int m1=l+(h-l)/3;
			int m2=h-(h-l)/3;
			if(calc(m1)<=calc(m2)){
				h=m2;
			}
			else{
				l=m1;
			}
		}
		int ans=1e18;
		for(int i=l;i<=h;i++){
			ans=min(ans,calc(i));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted4ms2032 KiB
2Accepted7ms2312 KiB
subtask20/20
3Accepted19ms2780 KiB
4Accepted37ms2740 KiB
5Accepted54ms3000 KiB
6Accepted71ms4216 KiB
7Accepted108ms2728 KiB
8Time limit exceeded3.532s3240 KiB
subtask320/20
9Accepted182ms3300 KiB
10Accepted166ms4952 KiB
11Accepted167ms3948 KiB
12Accepted172ms4300 KiB
13Accepted173ms4388 KiB
14Accepted174ms5224 KiB
subtask420/20
15Accepted1.069s3644 KiB
16Accepted1.656s16280 KiB
17Accepted1.809s3888 KiB
18Accepted1.554s16016 KiB
19Accepted1.623s5624 KiB
20Accepted1.613s12224 KiB
subtask50/40
21Time limit exceeded3.517s12372 KiB
22Time limit exceeded3.532s3860 KiB
23Time limit exceeded3.523s3244 KiB
24Time limit exceeded3.519s3252 KiB
25Time limit exceeded3.596s12540 KiB
26Time limit exceeded3.595s14608 KiB