链接:
https://codeforces.com/problemset/problem/1519/C
题意:
两个数组,第一个是每个学生所在学校,第二个是每个学生的能力。给每个学生分组,每个学校小组内人数必须相等。最后对于每个小组成员数量,计算其能力总值。
代码如下:
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#include<string>
#include<string.h>
#include<random>
using namespace std;
typedef long long ll;
ll u[200003];
vector<ll>v[200003];
pair< ll, ll > pos[200003];
int main() {int T;cin >> T;while (T--) {int n;cin >> n;for (int i = 1; i < 200003; i++) {v[i].clear();}for (int i = 1; i <= n; i++) {cin >> u[i];}ll s;for (int i = 1; i <= n; i++) {cin >> s;v[u[i]].push_back(s);}for (int i = 1; i <= n; i++) {sort(v[i].rbegin(), v[i].rend());pos[i].first = v[i].size();pos[i].second = i;for (int j = 1; j < v[i].size(); j++) {v[i][j] += v[i][j - 1];}}sort(pos+1, pos+1 + n);int p = 1;for (int i = 1; i <= n; i++) {while (p <= n && pos[p].first < i) {p++;}ll sum = 0;for (int j = p; j <= n; j++) {ll to = pos[j].first - pos[j].first % i;sum += v[pos[j].second][to-1];}cout << sum << " ";}cout << endl;}return 0;
}