Sicily-Dynamic Planning (milk delivery)

Source: Internet
Author: User

Milk Express

Description

Crystal found that his salary as a programmer was hard to support the overhead of the entire family. To increase revenue, crystal decided to get up at every morning to give milk to households in the same building as him.
Crystal lives in a strange building design, each floor has only one household. A total of N (1 <= n <= 1,000) households ordered milk. The floor where crystal is located is L (1 <=l <= 1,000,000 ).
At every morning, the milk company put the milk on the floor where crystal is located, and crystal immediately picked up all the milk and distributed the milk to the tenants who ordered the milk. As a resident, it is expected that the milk will be delivered as soon as possible. As a result, the milk company defines that the time difference between the moment a household receives the milk and the time before six o'clock that day (unit: nanoseconds) is the dissatisfaction of the household. The minimum requirement for a milkbot is to minimize the dissatisfaction of all the residents in charge.
Crystal has a lot of genius qualities to serve as a milkman. He can move one layer up or down from one level in a matter of seconds, he can pick up and distribute milk in an instant ...... However, he does not know how to minimize the sum of dissatisfaction values of the residents. Can you help a programmer? The input contains multiple groups of data. Number of groups that enter the first behavior data. In each group of data, the first row contains two positive integers n and l, (1 <= n <= 1,000) (1 <= L <= 1,000,000), and the following n rows, each row has a positive integer smaller than or equal to 1,000,000, which indicates the floor of a household whose milk is subscribed. Output only outputs a positive integer for each group of data, which is the sum of the dissatisfaction values of the smallest household. No output scheme is required. Ensure that the result is less than 2000000000. Problem-solving idea: this means that we know about dynamic planning, but we didn't know how to answer it at the beginning. I thought about it later, but the solution is still clear. For example, in the question: 1 9 10 11 19, if the courier is on the 10th floor, he tries to find it. When he leaves 9, there will be: F = f0 + (10 to 9 time) * (number of other users), F0 last time; if the idea is similar to 11. 1 -------- first open two-dimensional arrays: T1 [I] [J], T2 [I] [J], where I and j start from the express delivery floor, that is, I = J = 2. T1 [I] [J] indicates that the courier goes to I on the left, J on the right, and is now the smallest on the I (left. T2 [I] [J] indicates that the left side goes to I, the right side goes to J, and is now at the minimum of J (right side. 2 -------- so the state transition equation is: 1) when the courier is on the left I + 1: [I + 1, J] to [I, j] time is: t1 [I + 1] [J] + (s [I + 1]-s [I]) * (N-(J-I )); when the courier is on the right J: The time from [I + 1, J] to [I, j] is: t2 [I + 1] [J] + (s [J]-s [I]) * (N-(J-I); state equation: t1 [I] [J] = min (T1 [I + 1] [J] + (s [I + 1]-s [I]) * (N-(J-I), T2 [I + 1] [J] + (s [J]-s [I]) * (N-(J-I); 2) when the courier on the left I: [I, J-1] to [I, j] time is: t1 [I] [J-1] + (s [J]-s [I]) * (N-(J-I ));

When the courier in the right J: from [I, J-1] to [I, j] time is: t2 [I] [J-1] + (s [J]-s [J-1]) * (N-(J-I); state equation: t2i] [J] = min (T1 [I] [J-1] + (s [J]-s [I]) * (N-(J-I )), t2 [I] [J-1] + (s [J]-s [J-1]) * (N-(J-I); 3 ----- source code:

# Include <iostream>
# Include <algorithm>
# Include <cstring>
Using namespace STD;
Int T1 [1001] [1001], T2 [1001] [1001];
Int s [1001];
Int min (int A, int B ){
Return a <B? A: B;
}
Int main (){
Int T;
Int n, l;
Cin> T;
While (t --){
Cin> N> L;
For (INT I = 0; I <n; I ++)
Cin> S [I];
S [n ++] = L;
Sort (S, S + n );

Int p = 0;
While (s [p] <L) P ++;

Memset (T1, 0x6f, sizeof (T1 ));
Memset (t2, 0x6f, sizeof (T2 ));

T1 [p] [p] = t2 [p] [p] = 0;

For (INT I = P; I> = 0; I --){
For (Int J = P; j <n; j ++ ){
If (I <p ){
T1 [I] [J] = min (T1 [I + 1] [J] + (s [I + 1]-s [I]) * (N-(J-I), T2 [I + 1] [J] + (s [J]-s [I]) * (N-(J-I )));
}
If (j> P ){
T2 [I] [J] = min (T1 [I] [J-1] + (s [J]-s [I]) * (N-(J-I )), t2 [I] [J-1] + (s [J]-s [J-1]) * (N-(J-I )));
}
}
}
Cout <min (T1 [0] [n-1], T2 [0] [n-1]) <Endl;
}
Return 0;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.