River crossing Problem-greed

Source: Internet
Author: User

The topic is this:

Time limit:MS | Memory limit:65535 KB Difficulty:5
Describe

In the dark of the night, N-travelers came to a narrow and no-fence side of the bridge. If you don't use a flashlight, everyone is afraid to cross the bridge anyway. Unfortunately, a total of n individuals with only one flashlight, and the bridge is only narrow enough to allow two people at the same time. If they cross the bridge alone, the time required for n people is known, and if two people cross the bridge at the same time, it is the time required for the slower person to move alone. The question is, how to design a program that allows the N people to cross the bridge as quickly as possible.

Input
The
first line is an integer T (1<=t<=20) that represents the number of groups of test data
The first line of each set of test data is an integer n (1<=n<=1000) representing a total of n individuals to cross the river
The second row of each set of test data is n integer si, which indicates the time it takes the person to cross the river. (0<si<=100)
Output
the minimum time required for the output of all people crossing the river
Sample input
141 2 5 10
Sample output
17
Do this topic, really very angry, last year did the topic, now incredibly can't write out, I'm going crazy, aaaaaaaaa
Say: The idea of solving problems:
is a fool will think: back to send a flashlight of the time as little as possible, so that there are two of the best solution:
Sort by bridge time from small to large
One: Every time the fastest person (time[0]) and the slowest person at the end (Time[n]) in the past, and then the fastest that came back, kept circulating until all the past,
TIME[N]+TIME[0];
Two: The fastest two (time[0],time[1]) across the river, then the fastest back, then the slowest two people past, and then time[1] times fast people back, has been circulating .....
So the shortest time should be the minimum value of two: Min (time[n]+time[0]+time[n-1]+time[0],time[1]+time[0]+time[n]+time[1]);
The code is implemented as follows:
Import Java.util.Arrays;
Import Java.util.Scanner;

public class Main
{
static final int MAX = 1005;
static int time[] = new Int[max];
public static void Main (String []args)
{
Scanner cin = new Scanner (system.in);
int T = Cin.nextint ();
for (int i = 0; i < T; i++)
{
int N = Cin.nextint ();
for (int j = 0; J < N; J + +)
{
TIME[J] = Cin.nextint ();
}
Arrays.sort (Time,0,n);
if (N = = 1)
{
System.out.print (time[0] + "\ n");
}
else if (N = = 2)
{
System.out.print (time[1] + "\ n");
}
else if (N = = 3)
{
System.out.print (time[0]+time[1]+time[2] + "\ n");
}
Else
{
Printtime (N);
}
}
}
static void Printtime (int N)
{
int sum = 0;
int k = time[1] + time[0];
int i;
for (i = N-1; i > 2; i = i-2)
{
Sum + = Math.min (k+time[i]+time[1],time[i]+time[i-1]+2*time[0]);
}
if (i = = 1)
{
Sum + = time[1];
}
else if (i = = 2)
{
Sum + = k+time[2];
}
System.out.print (sum + "\ n");
}
}

River crossing Problem-greed

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.