This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<int, int> point;
const int mod = 1e9 + 7, inf = 1e9, MAXN = 2e5 + 5;
int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;}
int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;}
int mul(int x, int y) {return (ll) x * y % mod;}
int n, k;
int a[MAXN], b[MAXN];
vector <int> v;
ll sol;
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> k;
REP(i, n) cin >> a[i];
REP(i, n) cin >> b[i];
REP(i, n) { v.pb(a[i] - b[i]); sol += abs(a[i] - b[i]); }
sort(v.begin(), v.end());
ll lftCnt = 0, lftSum = 0, rghtCnt = n - k, rghtSum = 0, pnt1 = 0, pnt2 = n - k;
REP(i, n - k)
rghtSum += abs(v[i] - v[0]);
REP(i, n){
while(pnt1 < k && pnt1 < i && abs(v[pnt1] - v[i]) > abs(v[pnt2] - v[i] ) ){
lftSum -= abs(v[pnt1] - v[i - 1]); lftCnt --;
rghtSum += abs(v[pnt2] - v[i - 1]); rghtCnt ++;
pnt1 ++; pnt2 ++;
}
lftSum += lftCnt * (i > 0 ? v[i] - v[i - 1] : 0);
rghtSum -= rghtCnt * (i > 0 ? v[i] - v[i - 1] : 0);
lftCnt ++; rghtCnt --;
sol = min(sol, lftSum + rghtSum);
}
cout << sol;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |