Sicily 13981. Cow Baseball

13981. Cow Baseball

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

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

As Farmer John watches, he observes a group of three cows (X,Y,Z) completing two successful throws. Cow X throws the ball to cow Y on her 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 have been watching.

Input

* Line 1: The number of cows, N.

* Lines 2..1+N: Each line contains the integer location of 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 is right of X, Z is right of Y, and the distance from Y to Z is between XY and 2XY (inclusive), where XY represents the distance from X to Y.

Sample Input

5
3
1
10
7
4

Sample Output

4

Hint

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

Problem Source

2015年每周一赛第六场

感觉这样做反而慢了些,也不知道是不是测试样例太弱:

#include <iostream>
#include <algorithm>
using namespace std;

int n;
int num[1005];
int ans = 0;

int binarySearchIncreaseLastSmaller(int l, int r, int target) {  // 在不下降的序列中寻找恰好比target小于等于的数出现位置,也即最后一个比target小于等于的数出现的位置
	if (n == 0) return -1;
	while (l < r - 1) {
		int m = l + ((r - l) >> 1);
		if (num[m] <= target) 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) {  // 在不下降的序列中寻找恰好比target大于等于的数出现位置,也即第一个比target大于等于的数出现的位置
	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, num + n);
	for (int i = 0; i < n; i++) {
		for (int j = i + 1; j < n; j++) {
			int lowerBound = 2 * num[j] - num[i];
			int upperBound = lowerBound + (num[j] - num[i]);
			int ls = binarySearchIncreaseLastSmaller(j + 1, n - 1, upperBound);
			if (ls >= n) continue;
			int fb = binarySearchIncreaseFirstBigger(j + 1, n - 1, lowerBound);
			if (fb <= j) break;
			ans += ls - fb + 1;
		}
	}
	cout << ans << endl;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值