codeforces1509 C. The Sports Festival(区间dp)

题目链接
题意:
给与n个数字a1、a2、…、an。di=max(a1,a2,…,ai)−min(a1,a2,…,ai) ,对序列a重新排列,使d1+d2+⋯+dn 最小。
思路:
最容易看成是构造题了,构造了老半天没出来…
看了大佬题解,理解了老长时间才明白(蒟蒻)。排序后区间dp,dp[l][r]表示区间[l,r]的和的最小值。对于dp[l][r]可以由两个状态转移来,[l+1,r]到[l,r],[l,r-1]到[l,r],排序后s[j]表示区间最大值,s[i]表示最小值,所以dp[i][j] = s[j] - s[i] + min(dp[i + 1][j],dp[i][j - 1])。

code:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#define debug(x) cout << #x << ":" << x << endl;
#define bug cout << "********" << endl;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define fi first
#define se second
using namespace std;
typedef long long ll;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;

int n;
int s[maxn];
ll dp[2001][2001];

int main()
{
	scanf("%d", &n);
	for (int i = 1; i <= n;i++){
		scanf("%d", &s[i]);
	}
	sort(s + 1, s + n + 1);
	for (int len = 2; len <= n;len++){
		for (int i = 1; i <= n&&(i+len-1)<=n;i++){
			int j = i + len - 1;
			dp[i][j] = s[j] - s[i] + min(dp[i + 1][j], dp[i][j - 1]);
		}
	}
	cout << dp[1][n] << endl;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值