Bzoj 3314: [Usaco2013 nov]crowded cows (monotonic queue)

Source: Internet
Author: User

Sweep from left to right again, maintaining a monotonically non-descending queue. Then repeat from right to left and then you can count the answers.

----------------------------------------------------------------------------

#include <bits/stdc++.h>#define REP (i, n) for (int i = 0; i < n; ++i)#define CLR (x, C) memset (x, C, sizeof (x))#define foreach (i, x) for (__typeof (X.begin ()) i = X.begin (); I! = X.end (); i++)using namespace std; const int MAXN = 50009;struct R {int p, h;inline void Read () {scanf ("%d%d", &p, &h);}BOOL Operator < (const R &t) Const {return P < T.P;}} A[MAXN];bool L[MAXN], R[MAXN];int n, D;deque<int> Q;int main () {freopen ("test.in", "R", stdin);cin >> N >> D;Rep (i, N) a[i]. Read ();sort (A, a + N);CLR (l, 0), CLR (r, 0);While (! Q.empty ()) Q.pop_back ();Rep (i, N) {r* h = A + i;While (! Q.empty () && A[q.front ()].P + D < h->p) Q.pop_front ();if (! Q.empty () && a[q.front ()].h >= 2 * h->h) l[i] = true;While (! Q.empty () && a[q.back ()].h < h->h) Q.pop_back ();Q.push_back (i);}While (! Q.empty ()) Q.pop_back ();for (int i = n-1; ~i; i--) {r* h = A + i;While (! Q.empty () && a[q.front ()].p-d > H->p) q.pop_front ();if (! Q.empty () && a[q.front ()].h >= 2 * h->h) r[i] = true;While (! Q.empty () && a[q.back ()].h < h->h) Q.pop_back ();Q.push_back (i);}int ans = 0;Rep (i, N) if (L[i] && r[i]) ans++;cout << ans << "\ n";return 0;}

----------------------------------------------------------------------------

3314: [Usaco2013 nov]crowded cows time limit: 1 Sec Memory Limit: MB
Submit: Solved: 85
[Submit] [Status] [Discuss] Description

Farmer John ' s n cows (1 <= n <= 50,000) is grazing along a one-dimensional fence. Cow i is standing at location X (i) and have height H (i) (1 <= x (i), H (i) <= 1,000,000,000). A cow feels "crowded" if there is another cow at least twice she height within distance D on she left, and also another co W at least twice she height within distance D on she right (1 <= D <= 1,000,000,000). Since crowded cows produce less milk, Farmer John would like to count the number of such cows. Please help him.

n Cattle on an axis, each cow has a height. A distance value of D is now given.

If a cow is on its left, within the range of D, if a cow is found to be at least twice times the height of the ox, it can be found on the right. The cow will feel uncomfortable.

Ask how many heads will feel uncomfortable.

Input

* Line 1:two integers, N and D.

* Lines 2..1+n:line i+1 contains the integers x (i) and H (i). The locations of all N cows is distinct.

Output

* Line 1:the number of crowded cows.

Sample Input6 4
10 3
6 2
5 3
9 7
3 6
11 2

INPUT Details:there is 6 cows, with a distance threshold of 4 for feeling crowded. Cow #1 lives at position x=10 and have height h=3, and so on.Sample Output2
OUTPUT Details:the cows at positions x=5 and x=6 is both crowded. HINT

Source

Silver

Bzoj 3314: [Usaco2013 nov]crowded cows (monotonic queue)

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.