Codeforces C. RationalLee (思維) (Round #652 Div.2)

傳送門

題意: 給你n個數,有m個朋友,每個朋友要拿其中w[i]個數,每個人獲得的幸福值是他拿的數的最大值與最小值的和,如果只有一個數最大值最小值都是它,讓你求出所有人能獲得的最大幸福值是多少?
在這裏插入圖片描述
思路:

  • 由於每個朋友的幸福值是其得到的max和min的和,那麼就要保證其最大值經量大
  • 將a[]和w[]都先按升序排列,將依次最大的a[i]分給w值爲1的朋友,這樣他的幸福值就是max*2,貢獻值增長最快。
  • 將所有w[i]爲1的處理完後,再將a數組的max和min分配給w[i]最大的朋友,剩餘的w[i] - 2個數就分配最小的w[i] - 2個數(因爲中間的數的貢獻爲0),依次下去所求就是答案。

代碼實現:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll   mod = 1e9 + 7;
const int  N = 2e5 + 5;

int t, n, k;
int a[N], w[N];

int cmp(iint a, int b)
{
    return a > b;
}

signed main()
{
	IOS;
	cin >> t;
	while(t --){
		cin >> n >> k;
		for(int i = 0; i < n; i ++)
            cin >> a[i];
		for(int i = 0; i < k; i ++)
		    cin >> w[i];
        
		sort(a, a + n); sort(w, w + k, cmp);
		int ans = 0, p = n - 1;
		//將前k大的數分配給每個人一個
		for(int i = 0, j = n - 1; i < k; i ++, j --)
			ans += a[j];
        
		for(int i = 0, j = 0; i < k; i ++){	
			if(w[i] == 1){  //w[i]==1時,要特殊處理
                ans += a[p]; 
                p --;
                continue;
            }
			ans += a[j];	//加上w[i]的最小值
			j += w[i] - 1;	//因爲中間的數沒有貢獻,可直接略去
		}
		cout << ans << endl;
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章