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
思路
颠覆认知的区间dp。
用一个三维数组dp[a][b][c],a表示枚举的区间的左端点,b表示枚举的区间的右端点,c用0或1分别表示运送完该区域后送餐员停在在区间[a~b]的左右端点,dp[i][j]的最小值是min(dp[i][j][0],dp[i][j][1])即在左右端点处的最小值,然后针对每个情况推出状态转移方程:
对于dp[i][j][[0],可以从dp[i+1][j][0]与dp[i+1][j][1]两个状态推出,送完每个区域外部区域的愤怒值每段时间会增加sum[i]+(sum[n]-sum[j]),而走过的路程为x[i+1]-x[i]与x[j]-x[i],则增值为(sum[i]+sum[n]-sum[j])(x[i+1]-x[i])v,也就是说,一部分的状态转移方程为:
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum[n]-sum[j])(r[i+1].x-r[i].x));
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum[n]-sum[j])(r[j].x-r[i].x));
同理,dp[i][j][1]也可以得到如下状态:
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum[n]-sum[j-1])(r[j].x-r[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum[n]-sum[j-1])(r[j].x-r[j-1].x));
这里乘v没放在状态转移方程中是因为据说放在方程中数据会溢出。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
struct node
{
int x,b;
}r[1007];
bool cmp(node a,node c)
{
return a.x<c.x;
}
int sum[1007];
int dp[1007][1007][2];
int main()
{
int n,x,v;
while(scanf("%d %d %d",&n,&v,&x)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d %d",&r[i].x,&r[i].b);
}
n++;
r[n].x=x;
r[n].b=0;
sort(r+1,r+1+n,cmp);
int sta;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1]+r[i].b;
if(r[i].x==x)
{
sta=i;
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j][0]=dp[i][j][1]=INF;
}
}
dp[sta][sta][0]=dp[sta][sta][1]=0;
for(int i=sta;i>=1;i--)
{
for(int j=sta;j<=n;j++)
{
if(i==j)continue;
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(sum[i]+sum[n]-sum[j])*(r[i+1].x-r[i].x));
dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(sum[i]+sum[n]-sum[j])*(r[j].x-r[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(sum[i-1]+sum[n]-sum[j-1])*(r[j].x-r[i].x));
dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(sum[i-1]+sum[n]-sum[j-1])*(r[j].x-r[j-1].x));
}
}
int ans=min(dp[1][n][0],dp[1][n][1]);
printf("%d\n",ans*v);
}
return 0;
}
最后贴一下区间dp的模板代码
for(int len=1;len<=n;len++)
{//枚举区间长度
for(int i=1;i+len<=n+1;i++)
{//枚举起点
int ends=i+len-1;//终点
for(int j=i;j<ends;j++)
{//枚举间断点
dp[i][ends]=min(dp[i][ends],dp[i][j]+dp[j+1][ends]+something);
}
}
}
可以看的出来这题与模板完全不同,所以说只学形式没用啊,关键是学到这种思想!