"BZOJ4099" Trapped in the Haybales STL

Source: Internet
Author: User

"BZOJ4099" Trapped in the Haybalesdescriptionfarmer John had received a shipment of N large hay bales (1≤n≤100,000), and PL aced them at various locations along the road leading to his barn. Unfortunately, he completely forgets that Bessie the cow was out grazing along the road, and she could now be trapped within The bales! Each bale J have a size Sj and a position Pj giving its location along the one-dimensional road. 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 Ross through this position. As an exception, if she runs in the same direction for D units of distance, she builds up enough speed to break through an D permanently eliminate any hay bale of size strictly less than D. Of course, after doing this, she might open-up more space-to-make-a run at other hay bales, eliminating them as well. Bessie can escape to freedom if she can eventually break through either the leftmost or rightmost hay bale. Please Compute the total area of the road consisting of real-valued starting positions from which Bessie cannot escape. Farmer John received N Hay bales (1≤n≤100000), and place them in different places on a road. Unfortunately, he forgot that Bessie was grazing along the road, and she may now be trapped in a hay bag!   Each package has a size SJ and position PJ. Bessie could walk freely along the road, even to a bundle of bales, but she had to go through the position. If she runs the distance in the same direction, she can break through the hay bales that are less than the size of D. Of course, after doing so she can develop a wider space to break through other hay bales.   If Bessie can break through the most left or right hay bag, she can regain her new life. Please calculate the area of the road that Bessie cannot escape.  inputthe first line of input contains N. Each of the next N lines describes a bale, and contains-integers giving its size and position, each in the range 1 ... 10^9. All positions is distinct. The first line of the input contains an integer n. The following n lines each line describes a bundle of hay bales, containing two integers, each representing its size and position, and the range of each number is 1 ... 10^9. All the locations are different.  outputprint a single integer, giving the area of the road from which Bessie cannot escape.  outputs an integer that indicates the size of the road that could not be escaped by Beth 。  sample Input5
8 1
1 4
8 8
7 15
4
Sample Output -

Solution: This kind of thinking problem is difficult to do, but the question of the net to see.

If we sort the haystack by coordinates, for a certain interval [i,j], adding Bessie can break through all the hay in [i+1,j-1], but it's not going to break [i,j], which means there must be no more hay heap in [i,j] than i,j.

So, we'll sort the haystack by size from big to small, one by one to the axis, each to join a haystack, to find its precursor and successor, and to determine whether the two sections can break, if not, mark the interval (I am lazy with the difference array to mark)

The predecessor successor can be maintained with set, but note that we also need to know the size of the haystack after the predecessor, and this is a record of the number of haystack numbers in the first place (sorted by coordinates).

#include <cstdio> #include <iostream> #include <set> #include <algorithm>using namespace std; const int Maxn=100010;int h[maxn],x[maxn],rx[maxn],rh[maxn];bool CMP1 (int a,int b) {return x[a]<x[b];} BOOL CMP2 (int a,int b) {return h[a]>h[b];} int S[maxn],sum;int N,ref[maxn],ans;set <int> ms;int Main () {scanf ("%d", &n); int i,j;for (i=1;i<=n;i++) scanf ("%d%d", &h[i],&x[i]), RX[I]=RH[I]=I;//RX,RH store is actually numbered, we are sorting the numbers sort (RX+1,RX+N+1,CMP1); for (i=1;i<= n;i++) {ref[i]=x[rx[i]];x[rx[i]]=i;} Sort (RH+1,RH+N+1,CMP2); Set<int>::iterator it;for (i=1;i<=n;i++) {It=ms.lower_bound (x[rh[i]]); if (it!= Ms.begin () &&!ms.empty ()) {it--;j=rx[*it];if (Ref[x[rh[i]]]-ref[x[j]]<=min (H[rh[i]],h[j])) {S[x[j]]++;s [x[rh[i]]]--;}} It=ms.upper_bound (X[rh[i]), if (It!=ms.end ()) {j=rx[*it];if (Ref[x[j]]-ref[x[rh[i]]]<=min (h[rh[i]],h[j))) {s[x[ rh[i]]]++;s[x[j]]--;}} Ms.insert (X[rh[i]);} for (i=1;i<=n;i++) {ans+= (sum>0) * (Ref[i]-ref[i-1]); sum+=s[i];} printf ("%d", ans); return 0;}

BZOJ4099 trapped in the haybales STL

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.