0%

Flowers

题目链接:https://ac.nowcoder.com/acm/contest/7830/J

题目大意:

有n种花,第$i $种花有$a_i$朵。现在要用m朵花组成一束花,要求每束花的中每朵花的种类各不相同,求最多组成几束花。

题目分析:

假设m=2,这就变成了一个配对问题。我们设$m=max\{a_i\},tot=sum\{a_i\}$,当$m\leq\frac {tot}{2}$,则一定是能够恰好两两配对的(花朵总数为奇数会有一个剩余),答案为$\frac{tot}{2}$。反之,数量最多的那种花一定能和其他所有花配对,此时我们只要考虑剩下的花就可以了,答案为$tot -m$。

那么对于m大于2的情况怎么办呢?很简单,只要某一束花大于等于$\frac{tot}{m}$,这种花就一定能和其他所有花配对,我们把这种花的数量从tot里删掉,再把m减1就可以了,最后的答案是$\frac{tot}m$,注意这里的tot是删完之后剩下的花的总数。

AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 10;
typedef long long ll;
int a[N], n, m;

int main() {
int T; cin >> T;
while (T--) {
cin >> n >> m;
ll tot = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
tot += a[i];
}

for (int i = 1; i <= n; ++i) if (a[i] >= tot / m) {
tot -= a[i];
m--;
}
cout << tot / m << endl;
}
}

欢迎关注我的其它发布渠道