Description
Farmer John decided to take a picture of his cows, and he let N (1≤n≤50,000) cows stand in a straight line, each with its coordinates (range: 0..1,000,000,000) and Race (0 or 1). Farmer John has always been fond of doing something extraordinary, of course, the photograph is no exception. He only photographed some of the cows, and the team had to be "balanced". The balanced lineup refers to the equal number of cattle in a group of cattle, race 0 and Race 1. Please figure out the widest range to balance the herd within this range. The size of the interval is the coordinates of the right-most cow in the interval minus the coordinates of the most-done cow. In the input, each race has at least one cow and no two cows have the same coordinates.
Solution
Set S[i] The number of cows that are 1 of the previous I cattle-0 of the cattle
Clearly for the legal interval (L,r],s[l]==s[r]
Sweep over the left and right of each s statistic.
Seconds of
Code
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 Const intmaxn=1e5+5;5 6 structcow{7 intDfn,dis;8 BOOL operator< (Constcow&b)9 Const{returndis<B.dis;}Ten }A[MAXN]; One intS[MAXN],L[MAXN],R[MAXN]; A intN; - - intMain () { thescanf"%d",&n); - for(intI=1; i<=n;i++) -scanf"%d%d",&a[i].dfn,&A[i].dis); -Sort (A +1, a+n+1); + - for(intI=1; i<=n;i++){ + if(A[I].DFN) s[i]=s[i-1]+1; A Elses[i]=s[i-1]-1; at intx=s[i]+N; - if(!l[x]) l[x]=i; - Elser[x]=i; - } - - intcn1=0; in for(intI=0; i<=2*n;i++) - if(L[i]&&r[i]) Ans=max (ans,a[r[i]].dis-a[l[i]+1].dis); toprintf"%d\n", ans); + return 0; -}
"Differential + prefix and" BZOJ1637: [Usaco2007 mar]balanced Lineup