#include<iostream>
#include<vector>
#include<algorithm>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<functional>
#define MAX 100020
#define ll long long
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
ordered_set o;
int n, k, x;
ll sol = 100000000000000000;
int a[MAX];
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cin.exceptions(cin.failbit);
cin >> n >> x; k = n - x;
for(int i=0;i<n;i++) cin >> a[i];
for(int i=0;i<n;i++) {cin >> x; a[i]-=x;}
sort(a, a+n);
for(int i=0;i<k;i++) o.insert(a[i]);
ll m = 0, c = 0, nc = 0, l, r;
if(k%2) m = k / 2;
else m = k / 2 - 1;
int mv = *(o.find_by_order(m));
for(int i=0;i<k;i++) c += abs(a[i]-mv);
ll om = mv;
sol = min(sol,c);
for(int i=0;i<(n-k);i++) {
o.erase(o.find_by_order(o.order_of_key(a[i])));
o.insert(a[i+k]);
mv = *(o.find_by_order(m));
l = a[i]; r = a[i+k];
nc = c - (abs(om-l)) + (abs(mv-r));
if(k%2==0) nc -= (mv - om);
sol = min(sol,nc);
c = nc; om = mv;
}
cout << sol;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
6008 KB |
Output is correct |
2 |
Correct |
87 ms |
6040 KB |
Output is correct |
3 |
Correct |
84 ms |
6024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
6012 KB |
Output is correct |
2 |
Correct |
87 ms |
6068 KB |
Output is correct |
3 |
Correct |
78 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
123 ms |
6156 KB |
Output is correct |
2 |
Correct |
83 ms |
5964 KB |
Output is correct |
3 |
Correct |
96 ms |
6136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
2784 KB |
Output is correct |
2 |
Correct |
86 ms |
3036 KB |
Output is correct |
3 |
Correct |
87 ms |
2808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
4748 KB |
Output is correct |
2 |
Correct |
78 ms |
2156 KB |
Output is correct |
3 |
Correct |
92 ms |
3324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
1536 KB |
Output is correct |
2 |
Correct |
89 ms |
4600 KB |
Output is correct |
3 |
Correct |
88 ms |
2852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
87 ms |
3400 KB |
Output is correct |
2 |
Correct |
94 ms |
3204 KB |
Output is correct |
3 |
Correct |
101 ms |
4316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
103 ms |
4096 KB |
Output is correct |
2 |
Correct |
140 ms |
1396 KB |
Output is correct |
3 |
Correct |
87 ms |
6008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
4600 KB |
Output is correct |
2 |
Correct |
84 ms |
6024 KB |
Output is correct |
3 |
Correct |
98 ms |
4932 KB |
Output is correct |