【kuangbin带你飞-区间DP-4】F - Food Delivery ZOJ - 3469

题目链接

题目

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.

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 XiX_{i} ( XiX_{i} >= 0 ), BiB_{i} ( BiB_{i}>= 0), which are described above.

You can safely assume that all numbers in the input and output will be less than 23112^{31}-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

思路:

给出一个数组,其中一个位置是快餐店,其他位置是订单地点,现在一个外卖员要从快餐店出发,最后送完所有的订单。顾客等待外卖的不开心度正比于等待时间。求最小化所有顾客的最小不开心度。
区间dp,考虑当前dp[l][r][0]为区间【l,r】中且最后停在区间的左边,dp[l][r][1]表示停在右边。因为在直线上送外卖,相当于直线覆盖,直线覆盖下的点都可以直接给送达。相关的状态转移见代码。需要注意排序。
另外,在从一个区间点直线到达另一个区间点的时候,除了已经扫描覆盖的区间外,其他为覆盖的区间都有累加的不开心度,需要使用前缀和来进行累加,详情见代码。
最后答案为min(dp[1][n][0],dp[1][n][1])*V
这里我觉得有一点不好的地方,题目中说了速度是V1V^{-1},而输入的时候写的是V,最后结果应该出V,但是最后却是乘V,有点歧义,因为就算最后是乘V,但是V又是整数,说明原始的速度每分钟才不到1米,这显然不和常理。。。

代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1000 + 5;
const int inf = 0x3f3f3f3f;
typedef pair<int, int> ii;
int dp[maxn][maxn][2];
int sum[maxn];
int n, v, x;
int main() {
    ios::sync_with_stdio(false);//优化
    while(cin >> n >> v >> x) {
        vector<ii> p;
        for(int i = 0; i < n; i++) {
            int a, b;
            cin >> a >> b;
            p.emplace_back(a, b);
        }
        n++;
        p.emplace_back(x, 0);
        sum[0] = 0;
        //memset(sum,inf,sizeof(sum));
        memset(dp, inf, sizeof(dp));
        sort(p.begin(), p.end());
        int s = 0;
        for(int i = 1; i <= n; i++) {
            sum[i] = sum[i - 1] + p[i - 1].second;
            if(p[i - 1].first == x && p[i - 1].second == 0)
                s = i;
        }
        dp[s][s][0] = dp[s][s][1] = 0;
        for(int k = 2; k <= n; k++) {
            for(int i = 1; i + k - 1 <= n; i++) {
                int j = i + k - 1;
                dp[i][j][0] = min(dp[i][j][0], dp[i + 1][j][0] + (sum[n] - sum[j] + sum[i]) * (p[i].first - p[i - 1].first));
                dp[i][j][0] = min(dp[i][j][0], dp[i + 1][j][1] + (sum[n] - sum[j] + sum[i]) * (p[j - 1].first - p[i - 1].first));
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][1] + (sum[n] - sum[j - 1] + sum[i - 1]) * (p[j - 1].first - p[j - 2].first));
                dp[i][j][1] = min(dp[i][j][1], dp[i][j - 1][0] + (sum[n] - sum[j - 1] + sum[i - 1]) * (p[j - 1].first - p[i - 1].first));
            }
        }
        cout << min(dp[1][n][0], dp[1][n][1])*v << endl;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章