题解:CF38E Let's Go Rolling!
CF38E Let's Go Rolling!
考虑
先将弹珠按坐标升序排序。
定义
考虑转移。由于
其中第一个弹珠必须选择原地不动:
推导一下
用前缀和优化即可。
最终答案为
记得开 long long。
代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF=1e18;
const int N=3005;
int n;
int f[N],s[N];
struct node{
int x,c;
bool operator < (const node& b) const {return x < b.x;}
}a[N];
inline int calc(int i,int j){return s[i]-s[j]-a[j].x*(i-j);}
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].c;
sort(a+1,a+n+1);
for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i].x;
int ans=INF;
for(int i=1;i<=n;i++){
f[i]=INF;
if(i==1) f[i]=a[i].c;
for(int j=1;j<i;j++) f[i]=min(f[i],f[j]+calc(i-1,j)+a[i].c);
ans=min(ans,f[i]+calc(n,i));
}
cout<<ans;
return 0;
}