Food Delivery
Time Limit: 2 Seconds Memory Limit: 65536 KB
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 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
算法分析:
题意:
有n个人的住宅分布在一条线上且互不重叠,他们同时叫了同一家店的外卖。由于这些人都想尽快拿到外卖,所以等待的时间越长,他们就越不开心(每个人都有个不开心值)。外卖店也坐落在这条直线上,为了使得这些人的不开心总值最小,外卖小哥应该怎么派送呢?外卖小哥的速度为1/v,且忽略外卖小哥把外卖交给顾客的时间。
分析:
这题我们要发现一个思路,当外卖小哥送完顾客k的外卖后,它的下一个配送对象必须为与顾客k相邻且未派送的顾客l或者顾客r。为什么?因为如果不派送l或者r,而去派送较远的顾客,那么途中就必须要经过顾客l或者r,既然都经过了,那就肯定先把外卖送给他们。
我们定义dp[x][y][0]表示区间遍历完[x,y]了,并且当前停留在x位置上,将对最终的愤怒值之和造成的贡献。
定义dp[x][y][1]表示遍历完区间[x,y],并且当前停留在y位置上,将对最终的愤怒之和造成的贡献。
下面思路则来自:
在思考题目的时候,直接思考是联系不到区间dp的。直接思考得到的一种感觉是,一旦餐厅左边的某家顾客i被送达,那么从i到餐厅位置X之间的所有顾客都是会被送达的。想到了这一点以后,其实就想通了,单独讨论某一边的路线是没有意义的,我们可以想想,这个送餐员实际送餐的过程当中,一定是先到一边,没有跑到头,就折返回另一边,通过这种方式来达到总的不满意程度最低的目的。想到这里,直接思路就走不下去了,会想着贪心,但是要顾忌到的东西太多,贪心要同时考虑每一个顾客的位置远近和不满意程度的增长快慢。
这种情况下思考问题,就可以考虑动态规划这一种只关注条件转移而不关注整体实际操作先后的算法。
但是运用普通的区间dp,如石子归并问题中的模板,会发现它的特殊性。首先它的状态转移,当区间长度加一时,并不能随意从中间断开区间,并且从左还是从右边过来转移的思路思考点不一样。
于是就要开到三维数组,第三个下标来指示状态是从左边转移还是右边。dp[i][j][k],k=0表示从左边转移,k=1表示从右边转移。
明显是要找到餐厅的位置,以它为参考点进行操作的,同时还应该注意到,dp的起点也只能是这个特殊的点。所以在读入所有顾客位置和顾客不满意度之后,同时加入餐厅位置并将餐厅位置的不满意增量设定为0,把这n+1个量进行排序。找到餐厅的位置并且用loc保存下来。
这里顺带复习对结构体变量数组排序的时候,重新定义<号可以在结构体定义当中进行:friend bool operator<(node a,node b){return a.x<b.x;}
排序完以后要对所有的dp值赋成无穷大,并把dp[loc][loc][k]赋初值成0。这里却不能把任意的dp[i][i][k]赋值成0,不然dp[i+1][j]可能会在状态转移方程中出现一些问题。具体如何这里留个疑问,这个点我也没有彻底钻透。
接下来就是状态转移了。这里要注意,这个地方的dp数组含义并不是孤立在每个状态里的,它是进行到当前状态时,会对最终结果造成的不满意值的影响。所以在状态转移的时候,先分类它是从哪个状态转移过来的,再把转移前的dp值,加上进行该段路程配送会对总不满意值造成的影响。比如从i+1配送到i,转移前是i+1到j的区间,那么除了i+1和j以及他们之间的顾客,其他顾客的不满意值都在随时间累加。这样来理解这四个状态转移方程。
dp[i][j][0]=dp[i+1][j][0]+(cos[i+1].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i]));//从左边的i+1走到i cos[i].y也是要累计起来的
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j]-sum[i])));//从右边的j走到i
dp[i][j][1]=dp[i][j-1][1]+(cos[j].x-cos[j-1].x)*(sum[n+1]-(sum[j-1]-sum[i-1]));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(cos[j].x-cos[i].x)*(sum[n+1]-(sum[j-1]-sum[i-1])));
代码实现:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+7;
const int MAXN = 1e3+10;
int n, v, x;
int sum[MAXN];
LL dp[MAXN][MAXN][2];
struct node //重载小于号
{
int pos, val;
bool operator<(const node &a)const{
return pos<a.pos;
}
}q[MAXN];
int main()
{
while(scanf("%d%d%d", &n,&v,&x)!=EOF)
{
for(int i = 1; i<=n; i++)
scanf("%d%d", &q[i].pos, &q[i].val);
q[++n].pos = x; q[n].val = 0; //把餐馆加进去
sort(q+1, q+1+n); //根据位置进行排序
int st; //记录餐馆的下标
sum[0] = 0;
for(int i = 1; i<=n; i++)
{
sum[i] = sum[i-1]+q[i].val;
if(q[i].pos==x) st = i;
}
for(int l = 1; l<=n; l++) //初始化
for(int r = l; r<=n; r++)
dp[l][r][0] = dp[l][r][1] = INF;
dp[st][st][0] = dp[st][st][1] = 0;
for(int len = 2; len<=n; len++)
for(int l = 1; l<=n-len+1; l++)
{
int r = l+len-1;
dp[l][r][0] = min(dp[l][r][0], dp[l+1][r][0]+1LL*v*(q[l+1].pos-q[l].pos)*(sum[l]+sum[n]-sum[r]));
dp[l][r][0] = min(dp[l][r][0], dp[l+1][r][1]+1LL*v*(q[r].pos-q[l].pos)*(sum[l]+sum[n]-sum[r]));
dp[l][r][1] = min(dp[l][r][1], dp[l][r-1][1]+1LL*v*(q[r].pos-q[r-1].pos)*(sum[l-1]+sum[n]-sum[r-1]));
dp[l][r][1] = min(dp[l][r][1], dp[l][r-1][0]+1LL*v*(q[r].pos-q[l].pos)*(sum[l-1]+sum[n]-sum[r-1]));
}
LL ans = min(dp[1][n][0], dp[1][n][1]);
printf("%lld\n", ans);
}
}