CF938B Run For Your Prize 题解

Content

有两个人,一个在 \(1\) 处,一个在 \(10^6\) 处,在他们之间有 \(n\) 个奖品,第 \(i\) 个奖品在 \(a_i\) 处。一开始在 \(1\) 处的人每秒可向右移动 \(1\) 个单位,一开始在 \(10^6\) 处的人每秒可向左移动 \(1\) 个单位,我们认为他们经过了礼物就算得到礼物,并且不需要耗费时间,求他们得到所有礼物的最短时间。

数据范围:\(1\leqslant n\leqslant 10^5,1<a_i<10^6\)。

Solution

我们可以将满足 \(1< a_i\leqslant 5\times10^5\) 的奖品都划给在 \(1\) 处的人去取,将满足 \(5\times 10^5< a_i<10^6\) 的奖品都划给在 \(10^6\) 处的人去取,很容易发现这是最优的方案,然后我们看在 \(1\) 处的人取到他要取的最右边的奖品需要耗费的时间和在 \(10^6\) 处的人取到他要取的最左边的奖品需要耗费的时间的较大值,这其实就等同于求距离了。

Code

int n, x, a[100007], b[100007];

int main() {
	getint(n);
	_for(i, 1, n) {getint(x); if(x >= 1 && x <= 500000) a[++a[0]] = x; else b[++b[0]] = x;}
	sort(a + 1, a + a[0] + 1), sort(b + 1, b + b[0] + 1);
	int ans = max((a[0] ? a[a[0]] - 1 : 0), (b[0] ? 1000000 - b[1] : 0));
	writeint(ans);
	return 0;
}
上一篇:单链表反转总结篇


下一篇:CF999A Mishka and Contest 题解