C. The Sports Festival

题目链接

C. The Sports Festival

  • 对长度为 n 的序列 a 进行重排,设 di=maxj=1iajminj=1iaj ,要使 i=1ndi 最/小;
  • n2000,1ai109

解题思路

贪心,区间dp

可以发现,最后一项不受影响,一定是最大值-最小值,且最后一项一定是最大值或最小值,以最小值为例,如果最小值不是放在最后一项,则其一定放在某个位置处,其前面的数不会受到影响,而后面的数由于最大值不变,最小值减小使得最终整体的答案变大了,最大值同理,所以可以考虑最后一项是放最小值还是最大值,可以现将数组排序,再借助这个贪心策略进行区间dp:

  • 状态表示:f[i][j] 表示区间 [l,r] 内答案的最小值

  • 状态计算:f[i][j]=min(f[l+1][r],f[[l][r1])+a[r]a[l]

  • 时间复杂度:O(n2)

代码

// Problem: C. The Sports Festival // Contest: Codeforces - Codeforces Round #715 (Div. 2) // URL: https://codeforces.com/contest/1509/problem/C // Memory Limit: 256 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) // %%%Skyqwq #include <bits/stdc++.h> //#define int long long #define help {cin.tie(NULL); cout.tie(NULL);} #define pb push_back #define fi first #define se second #define mkp make_pair using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; } template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; } template <typename T> void inline read(T &x) { int f = 1; x = 0; char s = getchar(); while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); } while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar(); x *= f; } const int N=2005; int n,a[N]; LL f[N][N]; int main() { cin>>n; for(int i=1;i<=n;i++)cin>>a[i]; sort(a+1,a+1+n); for(int len=2;len<=n;len++) for(int l=1;l+len-1<=n;l++) { int r=l+len-1; f[l][r]=min(f[l+1][r],f[l][r-1])+a[r]-a[l]; } cout<<f[1][n]; return 0; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/16079810.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示