Topic link: E.Two Matchings
Title
Let you construct two sequences p, q of length n. The two sequences have different bits, and satisfy pi ≠ i and ppi = i {p_i≠i and p_{p_i} = i}pi=i and ppi=i . Given you a sequence a, you can calculate a value(∑ i = 1 nabs (ai − api)) / 2 {(\sum_{i=1}^{n}abs(a_i-a_{p_i}))/2}(∑i=1nabs(ai−api) ) / 2 Find the smallest sum of calculated values among p and q that meet the conditions.
answer
This question first analyzes the sequence of p and q. The question gives pi ≠ i and ppi = i {p_i≠i and p_{p_i} = i}pi=i and ppi=i , in fact, the sequence of p, q is equivalent to one, two, three...n swapping positions once. Since n is an even number, there will be no such thing as a position swapping multiple times.
If required(∑ i = 1 nabs (ai − api)) / 2 {(\sum_{i=1}^{n}abs(a_i-a_{p_i}))/2}(∑i=1nabs(ai−api) ) / 2 is the smallest, then we can easily determine a sequence first, that is, after sequence a is sorted, every two adjacent ones can be exchanged.
So how to determine the second smallest value?
If n=4, then the sequence of the next smallest value is: 4 1 3 2
If n is 6, then the sequence of the next smallest value is: 6 3 2 5 4 1
At first I was thinking why not when n=6: 3 1 4 2 6 5, and finally found that this is the same as the sequence we constructed for the first time: 2 1 4 3 6 5 The last two digits are the same, which is not suitable for the purpose.
Now that we know the minimum value of n=4 and n=6, do we still need to enumerate the minimum value of n=8,10...?
Of course not, gcd (4, 6) = 2 {gcd(4,6)=2}gcd(4,6)=2. According to Pei Shu's theorem in number theory,4 k 1 + 6 k 2 = gcd (4, 6) = 2 {4k_1+6k_2=gcd(4,6)=2}4 k1+6k2=gcd(4,6)=2 is solvable, then it means thatthe sum of multiples of 4 and multiples of 6 must be a multiple of 2.
The implication is that the minimum value of 4 and 6 is known, and the remaining n (even number) can be solved by recursion.
Let dp[i]: the minimum value of the sum of the previous i numbers.
Initialization:
dp[2]=inf
dp[4]=a[4]+a[3]-a[2]-a[1]
dp[6]=a[6]+a[5]-a[4 ]+a[3]-a[2]-a[1]
Then the state transition equation is:
dp[n]=min(dp[i-4]+a[i]+a[i-1]-a[ i-2]-a[i-3],
dp[i-6]+a[i]+a[i-1]-a[i-2]+a[i-3]-a[i-2 ]-a[i-1])
Code
#include<iostream>
#include <sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x
//#define int long long
const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const ll inf=1e18;
const int maxn=2e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
ll a[maxn];
ll dp[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n; scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
sort(a+1,a+1+n);
ll sum=0;
for(int i=2;i<=n;i+=2) sum+=a[i]-a[i-1];
dp[2]=inf;
dp[4]=a[4]+a[3]-a[2]-a[1];
dp[6]=a[6]+a[5]+a[3]-a[4]-a[2]-a[1];
for(int i=8;i<=n;i+=2)
{
dp[i]=min(dp[i-4]+(a[i]+a[i-1]-a[i-2]-a[i-3]),
(dp[i-6]+a[i]+a[i-1]-a[i-2]+a[i-3]-a[i-4]-a[i-5]));
}
sum+=dp[n];
printf("%lld\n",sum);
}
}