답안 #159672

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
159672 2019-10-23T20:52:43 Z theknife2001 Simfonija (COCI19_simfonija) C++17
110 / 110
39 ms 2940 KB
#include <bits/stdc++.h>
#define ll long long

using namespace std;
const int N=1e5+55;
int a[N];
int b[N];
int x[N];
int n,k;
int pre;
int suf;
ll cur;
int j,i;

void solve(int pos)
{
	pre--;
	suf++;
	//cout<<pre<<' '<<suf<<endl;
	while(x[j]-x[pos]<x[pos]-x[i]&&i<pos&&j<n)
	{
		//cout<<' '<<i<<' '<<j<<endl;
		cur+=x[j]-x[pos-1];
		pre++;
		suf--;
		cur-=x[pos-1]-x[i];
		j++;
		i++;
	}
	//cout<<cur<<' ';
	ll dif=x[pos]-x[pos-1];
	cur+=dif*suf-dif*(pre+1);
	//cout<<cur<<endl;
}
		
		

int main()
{
  	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=0;i<n;i++)
	cin>>a[i];
	for(int i=0;i<n;i++)
	cin>>b[i];
	for(int i=0;i<n;i++)
	x[i]=a[i]-b[i];
	sort(x,x+n);
	int temp=x[0];
	for(int i=0;i<n;i++)
	{
		x[i]-=temp;
		//cout<<x[i]<<' ';
		if(i<n-k)
		cur+=abs(x[i]);
	}
	//cout<<endl;
	j=n-k;
	ll ans=cur;
	pre=n-k-1;
	for(int i=1;i<n;i++)
	{
		solve(i);
		ans=min(ans,cur);
	}
	cout<<ans<<endl;
	return 0;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 1528 KB Output is correct
2 Correct 35 ms 1528 KB Output is correct
3 Correct 33 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 1496 KB Output is correct
2 Correct 35 ms 1520 KB Output is correct
3 Correct 33 ms 1488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 1528 KB Output is correct
2 Correct 35 ms 1528 KB Output is correct
3 Correct 33 ms 1528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 1528 KB Output is correct
2 Correct 32 ms 1528 KB Output is correct
3 Correct 33 ms 1464 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 1528 KB Output is correct
2 Correct 39 ms 2936 KB Output is correct
3 Correct 38 ms 2936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 1516 KB Output is correct
2 Correct 33 ms 1532 KB Output is correct
3 Correct 37 ms 2936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 1604 KB Output is correct
2 Correct 34 ms 2928 KB Output is correct
3 Correct 38 ms 2908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 1500 KB Output is correct
2 Correct 39 ms 2936 KB Output is correct
3 Correct 38 ms 2936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 1528 KB Output is correct
2 Correct 34 ms 2940 KB Output is correct
3 Correct 38 ms 2908 KB Output is correct