Tag:scanf within font minimum Enumeration ret iostream min ui
"BZOJ4101" [Usaco2015 open]trapped in the Haybales (Silver) Descriptionfarmer John had received a shipment of N large hay ba Les (1≤n≤100,000), and placed them at various locations along the roadconnecting of the barn with He house. Each bale J have a size Sj and a distinct position Pj giving its location along the one-dimensionalroad. Bessie The cow is currently located at position B, where there is no hay bale. Bessie the cow can move around freely along the road, even up to the position at which a Bale was located, but she cannot c Rossthrough this position. As an exception, if she runs in the same direction for D units of distance, she builds up enough speed tobreak through and Permanently eliminate any hay bale of size strictly less than D. Of course, after doing this, she might open upmore space to allow her to make a run at other hay bales, eliminating them a s well. FJ is currently re-painting he house and he barn, and wants to make sure Bessie cannot reach either one (cows and fresh PaIntdo not make a good combination!) Accordingly, FJ wants to make sure Bessie never breaks through the leftmost or rightmost haybale, so she stays effectively Trapped within the hay bales. FJ have the ability to add hay to a single bale of his choosing to Helpkeep Bessie trapped. Him determine the minimum amount of extra size he needs to add to some bale to ensure bessiestays trapped. Farmer John Put the N (1<=n<=100000) heap of hay on a straight road. The size of the pile of hay in the J is SJ, and the coordinates are PJ. The Cow Bessie is located at a point B without hay heap. The cow Bessie can move freely on the road, and can even walk to a haystack, but not through. But if she ran the distance in the same direction, she would have had enough speed to smash any haystack that was strictly less than D. Of course, after that, if she continues to move in that direction, her speed will not be zeroed. John could designate a pile of hay and increase its size, and he wondered how much he would need to increase in order to trap Bessie in the cow, or not at all. Inputthe first line of input contains N as well as Bessie ' s initial position B. Each of the next N lines describes a bale, andcontains the integers giving its size and position. All sizes and positions is in the range 1 ... 109.OutputPrint a single integer, giving the minimum amount of hay FJ needs to add to prevent Bessie from escaping. Print-1 if it is ImpossIble to prevent Bessie ' s escape. Sample Input5 7
8 1
1 4
3 8
12 15
theSample Output4
The problem: When I see this familiar topic again, think of a few years ago mad wa not only fear, I hold my breath, once again opened the topic, at this moment, I suddenly shocked the discovery-
I read the wrong question!!!
Well, the question is only to increase the size of a haystack, I have always thought that there are multiple (that is, two), and also really write an algorithm, beat the limit data is no problem!!
Go to the Chase:
In the case of increasing the haystack on the left side of the Bessie, we enumerate to the right of the Haystack J, set the enlarged size to K, the height of the hay heap number I, the hay heap size size, hay heap coordinates x, easy to list the equation
That is size[i]+x[i] the bigger the better, the premise x[i] not too small
So let's take a look at the maximum value of size[i]+x[i], then two x[i], and then update the answer.
The same is the case with increasing the hay heap on the right side of Bessie
#include <cstdio> #include <cstring> #include <iostream> #include <set> #include <algorithm >using namespace Std;const int maxn=100010;int n,m,ans;struct bale{int x,d;} S[maxn];int f[maxn];set<int> s1,s2;bool cmp (Bale A,bale b) {return a.x<b.x;} int main () {scanf ("%d%d", &n,&m), Ans=1<<30;int i,j,l,r,mid;for (i=1;i<=n;i++) scanf ("%d%d", &s[i ].d,&s[i].x); S[++n].x=m;sort (s+1,s+n+1,cmp); for (i=1;i<=n;i++) if (s[i].x==m) {m=i;break;} F[m]=-1<<30;for (i=m-1;i>=1;i--) F[i]=max (F[I+1],S[I].X+S[I].D); for (i=m+1;i<=n;i++) F[i]=max (f[i-1],s [i].d-s[i].x); for (i=m+1;i<=n;i++) {l=1,r=m;while (l<r) {mid=l+r>>1;if (S[I].X-S[MID].X<=S[I].D) r= mid;elsel=mid+1;} if (r<m) ans=min (Ans,max (0,s[i].x-f[r]));} for (i=1;i<m;i++) {l=m+1,r=n+1;while (l<r) {mid=l+r>>1;if (S[MID].X-S[I].X<=S[I].D) l=mid+1;elser=mid ;} if (l>m+1) ans=min (Ans,max (0,-s[i].x-f[l-1]));} if (ans==1<<30) printf ("-1"); elseprintf ("%d", ans); return 0;}
"BZOJ4101" [Usaco2015 open]trapped in the Haybales Silver two points