codeforce Round #652(div2) C. RationalLee(题解)

Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought n integers, now it’s time to distribute them between his friends rationally…

Lee has n integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,,an in his backpack and he has k friends. Lee would like to distribute all integers in his backpack between his friends, such that the i-th friend will get exactly wi integers and each integer will be handed over to exactly one friend.

Let’s define the happiness of a friend as the sum of the maximum and the minimum integer he’ll get.

Lee would like to make his friends as happy as possible, in other words, he’d like to maximize the sum of friends’ happiness. Now he asks you to calculate the maximum sum of friends’ happiness.

Input
The first line contains one integer t ( 1 ≤ t ≤ 1 0 4 ) (1≤t≤10^4) (1t104)— the number of test cases.

Next 3t lines contain test cases — one per three lines.

The first line of each test case contains two integers n and k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ; 1 ≤ k ≤ n ) (1≤n≤2⋅10^5; 1≤k≤n) (1n2105;1kn) — the number of integers Lee has and the number of Lee’s friends.

The second line of each test case contains n integers a 1 , a 2 , … , a n ( − 1 0 9 ≤ a i ≤ 1 0 9 ) a_1,a_2,…,a_n (−10^9≤a_i≤10^9) a1,a2,,an(109ai109)— the integers Lee has.

The third line contains k integers w 1 , w 2 , … , w k ( 1 ≤ w i ≤ n ; w 1 + w 2 + … + w k = n ) w_1,w_2,…,w_k (1≤wi≤n; w_1+w_2+…+w_k=n) w1,w2,,wk(1win;w1+w2++wk=n) — the number of integers Lee wants to give to each friend.

It’s guaranteed that the sum of n over test cases is less than or equal to 2 ⋅ 1 0 5 2⋅10^5 2105.

Output
For each test case, print a single integer — the maximum sum of happiness Lee can achieve.
Example:
input:
3
4 2
1 13 7 17
1 3
6 2
10 10 10 10 11 11
3 3
4 4
1000000000 1000000000 1000000000 1000000000
1 1 1 1
output:
48
42
8000000000
题意:
小明有n件礼物,为 a 1 . . . . a n a_1....an a1....an的价值的礼物分给k个人,k分别想要 w 1 , . . . w k w_1,...w_k w1,...wk 件,小明分别分给他们 w i w_i wi 件。现在分给每个人的快乐值是最多价值和最少价值的之和。
题解:
1.每个分发到的有效值是最大和最小,因此尽可能的把中间的值最小化,则快乐值会更大。

#include <iostream>
#include<algorithm>
using namespace std;
const int maxn = 2e5 + 10;
long long a[maxn];
long long b[maxn];
int main()
{
    int t;
    cin >> t;
    while (t--) {
        int n,k;
        long long ans = 0;
        cin >> n >> k;
        for (int i = 0; i < n; i++) {
            cin >> a[i];
        }
        sort(a, a + n);//排序
        int t = n-1;
        int flag = 0;
        for (int i = 0; i < k; i++) {
            cin >> b[i];
            if (b[i] == 1) {
                ans += a[t] * 2;//如果为1,则分配当前最大的值
                t--;//更新最大值的下表
            }
        }
        sort(b, b + k);//排序
        //int y = k - temp;
        int l = 0;
        for (int i = k-1; i>=0; i--) {//先分配b[i]大的值,这样能去掉更多小的值。
            if (b[i] != 1) {
                ans = ans + a[t] + a[l];//加上当前最小的值和当前最大的值
                l = l + (b[i] - 1);//更新最小值的下标
                t--;//更新最大值的下标
            }
            
       }
        cout << ans << endl;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值