1 条题解
-
0HLBhahaqiu LV 8 MOD @ 2020-12-11 21:27:14
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;const int MAXN=52;
int f[MAXN][MAXN][2];
int d[MAXN];
int s[MAXN];
int sump;
int n,poss;void init()
{
int p;
scanf("%d%d",&n,&poss);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&d[i],&p);
s[i]=s[i-1]+p; sump+=p;
}
}inline int getd(int x,int y)
{
return abs(d[x]-d[y]);
}void DP()
{
memset(f,0x37,sizeof(f));
for(int i=1;i<=n;i++)
f[i][i][0]=f[i][i][1]=abs(d[i]-d[poss])*sump;
for(int l=2;l<=n;l++)
for(int i=1;i<=n&&i<=n-l+1;i++)
{
int j=i+l-1;
f[i][j][0]=min(f[i+1][j][0]+(sump-(s[j]-s[i]))*getd(i,i+1),
f[i+1][j][1]+(sump-(s[j]-s[i]))*getd(i,j));
f[i][j][1]=min(f[i][j-1][0]+(sump-(s[j-1]-s[i-1]))*getd(i,j),
f[i][j-1][1]+(sump-(s[j-1]-s[i-1]))*getd(j-1,j));
}
cout<<min(f[1][n][0],f[1][n][1])<<endl;
}int main()
{
init();
DP();
}
- 1