102442024-03-29 18:04:17111Egyengetőcpp17Time limit exceeded 20/1003.579s5444 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;
	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)));
			if(h+1<i)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));
			if(h+1<i)ans=min(ans,calc(*s.find_by_order(h+1)-K));
		}
		Solution(ans);
	}
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1888 KiB
2Accepted3ms2124 KiB
subtask20/20
3Accepted4ms2300 KiB
4Accepted6ms2524 KiB
5Accepted8ms2736 KiB
6Accepted10ms2828 KiB
7Accepted18ms3100 KiB
8Time limit exceeded3.555s2872 KiB
subtask320/20
9Accepted35ms3296 KiB
10Accepted50ms3552 KiB
11Accepted52ms3760 KiB
12Accepted39ms3720 KiB
13Accepted39ms4100 KiB
14Accepted39ms3932 KiB
subtask40/20
15Accepted1.358s4800 KiB
16Time limit exceeded3.579s3848 KiB
17Accepted2.5s5444 KiB
18Time limit exceeded3.578s3900 KiB
19Time limit exceeded3.579s3792 KiB
20Accepted2.821s5408 KiB
subtask50/40
21Time limit exceeded3.539s4012 KiB
22Time limit exceeded3.543s4096 KiB
23Time limit exceeded3.539s3924 KiB
24Time limit exceeded3.532s4056 KiB
25Time limit exceeded3.52s3788 KiB
26Time limit exceeded3.522s3796 KiB