102452024-03-29 18:05:15111Egyengetőcpp17Runtime error 20/10035ms4828 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

#include "grader.h"

#define int long long

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N=getN(),K=getK(),M=4e8;
	if(N>1000)return 1;
	ordered_set<int>s;
	auto calc=[&](int x)->int{
		int c=0;
		for(int i:s){
			if(i<x){
				c+=x-i;
			}
			if(i>x+K){
				c+=i-(x+K);
			}
		}
		return c;
	};
	for(int i=0;i<N;i++){
		s.insert(Data());
		int ans=1e18;
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(m>=i-s.order_of_key(*s.find_by_order(m)+K)){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)));
		}
		{
			int l=0,h=i;
			while(l<h){
				int m=(l+h)/2;
				if(s.order_of_key(*s.find_by_order(m)-K)>=i-m){
					h=m;
				}
				else{
					l=m+1;
				}
			}
			ans=min(ans,calc(*s.find_by_order(h)-K));
			if(h>0)ans=min(ans,calc(*s.find_by_order(h-1)-K));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1888 KiB
2Accepted3ms2124 KiB
subtask20/20
3Accepted4ms2320 KiB
4Accepted6ms2408 KiB
5Accepted7ms2672 KiB
6Accepted9ms2884 KiB
7Accepted16ms3112 KiB
8Runtime error3ms3264 KiB
subtask320/20
9Accepted32ms3412 KiB
10Accepted35ms3560 KiB
11Accepted35ms3772 KiB
12Accepted30ms3732 KiB
13Accepted30ms3724 KiB
14Accepted30ms3980 KiB
subtask40/20
15Runtime error3ms3880 KiB
16Runtime error3ms3880 KiB
17Runtime error3ms3876 KiB
18Runtime error3ms4104 KiB
19Runtime error3ms4324 KiB
20Runtime error3ms4404 KiB
subtask50/40
21Runtime error3ms4536 KiB
22Runtime error3ms4400 KiB
23Runtime error3ms4536 KiB
24Runtime error3ms4768 KiB
25Runtime error3ms4828 KiB
26Runtime error3ms4828 KiB