Consider swapping anyway, and set the last array to a a a array
Considering the meaning of absolute value, a i a_i ai} and b i b_i bi , there must be one added and one subtracted
So if it's not fixed k k The value of k, the optimal case is equivalent to a , b a,b a. B array merge sort
front n n If n is large, give them a plus sign, followed by n n It is optimal to assign a minus sign to n numbers
So consider a couple ( a i , b i ) (a_i,b_i) (ai, bi), if the signs assigned to the optimal solution are different, there is no need to control Because in this case, the larger number must be given a positive sign, and the smaller number must be given a positive sign
So we just need to consider what a i , b i a_i,b_i ai, bi , are also assigned positive (let the set of such pairs be S 1 S_1 S1) or a pair with a negative sign at the same time (let such a set be S 2 S_2 S2)
Obviously, every time we exchange S 1 S_1 S1 # and S 2 S_2 S2 ﹤ that is, the minimum number of steps can reach the optimal solution, and it is assumed that it needs to be exchanged z z z times( z z z is S 1 S_1 S1 (number of elements in)
if k > = z k>=z k> = Z, then the optimal solution must be reached, because in the optimal solution a a There must be two in a + + +Number or two − - − sign, exchange such a pair of answers
if k < z k<z K < Z, it would be better to consider how to exchange
set up S 1 S_1 One pair in S1 ^ is a i , b i a_i,b_i ai, bi , where the smaller fraction is c c c the larger number is d d d
and S 2 S_2 The element in S2 is a j , b j a_j,b_j aj, bj, where the smaller fraction is e e e the larger number is f f f
So what are the two benefits before the exchange ( d − c ) + ( f − e ) (d-c)+(f-e) (d−c)+(f−e)
And because S 1 S_1 All elements in S1 must be greater than S 2 S_2 All elements in S2 (because they are at the front of the final order) n n n elements)
What are the benefits of these two pairs after exchange ( d − e ) + ( c − f ) (d-e)+(c-f) (d−e)+(c−f)
That is, there are more benefits after the exchange 2 ∗ ( c − f ) 2*(c-f) 2 * (c − f), that is, more 2 ∗ ( m i n ( a i , b i ) − m a x ( a j , b j ) ) \rm 2*(min(a_i,b_i)-max(a_j,b_j)) 2∗(min(ai,bi)−max(aj,bj))
So it's the same exchange k k k times, S 1 S_1 S1 , we take min ( a i , b i ) \min(a_i,b_i) min(ai, bi) is sorted from large to small, and S 2 S_2 S2 ^ in m a x ( a j , b j ) \rm max(a_j,b_j) max(aj, bj) is sorted from small to large
In this way, it must be optimal to exchange the corresponding positions from front to back
#include <bits/stdc++.h> using namespace std; #define int long long const int maxn = 1e7+10; int n,k,a[maxn],b[maxn],f[maxn]; typedef pair<int,int>p; struct RR { int val,id; bool operator < ( const RR&tmp ) const { return val>tmp.val; } }S[maxn]; int top; vector<int>S1,S2; void solve() { if( n==2 ) { if( k&1 ) swap( a[1],a[2] ); cout << abs( a[1]-b[1] )+abs( a[2]-b[2] ); exit( 0 ); } int ans = 0; for(int i=1;i<=n;i++) ans += S[i].val; for(int i=1;i<=n;i++) ans -= S[i+n].val; cout << ans; exit( 0 ); } signed main() { cin >> n >> k; for(int i=1;i<=n;i++) cin >> a[i],S[++top] = {a[i],i}; for(int i=1;i<=n;i++) cin >> b[i],S[++top] = {b[i],i+n}; sort( S+1,S+1+top ); for(int i=1;i<=top;i++) f[S[i].id] = (i<=n); for(int i=1;i<=n;i++) { if( f[i]!=f[i+n] ) continue;//Different number if( f[i] ) S1.push_back( min(a[i],b[i]) );//All positive signs else S2.push_back( max(a[i],b[i]) );//All negative signs } if( k>=S1.size() ) solve();//Perfect match sort( S1.begin(),S1.end(),[](int a,int b){ return a>b; } ); sort( S2.begin(),S2.end(),[](int a,int b){ return a<b; } ); int ans = 0; for(int i=1;i<=n;i++) ans += abs( a[i]-b[i] ); for(int i=0;i<k;i++) ans += 2*( S1[i]-S2[i] ); cout << ans; }