Luo Gu P1007

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 11 people through. If there are 22 people meet each other halfway line on the bridge, then they will anyway to bypass the other 22 people, only 11 people go 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 LL, soldiers can only stay in place integer coordinates. All speeds are 11 soldiers, but at some point a soldier came to the coordinates 00 or L + 1L + 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
First line: an integer LL, represents a difficult path length. Coordinates of the bridge is 11 ... LL

Second row: an integer NN, represents the number of initial stay on the bridge when soldiers

Third row: There NN integers, respectively, the initial coordinates of each soldier.

Output format
is only one line, the output of two integers, respectively troop withdrawals in single minimum time and maximum time. Two integers separated by a space character.

Input
. 4
2
. 1. 3
Output
24

Instructions / tips
Initially, no two soldiers in a same coordinates.

Meaning of the questions: In a long as there are n number of soldiers to stay away from the bridge on L single-plank bridge, the soldiers only left or right front line and the speed is 1, when the two would meet face to face soldiers turned backward, and asked the first latest fleeing soldiers were how long the bridge
ideas: the ultimate goal is to finish the line segment of length L, simulation is certainly not going to work, we encounter the situation of soldiers can be seen through the other side, it is clear that total answers to no impact, so long as we read the position, left and right, respectively, consider taking the maximum from the minimum value can be.

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
    int n,l,p;

    cin >> l >> n;

    int maxx,minx;

    maxx=minx=0;

    for(int i=1;i<=n;i++)
    {
        cin >> p;

        maxx=max(maxx,max(l-p+1,p));/// L-p-1 p 分别是向左或向右  求最大
        minx=max(minx,min(l-p+1,p));/// 求最小
    }

    cout << minx << " " << maxx << endl;

    return 0;
}

Published 54 original articles · won praise 0 · Views 1226

Guess you like

Origin blog.csdn.net/weixin_44144278/article/details/99361617