cf Round #652 (Div. 2) C题

C.RotionalLee

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 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≤104) — 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⋅105; 1≤k≤n) — the number of integers Lee has and the number of Lee’s friends.

The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the integers Lee has.

The third line contains k integers w1,w2,…,wk (1≤wi≤n; 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⋅105.

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
Note
In the first test case, Lee should give the greatest integer to the first friend (his happiness will be 17+17) and remaining integers to the second friend (his happiness will be 13+1).

In the second test case, Lee should give {10,10,11} to the first friend and to the second friend, so the total happiness will be equal to (11+10)+(11+10)
In the third test case, Lee has four friends and four integers, it doesn’t matter how he distributes the integers between his friends.

大致题意:有n个元素,k个人,第i个人可以选wi个元素,请问如i何分配元素使得每个人所选的元素的最大值与最小值之和最大。

题目思路:贪心,当wi为1时,应该把最大的元素分配给他,因为此时这个人所持有的元素的最大值和最小值都是这个元素,可以发挥最大的效果。对于wi != 1的元素,把剩余的(k - count(wi = 1))个最大值依次分配给他们,那么如何选择最小值呢,我们肯定希望尽量使得最小值最大,那么就想到了把最小的wi - 1个元素先 给wi最大的(可以保证剩余的最小值最大),然后把剩下最小的wi-1个给wi 次小的,依次类推(为什么给wi - 1个呢, 因为已经把最大的k个数中的其中一个属分配给他了)

以下是ac代码:

#include <iostream>
#include<bits/stdc++.h>

using namespace std;

const int maxn = 2e5 + 10;
int a[maxn], w[maxn];
int main()
{
    int t;
    scanf("%d", &t);
    while(t--){
        int n, k;
        memset(a, 0, sizeof(a));
        memset(w, 0, sizeof(w));
        scanf("%d%d", &n, &k);
        for(int i = 0; i < n; i++)
            scanf("%d", &a[i]);
        for(int j = 0; j < k; j++)
            scanf("%d", &w[j]);
        sort(a, a + n, greater<int>()); /*元素大的在前*/
        sort(w, w + k);                 /*wi小的在前*/
        long long sum = 0;
        for(int i = 0; i < k; i++){
            if(w[i] == 1){/*wi == 1,特殊情况,取两次最大值*/
                sum += 2 * a[i];
            }
            else        /*wi != 1,取一次*/
                sum += a[i];
        }
        sort(a, a + n); /*元素小的在前*/
        int j = 0;      /*下一个最小值元素的位置*/
        for(int i = k - 1; i >= 0; i--){
            if(w[i] != 1){/*第一个人选前面 a[i] - 1个最小的*/
                sum += a[j];
                j += w[i] -  1;/*取下一个元素的位置*/
            }
            else
                break;
        }
        printf("%lld\n", sum);
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值