When we are focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At this time, we may call for food delivery.
Suppose there are N people living in a straight street that is just lies on an X-coordinate axis. The ith person's coordinate is Xi meters. And in the street there is a take-out restaurant which has coordinates X meters. One day at lunchtime, each person takes an order from the restaurant at the same time. As a worker in the restaurant, you need to start from the restaurant, send food to the N people, and then come back to the restaurant. Your speed is V-1 meters per minute.
You know that the N people have different personal characters; therefore they have different feeling on the time their food arrives. Their feelings are measured by Displeasure Index. At the beginning, the Displeasure Index for each person is 0. When waiting for the food, the ith person will gain BiDispleasure Index per minute.
If one's Displeasure Index goes too high, he will not buy your food any more. So you need to keep the sum of all people's Displeasure Index as low as possible in order to maximize your income. Your task is to find the minimal sum of Displeasure Index.
Input
The input contains multiple test cases, separated with a blank line. Each case is started with three integers N ( 1 <= N <= 1000 ), V ( V > 0), X ( X >= 0 ), then N lines followed. Each line contains two integers Xi ( Xi >= 0 ), Bi ( Bi >= 0), which are described above.
You can safely assume that all numbers in the input and output will be less than 231 - 1.
Please process to the end-of-file.
Output
For each test case please output a single number, which is the minimal sum of Displeasure Index. One test case per line.
Sample Input
5 1 0
1 1
2 2
3 3
4 4
5 5
Sample Output
55还是一个状态的转移,每次有两种选择 [i,j]由[i+1,j]或者[i,j-1]转移而来,所有的子状态必须已知,所以递归。当然[i,j]处理结束的时候有可能在左边,也有可能在右边,需要再增加一维【】表示左右。最终的递归终点就是,饭店的位置,左右都是0.
#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long ll;
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1010;
/*
有前缀和,所以我们最好从1开始
*/
int dp[maxn][maxn][2];
struct node {
int x,v;
bool operator<(const node& b)const {
return x<b.x;
}
} p[maxn];
int sum[maxn];
int n,x,v;
int sst;
int solve(int st,int ed,int dir)
{
//printf("st:%d ed:%d\n",st,ed);
if(dp[st][ed][dir]!=INF) {
return dp[st][ed][dir];
}
if(st==ed)
return INF;
if(dir==0) {
int ans1=solve(st+1,ed,0)+(p[st+1].x-p[st].x)*(sum[st]+sum[n]-sum[ed]);
int ans2=solve(st+1,ed,1)+(p[ed].x-p[st].x)*(sum[st]+sum[n]-sum[ed]);
return dp[st][ed][0]=min(ans1,ans2);
} else {
int ans1=solve(st,ed-1,0)+(p[ed].x-p[st].x)*(sum[st-1]+sum[n]-sum[ed-1]);
int ans2=solve(st,ed-1,1)+(p[ed].x-p[ed-1].x)*(sum[st-1]+sum[n]-sum[ed-1]);
return dp[st][ed][1]=min(ans1,ans2);
}
}
int main()
{
while(~scanf("%d %d %d",&n,&v,&x)) {
for(int i=1; i<=n; i++) {
scanf("%d %d",&p[i].x,&p[i].v);
}
p[++n].x=x,p[n].v=0;
sort(p+1,p+n+1);
memset(dp,INF,sizeof(dp));
sum[0]=0;
for(int i=1; i<=n; i++)
sum[i]=sum[i-1]+p[i].v;
for(int i=1; i<=n; i++)
if(p[i].x==x) {
sst=i;
break;
}
dp[sst][sst][0]=dp[sst][sst][1]=0;
int ans1=solve(1,n,0);
int ans2=solve(1,n,1);
printf("%d\n",min(ans1,ans2)*v);
}
return 0;
}