Description
John keeps a cow named Joseph. Once she went to herd cattle and came to a very long field where N patches of grass grew luxuriantly. We can think of grassland as a number of points on the axis. Joseph was so excited to see the grass that he wanted to eat it all. So it began to walk around and graze. John and Joseph started at position p. Joseph's moving speed is a unit of time and a unit distance. Unfortunately, if grass is not eaten for a long time, it will corrupt. We define the corruption value of a pile of grass as the total time from Joseph to eat it. Joseph didn't want to eat too much rotten grass. He asked John to arrange a route for him so that after he had eaten all the grass, the total value of rot was the smallest. John's maths is terrible. She doesn't know what to do. Can you help her?
Input
- Line 1 : Two space-separated integers: N and L. N<=1000
- Lines 2..N+1: Each line contains a single integer giving the position P of a clump (1 <= P <= 1,000,000).
Output
- Line 1: A single integer: the minimum total staleness Bessie can achieve while eating all the clumps.
Sample Input
4 10
1
9
11
19INPUT DETAILS:
Four clumps: at 1, 9, 11, and 19. Bessie starts at location 10.
Sample Output
44
OUTPUT DETAILS:
Bessie can follow this route:
start at position 10 at time 0
move to position 9, arriving at time 1
move to position 11, arriving at time 3
move to position 19, arriving at time 11
move to position 1, arriving at time 29
giving her a total staleness of 1+3+11+29 = 44. There are other
routeswith the same total staleness, but no route with a smaller one.44
Title Solution
Actually, this translation is very different from usaco.
Let f[i][j][0] and f[i][j][1] be the minimum corruption values of i~j stop at I and i~j stop at j, respectively
Think about N^3 all morning. Is the enumeration length + start point + breakpoint
In this way, the end point can be calculated according to the starting point + length, but it seems to be the practice of N^3.
??????? It turns out that direct N^2 is OK.
Because each f[i][j] inherits the nearest haystack, there is no need to enumerate breakpoints at all.
So the equation becomes
f[x][y][0]=min(f[x+1][y][0]+(d[x+1]-d[x])(n-y+x),f[x+1][y][1]+(d[y]-d[x])(n-y+x));
f[x][y][1]=min(f[x][y-1][0]+(d[y]-d[x])(n-y+x),f[x][y-1][1]+(d[y]-d[y-1])(n-y+x));
Why multiply (n-y+x)? Because (y-x) is the number of grasslands from x+1 to y, this section has been finished.
Then all but these will be affected. The number of grasslands affected is n-(y-x).
In this way, multiply and add...
Oh, the space card is a little tight. Consider scrolling. I won't do it when I'm lazy.
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
int f[1100][1100][2];//Having eaten the first i~j hay, stop at the edge of i/j
int n,k;
int d[1100];
int main()
{
memset(f,63,sizeof(f));
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)scanf("%d",&d[i]);
sort(d+1,d+1+n);
for(int i=1;i<=n;i++)f[i][i][0]=f[i][i][1]=abs(d[i]-k)*n;
for(int i=2;i<=n;i++)//Enumeration length
{
for(int x=1;x<=n-i+1;x++)
{
int y=x+i-1;
f[x][y][0]=min(f[x+1][y][0]+(d[x+1]-d[x])*(n-y+x),f[x+1][y][1]+(d[y]-d[x])*(n-y+x));//(n-y+x) grasslands that have not yet been eaten, which naturally affect them
f[x][y][1]=min(f[x][y-1][0]+(d[y]-d[x])*(n-y+x),f[x][y-1][1]+(d[y]-d[y-1])*(n-y+x));
}
}
printf("%d\n",min(f[1][n][0],f[1][n][1]));
return 0;
}