There are n ships and N Stakes in a pier, the length of the boat is 2*x, and the width of the pier is m,n (relative to the left bank of the wharf) in the data. There can be no overlap between the ship and the ship, that is, the bow of each ship cannot exceed the stern of the previous ship, and certainly not the two sides of the pier. Between the ship and the stake is connected by a rope, and 1 stakes can only be tied to 1 boats, one end of the rope tied to a stake, the other tied to the middle of the boat. The distance between the boat and the stake is the length of the rope required. You arrange the position of the boat according to the conditions given, so that the longest rope used is the shortest. Output this shortest length, if the pier is not in line with all ships then output-1.
For example: N = 3, X = 2, M = 16. The positions of the Three stakes are: 1 3 14. The length of the ship is 2*x = 4. You can place three ships at 2 6 14 (referring to the position in the middle of the boat) so that there is no overlap between the ship and the ship, and the longest rope used is the shortest, the length is 3, that is, the distance from the 2nd ship to the second stake. Input
Line 1th: 3 numbers N X M, separated by a space (1 <= N <= 50000, 1 <= X <= 10^9, 1 <= M <= 10^9). 2-n + 1 lines: 1 numbers per line pi, corresponding to the position of the stake (0 <= Pi <= pi+1 <= M), and the data given is ordered.
Output
The minimum value of the longest rope to output. If the docks don't fit all the ships then output-1.
Input example
3 2 161314
Output example
3
Problem-solving ideas: one sees the longest rope shortest, think of two points, indeed, the two points can be solved, but there are better O (n) method, think also quite interesting. First of all the boats are arranged from left to right, and then move them to the right, but the number of shifts is a problem, moved more, less is not the minimum, in the boat to the right of the process of moving the right stake to the minimum value of the ship's heart, canto neutralize with the minimum value, take the average of them, so that the smallest is not too small, the largestnot too big.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int maxn=50000+100;int a[maxn];int b[maxn];//Record the minimum distance between the first and the first and the stake to the heart of the ship int bbs (int x) { if (x<0) Return-x; return x;} int main () { int n,x; int m; scanf ("%d%d%d", &n,&x,&m); for (int i=0; i<n; i++) { scanf ("%d", &a[i]); } if (m/2/x<n) { printf (" -1\n"); } else { int ans=0,cur=0; for (int i=n-1;i>=0;i--) { b[i]=a[i]-(i*2*x+x); if (i<n-1) b[i]=min (b[i],b[i+1]); } for (int i=0;i<n;i++) { int temp= (a[i]-(i*2*x+x)-cur+b[i]-cur)/2; if (temp>0) { cur+=min (temp,m-n*x*2-cur); } Ans=max (Ans,bbs (a[i]-(i*2*x+x)-cur)); } cout<<ans<<endl; } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
51nod 1243 Row boat problem (exercise thinking good question)