公式

C - Sake or Water 解説 by en_translator


In the following, let us refer to the amount of transparent liquid in each cup as the cup’s capacity.
Let \(A'_1,A'_2,\ldots, A'_N\) \((A'_1\geq A'_2\geq \cdots\geq A'_N)\) be the capacities of the cups in descending order, which can be obtained by sorting the given capacities \((A_1,A_2,\ldots,A_N)\) in descending order.

First, given a set of choice of cups, let us consider the amount of sake he is guaranteed to drink.
If he chooses \((N-K)\) cups or less, that is, when he chooses no more cups than the number of water cups, then it is possible that all cups contain water, so the amount of sake he can drink for sure is \(0\) milliliters.
Otherwise, the amount of the sake he drinks is minimized when the \((N-K)\) cups with largest capacities contain water, and the other contain sake. In that case, the amount of sake he can drink is the sum of capacities of all cups except for the \((N-K)\) cups with largest capacities among the cups he chooses.
Note that when sorting the cups by capacities, cups with the same capacity can be arranged in any order without affecting the amount of sake he can drink.

Next, when the number of cups \(M\) that he chooses is fixed \((1\leq M\leq N)\), let us consider the maximum possible “amount of sake that he is guaranteed to drink” (over all choices of \(M\) cups).
If \(M\leq N-K\), the amount is \(0\) milliliters regardless of the choice, so the maximum value is also \(0\) milliliters.
Now assume \(M>N-K\). The amount of sake he can drink is the total capacities of the \((M-(N-K))\) cups with sake among the chose cups. Moreover, in the case considered above where the amount of sake he can drink is minimized, the \((N-K)\) cups with the largest capacities among the \(N\) cups can never be “the cups with sake among the chosen cups,” because if those cups are not chosen, then they do not satisfy the conditions anyway, and even if they are chosen, they are all within the top \((N-K)\) largest cups, which always contain water in the case above. Therefore, the total amount of sake he can drink is the sum of capacities of the cups with the \((N-K+1)\), \((N-K+2)\), \(\ldots\), and \(M\)-th capacities, which is \((A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{M})\) milliliters. This value is achievable: he can choose the \(1\), \(2\), \(\ldots\), \(M\)-th cups with the largest capacities to drink \((A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{M})\) milliliters of sake.
Hence, the amount of sake he can drink for sure when he chooses \(M\) cups \((1\leq M\leq N)\) is \(0\) milliliters if \(M\leq N-K\) and \((A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{M})\) milliliters otherwise.

Thus, it is sufficient to print the minimum \(M\) that makes this value greater than or equal to \(X\) (or print \(-1\) if no integers satisfy it).
Here, trying to compute \((A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{i})\) independently for each \(i=1,2,\ldots, N\) costs \(O(N^2)\) time at worst, exceeding the execution time limit. But noticing that \(A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{i}=(A’_{N-K+1}+A’_{N-K+2}+\cdots+A’_{i-1})+A'_i\), we can add \(A'_i\) to the sum for \((i-1)\) to obtain the sum for \(i\). This way, the sum can be obtained in \(O(1)\) time for each \(i\), and the overall complexity is \(O(N)\). Including the initial sorting, the problem can be solved in a total of \(O(N\log N)\) time, which is fast enough.
Therefore, the problem has been solved.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main(void){
	int n, k;
	long long x, y = 0;
	cin >> n >> k >> x;
	vector<long long>a(n);
	for(int i=0;i<n;i++)cin>>a[i];
	sort(a.begin(),a.end(),greater<long long>());
	int ans=0;
	for(int i=0;i<n;i++){
		ans++;
		if(i<(n-k))continue;
		y+=a[i];
		if(y>=x)break;
	}
	if(y<x)cout << -1 << endl;
	else cout << ans << endl;
	return 0;
}

投稿日時:
最終更新: