题目链接
解题思路:这题规律。。。。。。无语:
#include <iostream>
#include <cstdio>
#include <stack>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
#include <deque>
#include <cmath>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <set>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define _for(i,a,b) for( int i = (a); i < (b); ++i)
#define _rep(i,a,b) for( int i = (a); i <= (b); ++i)
#define for_(i,a,b) for( int i = (a); i >= (b); -- i)
#define rep_(i,a,b) for( int i = (a); i > (b); -- i)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define hash Hash
#define next Next
#define count Count
#define pb push_back
#define f first
#define s second
using namespace std;
const int N = 2e5+10, mod = 1e9 + 7;
const long double eps = 1e-5;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x)
{
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args)
{
read(first);
read(args...);
}
int n;
ll a[N];
ll dp[N];
int main()
{
int T;
read(T);
while(T --)
{
read(n);
for(int i = 1; i <= n; ++ i) read(a[i]);
sort(a+1,a+n+1);
ll ans = 0;
for(int i = 2; i <= n; i += 2)
ans += a[i] - a[i - 1];
dp[2] = 1ll << 60;
dp[4] = a[4] + a[3] - a[2] - a[1];
dp[6] = a[6] + a[5] - a[4] + a[3] - 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]);
cout << ans + dp[n] << endl;
}
return 0;
}
本文地址:https://blog.csdn.net/weixin_45630722/article/details/107523517