Sicily 13981. Cow Baseball

Source: Internet
Author: User

13981. Cow Baseball Constraints

Time limit:1 secs, Memory limit:256 MB

Description

Farmer John's N Cows (3 <= n <=) is standing in a row with each located at a distinct position on the number line. They is practicing throwing a baseball around, getting ready for an important game against the cows on the neighboring F Arm.

As Farmer John watches, he observes a group of three cows (x, y, Z) completing both successful throws. Cow X throws the ball to Cow y on she right, and then Cow y throws the ball to Cow Z on her right. Farmer John notes that the second throw travels at least as far and no more than twice as far as the first throw. Please count the number of possible triples of cows (x, y, z) that Farmer John could has been watching.

Input

* Line 1:the number of cows, N.

* Lines 2..1+n:each line contains, the integer location of a, a single cow (an integer in the range 0..100,000,000).

Output

* Line 1:the number of triples of cows (x, y, z), where Y are right of X, Z are right of y, and the distance from Y to Z are b Etween XY and 2XY (inclusive), where XY represents the distance from X to Y.

Sample Input
5311074
Sample Output
4
Hint

In the sample, there is 5 cows, at positions 3, 1, ten, 7, and 4. The four possible triples is the cows as positions 1-3-7, 1-4-7, 4-7-10, and 1-4-10.

Problem Source

2015 every Monday match sixth game

It feels slower to do this, and I don't know if the test sample is too weak:

#include <iostream> #include <algorithm>using namespace Std;int n;int num[1005];int ans = 0;int Binarysearchincreaselastsmaller (int l, int r, int target) {//In the non-descending sequence, look for the occurrence of a number that is exactly less than or equal to the target. The last position that is less than or equal to the target appears if (n = = 0) return-1;while (L < r-1) {int m = L + ((r-l) >> 1); if (Num[m] <= Targ ET) L = m;else r = m-1;} if (Num[r] <= target) return R;else if (Num[l] <= target) return l;else return n; int Binarysearchincreasefirstbigger (int l, int r, int target) {//In the non-descending sequence, look for the occurrence of a number that is exactly greater than or equal to the target. This is the position where the first number that is greater than or equal to the target appears if (n = = 0) return-1;while (L < r) {int m = L + ((r-l) >> 1); if (Num[m] < target) L = m + 1;else r = m;} if (Num[r] >= target) return r;else return-1;} int main () {Std::ios::sync_with_stdio (false), CIN >> n;for (int i = 0; i < n; i++) cin >> num[i];sort (NUM, n Um + N); for (int i = 0, i < n; i++) {for (int j = i + 1; j < N; j + +) {int lowerbound = 2 * num[j]-Num[i];int Upper Bound = Lowerbound + (Num[j]-num[i]); int ls = Binarysearchincreaselastsmaller (j + 1, n-1, upperbound); if (LS >= n) continue;int fb = Binarysearc Hincreasefirstbigger (j + 1, n-1, lowerbound); if (FB <= j) Break;ans + = LS-FB + 1;}} cout << ans << endl;return 0;}

Sicily 13981. Cow Baseball

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.