Advertising Agency

一、题目

Masha works in an advertising agency. In order to promote the new brand, she wants to conclude contracts with some bloggers. In total, Masha has connections of n different bloggers. Blogger numbered i has ai followers.
Since Masha has a limited budget, she can only sign a contract with k different bloggers. Of course, Masha wants her ad to be seen by as many people as possible. Therefore, she must hire bloggers with the maximum total number of followers.
Help her, find the number of ways to select k bloggers so that the total number of their followers is maximum possible. Two ways are considered different if there is at least one blogger in the first way, which is not in the second way. Masha believes that all bloggers have different followers (that is, there is no follower who would follow two different bloggers).
For example, if n=4, k=3, a=[1,3,1,2], then Masha has two ways to select 3 bloggers with the maximum total number of followers:
conclude contracts with bloggers with numbers 1, 2 and 4. In this case, the number of followers will be equal to a1+a2+a4=6.
conclude contracts with bloggers with numbers 2, 3 and 4. In this case, the number of followers will be equal to a2+a3+a4=6.
Since the answer can be quite large, output it modulo 1e9+7.

从n个博主中选择k个博主,使粉丝人数最多。每个博主有ai个粉丝。结果模1e9+7

二、输入

The first line contains one integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.
The first line of each test case contains two integers n and k (1≤k≤n≤1000) — the number of bloggers and how many of them you can sign a contract with.
The second line of each test case contains n integers a1,a2,…an (1≤ai≤n) — the number of followers of each blogger.
It is guaranteed that the sum of n over all test cases does not exceed 1000.

一个整数t,表示测试案例的数量。
每个测试案例的第一行有两个整数n, k(1≤k≤n≤1000)
每个测试案例的第二行有n个整数,表示第i个博主的粉丝数量ai(1≤ai≤n)

三、输出

For each test case, on a separate line output one integer — the number of ways to select k bloggers so that the total number of their followers is maximum possible.

粉丝量最大的方案数

四、样例

input:

3
4 3
1 3 1 2
4 2
1 1 1 1
2 1
1 2

output:

2
6
1

The test case is explained in the statements.
In the second test case, the following ways are valid:
conclude contracts with bloggers with numbers 1 and 2. In this case, the number of followers will be equal to a1+a2=2;
conclude contracts with bloggers with numbers 1 and 3. In this case, the number of followers will be equal to a1+a3=2;
conclude contracts with bloggers with numbers 1 and 4. In this case, the number of followers will be equal to a1+a4=2;
conclude contracts with bloggers with numbers 2 and 3. In this case, the number of followers will be equal to a2+a3=2;
conclude contracts with bloggers with numbers 2 and 4. In this case, the number of followers will be equal to a2+a4=2;
conclude contracts with bloggers with numbers 3 and 4. In this case, the number of followers will be equal to a3+a4=2.
In the third test case, the following ways are valid:
concludes a contract with a blogger with the number 2. In this case, the number of followers will be equal to a2=2.

五、思路 + 代码

选k个博主要使粉丝量最大,不用想也是优先从粉丝多的博主选起。
把粉丝量从多到少去重排序,并标记拥有该粉丝量的博主个数。
再按多到少的顺序去选择k个博主,当选到第k个的时候将落到一个区间上,如果k在这个区间的最后一个,或者这个区间只有一个博主,那么总方案显然只有一种;如果落在一个区间的中间或者开头,并且这个区间包含的博主数大于1,那方案数就是组合数C(这个区间的总博主数,在这个区间里选择的博主数)。

本质就是考组合公式C(n,k)
这里用动态规划

代码如下:

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#define INF 1e10
#define MOD 1000000007
using namespace std;
typedef long long ll;
map<int, ll> m;
ll a[1003];
ll dp[2020][2020];
ll C(int n, int k)
{
	if (k == 0 || k == n) return 1;
	if (dp[n][k]) return dp[n][k];
	return (dp[n][k] = (C(n - 1, k - 1) + C(n - 1, k)) % MOD);
}
int main() {
	int t;
	cin >> t;
	while (t--) {
		memset(a, 0, sizeof(a));
		int n, k;
		cin >> n >> k;
		for (int i = 0; i < n; i++) {
			int x;
			cin >> x;
			a[x] ++;
		}
		vector<int> v;
		for (int i = 1002; i >= 0; i--) {
			if (a[i] != 0) {
				v.push_back(a[i]);
			}
		}
		int index = 0;
		while (k > v[index]) {
			k -= v[index++];
		}
		if (k == 0)
			cout << 1 << endl;
		else
			cout << C(v[index], k) << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值