Submission #250634

# Submission time Handle Problem Language Result Execution time Memory
250634 2020-07-18T14:45:28 Z dvdg6566 Simfonija (COCI19_simfonija) C++14
110 / 110
82 ms 8816 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll,ll> pi;
typedef vector<pi> vpi;
typedef double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end() 
#define SZ(x) (ll)x.size()
#define f first
#define s second
const ll MAXN=100001;
const ll MAXK=1000001;
const ll INF = 1e13;
const ll MOD = 1e9+7;

vi V;
ll T;
ll A[MAXN];
ll B[MAXN];
ll N,K;
multiset<int> lft;
multiset<int> rgt;


int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin>>N>>K;
	if(N==K){cout<<0;return 0;}
	for(int i=0;i<N;++i){
		cin>>A[i];
	}
	for(int i=0;i<N;++i){
		cin>>B[i];
		V.pb(B[i]-A[i]);
	}
	ll res=0;
	ll ans=INF;
	sort(ALL(V));
	int cutoff=V[(N-K)/2];

	for(int i=0;i<N-K;++i){
		if(V[i]<=cutoff){
			lft.insert(V[i]);
			res+=cutoff-V[i];
		}
		else {
			rgt.insert(V[i]);
			res+=V[i]-cutoff;
		}
	}
	while(SZ(rgt)&&*rgt.begin()==cutoff){
		lft.insert(cutoff);
		rgt.erase(rgt.begin());
	}
	// for(auto i:lft)cerr<<i<<' ';cerr<<'\n';
	ans=min(ans,res);
	for(int i=0;i<K;++i){
		rgt.insert(V[i+N-K]);
		lft.erase(lft.find(V[i]));
		res+=abs(V[i+N-K]-cutoff);
		res-=abs(V[i]-cutoff);

		while(SZ(lft) < SZ(rgt)){
			ll newct=*rgt.begin();
			ll delta=newct-cutoff;
			res+=delta*(-SZ(rgt)+SZ(lft));
			while(SZ(rgt)&&*rgt.begin()==newct){
				lft.insert(newct);
				rgt.erase(rgt.begin());
			}
			cutoff=newct;
		}
		ans=min(ans,res);
	}
	cout<<ans;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 0 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 7408 KB Output is correct
2 Correct 51 ms 7576 KB Output is correct
3 Correct 48 ms 7416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 7404 KB Output is correct
2 Correct 49 ms 7408 KB Output is correct
3 Correct 47 ms 7404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 7404 KB Output is correct
2 Correct 50 ms 7404 KB Output is correct
3 Correct 51 ms 7408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 4080 KB Output is correct
2 Correct 61 ms 5872 KB Output is correct
3 Correct 55 ms 5616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 6128 KB Output is correct
2 Correct 82 ms 3440 KB Output is correct
3 Correct 67 ms 6252 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 58 ms 2808 KB Output is correct
2 Correct 61 ms 5744 KB Output is correct
3 Correct 62 ms 5748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 4716 KB Output is correct
2 Correct 62 ms 5992 KB Output is correct
3 Correct 59 ms 7024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 5360 KB Output is correct
2 Correct 56 ms 4216 KB Output is correct
3 Correct 53 ms 8816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 6000 KB Output is correct
2 Correct 46 ms 7356 KB Output is correct
3 Correct 57 ms 6256 KB Output is correct