Description
One night some travelers want to cross the bridge. They have only one torch. The light of a torch allows up to two travelers to cross the bridge at the same time. No torches or more than 2 people can not cross the bridge. Each traveler needs a specific time to cross the bridge, while two travelers should be slower when crossing the bridge at the same time. We want to know how long it takes for all travelers to cross the bridge at least? Example if there were 4 of them. They need to spend 6,7,10,15 minutes crossing the bridge. Demonstrates how they use 44 minutes to cross the bridge, but can they do it faster?
Input
The first row a number n represents the total number of travelers, 1 <= N <= 100,000. The next n lines indicate the time of the bridge for all travelers, from small to large, with a number not exceeding 1,000,000,000.
Output
The output of a number indicates a minimum bridge time.
Sample Input
4
6
7
10
15
Sample Output
42
(From Network):
We consider the use of the minimum time of two people its storing, the time of the large people sent to the past, there are two ways:
1. The person with the least time and the person with the greatest time past, and then the least time person to take the torch back;
2. Time minimum and the second small two people past, then the time of the youngest person to take the torch back, then the most large and the second largest two people in the past, time the second small person to take the torch back;
In order to ensure that the best, should be nothing more than these two forms, then make F[i] that the current people do not have to bridge the shortest time I have left the DP can be.
Code:
--------------------------------------------------------------------------------------------------
#include <cstdio>
#define MAXN 100005
int min (int a, int b) {return a < b? A:b;}
int n, A[MAXN], F[MAXN];
int main ()
{
freopen ("mos.in", "R", stdin);
freopen ("Mos.out", "w", stdout);
scanf ("%d", &n);
for (int i = 1; I <= n; i++) scanf ("%d", &a[i]);
for (int i = n-1; I >= 2; i--)
{
F[i] = f[i + 1] + a[1] + a[i + 1];
if (i <= n-2) f[i] = min (F[i], F[i + 2] + a[1] + a[2] * 2 + a[i + 2]);
}
printf ("%d", n >= 3? F[2] + a[2]: a[n]);
return 0;
}
--------------------------------------------------------------------------------------------------
[BZOJ2072] [POI2004] MOS Bridge