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 Bi Displeasure 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.
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 Nlines 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.
For each test case please output a single number, which is the minimal sum of Displeasure Index. One test case per line.
5 1 0
1 1
2 2
3 3
4 4
5 5
55
其实一开始真的完全懵逼,还以为会是一个贪心题==,然后感觉贪不出来,又开在了dp专题里面,不免就想到了自己每次写的毫无理由的区间dp,接下来就是找动态转移方程了。先把外卖小哥所在的点加进去,然后根据所在地点从小到大的排序,然后外卖小哥所在的地点开始向左向右扫描
dp[i][j][0]表示送完i到j的外卖客户的最小不开心值,此时停在i点;dp[i][j][1]表示送完i到j的外卖客户的最小不开心值,此时停在j点;动态转移方程有四个:
dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][0] + (no[i+1].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][1] + (no[j].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][1] + (no[j].x - no[j-1].x) *(sum[n]-sum[j-1]+sum[i-1]));
dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][0] + (no[j].x - no[i].x) *(sum[n]-sum[j-1]+sum[i-1]));
#include<stdio.h>
#include<iostream>
#include<set>
#include<queue>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
struct node{
int x , b ;
}no[1005];
int dp[1005][1005][2];
bool cmp(node x , node y){
return x.x < y.x;
}
int sum[1005];
int n , v , x;
int main()
{
while(~scanf("%d%d%d" , &n , &v , &x)){
for(int i = 1 ; i <= n ; i ++)
scanf("%d%d" , &no[i].x , &no[i].b);
no[++n].x = x ; no[n].b = 0 ;
sort(no + 1 , no + n + 1 , cmp);
sum[0] = 0 ;
for(int i = 1 ; i <= n ; i ++){
sum[i] = sum[i - 1] + no[i].b;
}
for(int i = 0 ; i <= n ; i ++){
for(int j = 0 ; j <= n ; j ++){
dp[i][j][0] = dp[i][j][1] = inf;
}
}
int mid = 0;
for(int i = 1 ; i <= n ; i ++){
if(no[i].x == x){
mid = i ;
dp[i][i][0] = dp[i][i][1] = 0;
break;
}
}//初始化起点,即送餐开始位置
for(int i = mid ; i > 0 ; i --){//分别向左向右dp
for(int j = mid ; j <= n ; j ++){
if(i == j)
continue;
dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][0] + (no[i+1].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
dp[i][j][0] = min(dp[i][j][0] , dp[i+1][j][1] + (no[j].x - no[i].x) *(sum[n]-sum[j]+sum[i]));
dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][1] + (no[j].x - no[j-1].x) *(sum[n]-sum[j-1]+sum[i-1]));
dp[i][j][1] = min(dp[i][j][1] , dp[i][j-1][0] + (no[j].x - no[i].x) *(sum[n]-sum[j-1]+sum[i-1]));
}
}
cout<<(min(dp[1][n][1] , dp[1][n][0]))*v<<endl;//最后可以停在最左边或者最右边
}
return 0;
}