Poj: http://poj.org/problem? Id = 1700
The simplest farmer is greedy.
Online Search Ideas:
Http://www.cnblogs.com/drizzlecrj/archive/2007/10/20/931011.html
The following is the best solution for constructing n people (n> = 1) Bridge
1) if n = 1 or n = 2, the owner crosses the bridge directly.
2) if n = 3, the fastest person can send the other two to cross the river at a time.
3) if n> = 4, set a and B as the fastest and fast travelers, the time required for crossing the bridge is a, B, and Z, Y is the slowest traveler and slow traveler. The time required for crossing the bridge is Z and Y, respectively. So
There are two ways to use the smallest two to send the largest two to the past (the last state is only the largest to send the past, the smallest to stay here ):
1) Use the smallest A to send Y and Z:Z + A + Y +;
2) first get the smallest two (B), then the smallest a returns (A), then the two largest (z), and then the second smallest B returns (B ),B + A + Z + B;
The time difference between the two methods is 2b-a-y.
When 2B> A + Y, use mode 1) Move Z and y across the bridge
When 2B <A + Y, use mode 2) Move Z and y across the bridge
When 2b = a + Y, use mode 1 or Mode 2 to move Z and y across the bridge.
In this way the problem becomes the case of N-2 travelers, recursive solution can be done.
ACCode
# include
# include
# include
using namespace STD; int s [1010]; int time (int * s, int N) {If (n = 1) return s [0]; If (n = 2) return s [1]; if (n = 3) return s [0] + s [1] + s [2]; If (n> = 4) {If (s [1] * 2> = s [0] + s [N-2]) return 2 * s [0] + s [N-1] + s [N-2] + time (S, N-2 ); else if (s [1] * 2
T; while (t --) {int N; CIN> N; memset (S, 0, sizeof (s); For (INT I = 0; I
S [I]; sort (S, S + n); cout