Luo Gu P1007 single-plank bridge

Luo Gu P1007 single-plank bridge

link

https://www.luogu.org/problem/P1007

topic

Topic background

War has entered a critical time. You are transport team leader, he is leading transport troops delivering supplies to the front lines. Do the same boring questions like transport tasks. You want to find some stimulation, so order your soldiers to the front of a single-plank bridge to enjoy the scenery while you stay under the bridge to enjoy the soldiers. The soldiers were very angry because of this single-plank bridge is narrow, it can only accommodate one person through. If there are two people meet each other halfway line on the bridge, then they may wish to bypass the two individuals to each other, there can be only one person back under the bridge, let the other people get through. However, more than one person at the same time stay in the same position.

Title Description

Suddenly, you receive a message sent from headquarters, enemy bombers flying toward the single-plank bridge being where you are! For security, your troops have taken down a difficult path. Single-plank bridge length L, the soldiers can only stay in place integer coordinates. Speed ​​all the soldiers are 1, but at some point a soldier came to the coordinates 0 or L + 1 position, he left the single-plank bridge.

Each soldier has an initial direction to face, they will travel at a constant speed in that direction, does not change the direction of their own way. However, if two soldiers meet face to face, they can not pass each other with each other, so they were turned to continue walking. He turned and does not require any time.

Because of previous anger, you can not control your soldiers. Even, you can not even face the initial direction of each soldier do not know. So, you want to know how much time you need a minimum of troops could withdraw from single-plank bridge. In addition, the headquarters also arrange to block the enemy's attack, so you also need to know how much time you need to complete withdrawal of the troops most difficult path.

Input Format

The first line: an integer L, represents the length of the single-plank bridge. Coordinate the bridge is 1 ... L

The second line: an integer N, the number of soldiers remaining in the initial bridge

Third row: there are N integers, respectively, the initial coordinates of each soldier.

Output Format

Only one line, the output of two integers, respectively, the minimum time and maximum troop withdrawal in single time. Two integers separated by a space character.

Sample input and output

Input # 1

4
2
1 3

Output # 1

2 4

Description / Tips

Initially, no two soldiers in a same coordinates.

Data range N≤L≤5000.

Thinking

Topic okay, ideas are relatively clear, the only point to note is that two soldiers encounter U-turn and they staggered through the same time, it is only necessary to modify min to max.

Code

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int l,n;
    cin>>l>>n;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    
    int mintime=0;
    for(int i=0;i<n;i++)
    {
        mintime = max(mintime,min(a[i],l+1-a[i]));
    }
    
    int maxtime=0;
    for(int i=0;i<n;i++)
    {
        maxtime = max(maxtime,max(a[i],l+1-a[i]));
    }
    
    cout<<mintime<<" "<<maxtime;
    return 0;
} 

Guess you like

Origin www.cnblogs.com/blogxjc/p/11301133.html