"Differential + prefix and" BZOJ1637: [Usaco2007 mar]balanced Lineup

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.