Background
Orz5-1 match
Describe
Give 2 sequence A={a[1],a[2],...,a[n]},b={b[1],b[2],...,b[n]}, choose K elements from a, B to one by one pairs (can not follow the order of the original in the sequence), and make all pairs of element difference of the absolute value of the maximum.
For example, select A[p[1]],a[p[2]],......,a[p[k]] and B[q[1]],b[q[2]],......,b[q[k]], where the p sequence of element 22 is not the same, the Q sequence of element 22 is not the same, then the answer is |a[p[1]]-b[q[1]]| +|a[p[2]]-b[q[2]]|+ ... +|a[p[k]]-b[q[k]]|, the task now is to maximize the answer.
Format input Format
The 1th behavior of the input 2 positive integer n,k, indicating the length of the sequence and the number of elements to be selected.
Line 2nd contains n positive integers, which describe a sequence.
Line 3rd contains n positive integers, which describe the sequence B.
Output format
The output includes only a non-negative integer, which is the maximum result.
Note: The answer may be more than 2^31-1, use int64 or long long (use the printf output to store the results in "%i64d") type.
Example 1 Sample Input 1
4 22 5 6 31 4 6 7
Sample Output 1
10
Limit
For 10% of the data, there are k≤5,n≤10;
For 30% of the data, there are n≤100;
For 50% of the data, there are n≤1000;
For 100% of the data, there is k≤n≤100000;a[i],b[i]≤1000000.
time limit 1s.
Tips
Pairing (2,7), (6,1) results are |2-7|+|6-1|=10.
It seems that greed is only suitable for high IQ people to play,
╮(╯▽╰)╭,
Don't cry, cry what ah.
Not good to see all.
Topic you!
Say what i64d!!!
It's lld!!!.
Beat you to death!
mmp!
Look at the code, the typical greedy, see to understand. :
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd;intn,k;Long Longans;inta[100002],b[100002];Long LongABSS (Long LongXLong Longy) { if(x>y)returnX-X; Else returnY-x;}intMain () {scanf ("%d%d",&n,&k); for(intI=1; i<=n;++i) scanf ("%d",&A[i]); for(intI=1; i<=n;++i) scanf ("%d",&B[i]); Sort (a+1, a+n+1); Sort (b+1, b+n+1); intl1=1, l2=1, r1=n,r2=N; for(intI=1; i<=k;++i) {if(ABSS (A[L1],B[R2]) >ABSs (A[r1],b[l2])) {ans+=ABSs (A[L1],B[R2]); L1++; R2--; } Else{ans+=ABSs (A[r1],b[l2]); R1--; L2++; }} printf ("%lld", ans); return 0;}
If you're not happy, I'll share it with the handsome fool on the right.
you see, he is so good-looking, so affectionate look at you, you still sad?
Oh, really! The picture stared at him for five seconds to laugh.
Everything's going to be over.
VIOJS 1662 Max Pairing