E Two Matchings
题解链接:https://blog.csdn.net/m0_43448982/article/details/107431309
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=2e5+10; 5 ll a[maxn]; 6 ll dp[maxn]; 7 int main() 8 { 9 int T; 10 scanf("%d",&T); 11 while(T--){ 12 int n; 13 scanf("%d",&n); 14 for(int i=1;i<=n;i++){ 15 scanf("%lld",&a[i]); 16 dp[i]=0; 17 } 18 sort(a+1,a+n+1); 19 dp[4]=a[4]-a[1]; 20 dp[6]=a[6]-a[1]; 21 dp[8]=dp[4]+a[8]-a[5]; 22 for(int i=10;i<=n;i+=2){ 23 dp[i]=min(dp[i-4]+a[i]-a[i-3],dp[i-6]+a[i]-a[i-5]); 24 } 25 printf("%lld\n",dp[n]*2); 26 } 27 return 0; 28 }