欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

2020牛客多校第3场:Two Matchings[找规律+dp]

程序员文章站 2022-03-29 17:46:33
题目链接解题思路:这题规律。。。。。。无语:#include #include #include #include #include #include #include #include #include #include

题目链接


解题思路:这题规律。。。。。。无语:
2020牛客多校第3场:Two Matchings[找规律+dp]


#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

相关标签: 思维dp