题意

$n$ 个数中选出最多对使得它们差的绝对值大于 $K$

$n\leq10^6,K\leq10^9$

题解

一个结论:升序下最后一定是砍两半。双指针贪心即可。

代码

const int N = 1e6 + 5;

inline ll Read() {
	ll x = 0, f = 1;
	char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = -f, c = getchar();
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + c - '0', c = getchar();
	return x * f;
}

namespace Main {
	int n, K, ans;
	int a[N];
	int main () {
		n = Read(), K = Read();
		for (int i = 1; i <= n; i++) a[i] = Read();
		for (int i = 1, j = n / 2 + 1; i <= n / 2 && j <= n; i++, j++) {
			if (a[j] - a[i] >= K) ans++;
			else i--;
		}
		printf ("%d\n", ans);
		return 0;
	}
}

int main () {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	Main::main();
	return 0;
}
EOF

评论

暂无评论

发表评论

可以用@mike来提到mike这个用户,mike会被高亮显示。如果你真的想打“@”这个字符,请用“@@”。

博客信息

作者
Jayun
时间
2023-11-10 17:44:21