C. The Sports Festival
观察发现 我们显然选择一个数字开始后 我们拿周围的数字显然存在最优解(sort过)
这样就很金典了 n=2000 我们显然可以暴力区间dp
然后将转移只用从拿左边数字 和 右边数字O(1)转移即可
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 998244353;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
int dp[2010][2010];
void solve() {
int n;cin>>n;
map<int,int>mp;
vector<int>a(n+1);
for(int i=1;i<=n;i++){
cin>>a[i];
mp[a[i]]++;
}
vector<int>b,c;b.push_back(0),c.push_back(0);
for(auto [x,y]:mp){
b.push_back(x),c.push_back(y);
}
int m=b.size()-1;
for(int i=0;i<=m;i++)for(int j=0;j<=m;j++)dp[i][j]=INF;
for(int i=1;i<=m;i++)dp[i][i]=0;
for(int len=2;len<=m;len++){
for(int i=1,j=i+len-1;j<=m;j++,i++){
dp[i][j]=min(dp[i][j],dp[i][j-1]+(b[j]-b[i])*c[j]);
dp[i][j]=min(dp[i][j],dp[i+1][j]+(b[j]-b[i])*c[i]);
}
}
cout<<dp[1][m]<<endl;
}
signed main(){
fast
int t;t=1;//cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}
标签:const,数字,int,715,back,Codeforces,push,Div
From: https://www.cnblogs.com/ycllz/p/16828385.html