Educational Codeforces Round 108 (Rated for Div. 2)-C. Berland Regional-题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 108 (Rated for Div. 2)-C. Berland Regional-题解相关的知识,希望对你有一定的参考价值。

Educational Codeforces Round 108 (Rated for Div. 2)-C. Berland Regional

传送门
Time Limit: 2 seconds
Memory Limit: 256 megabytes

Problem Description

Polycarp is an organizer of a Berland ICPC regional event. There are n n n universities in Berland numbered from 1 1 1 to n n n. Polycarp knows all competitive programmers in the region. There are n n n students: the i i i-th student is enrolled at a university u i u_i ui and has a programming skill s i s_i si.

Polycarp has to decide on the rules now. In particular, the number of members in the team.

Polycarp knows that if he chooses the size of the team to be some integer k k k, each university will send their k k k strongest (with the highest programming skill s s s) students in the first team, the next k k k strongest students in the second team and so on. If there are fewer than k k k students left, then the team can’t be formed. Note that there might be universities that send zero teams.

The strength of the region is the total skill of the members of all present teams. If there are no teams present, then the strength is 0 0 0.

Help Polycarp to find the strength of the region for each choice of k k k from 1 1 1 to n n n.

Input

The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \\le t \\le 1000 1t1000) — the number of testcases.

The first line of each testcase contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \\le n \\le 2 \\cdot 10^5 1n2105) — the number of universities and the number of students.

The second line of each testcase contains n n n integers u 1 , u 2 , … , u n u_1, u_2, \\dots, u_n u1,u2,,un ( 1 ≤ u i ≤ n 1 \\le u_i \\le n 1uin) — the university the i i i-th student is enrolled at.

The third line of each testcase contains n n n integers s 1 , s 2 , … , s n s_1, s_2, \\dots, s_n s1,s2,,sn ( 1 ≤ s i ≤ 1 0 9 1 \\le s_i \\le 10^9 1si109) — the programming skill of the i i i-th student.

The sum of n n n over all testcases doesn’t exceed 2 ⋅ 1 0 5 2 \\cdot 10^5 2105.

Output

For each testcase print n n n integers: the strength of the region — the total skill of the members of the present teams — for each choice of team size k k k.

Sample Input

4
7
1 2 1 2 1 2 1
6 8 3 1 5 1 5
10
1 1 1 2 2 2 2 3 3 3
3435 3014 2241 2233 2893 2102 2286 2175 1961 2567
6
3 3 3 3 3 3
5 9 6 7 9 7
1
1
3083

Sample Onput

29 28 26 19 0 0 0 
24907 20705 22805 9514 0 0 0 0 0 0 
43 43 43 32 38 43 
3083 

Note

In the first testcase the teams from each university for each k k k are:

  • k = 1 k=1 k=1:
    • university 1 1 1 : [ 6 ] , [ 5 ] , [ 5 ] , [ 3 ] [6],[5],[5],[3] [6],[5],[5],[3];
    • university 2 2 2: [ 8 ] , [ 1 ] , [ 1 ] [8],[1],[1] [8],[1],[1];
  • k = 2 k=2 k=2:
    • university 1 1 1: [ 6 , 5 ] , [ 5 , 3 ] [6,5],[5,3] [6,5],[5,3];
    • university 2 2 2: [ 8 , 1 ] ; [8,1]; [8,1];
  • k = 3 k=3 k=3:
    • university 1 1 1: [ 6 , 5 , 5 ] [6,5,5] [6,5,5];
    • university 2 2 2: [ 8 , 1 , 1 ] [8,1,1] [8,1,1];
  • k = 4 k=4 k=4:
    • university 1 1 1: [ 6 , 5 , 5 , 3 ] [6,5,5,3] [6,5,5,3];

题目大意

不同学校有不同战力的学生。
学校组队将会优先派最强战力的学生。
如果学校有 7 7 7个人,但是 3 3 3人一队,那么就只能组两队,剩下一个战力最低的学生不参加。
k k k人一队时所有学校所派学生的总战力。( k k k 1 1 1 n n n,其中 n n n学生总人数)。


输入描述

先给你一个 t t t,代表一共有 t t t组测试样例。对于每组测试样例:
先给你一个 n n n,代表所有学校共有 n n n个学生。再给你两行,每行有 n n n个数。
第一行的第 i i i个数代表学生 i i i所属的学校。
二e行的第 i i i个数代表学生 i i i的战力值。


题目分析

n n n范围是 1 1 1~ 2 ⋅ 1 0 5 2\\cdot10^5 2105,还要输出 n n n k k k人组队时的总战力,暴力肯定超时。

接下来就想如何优化

首先学校要优先派选最强的学生去参赛,所以不妨先来个排序。
输入后,对每个学校的学生各自进行排序。假如一个学校有 5 5 5个学生,战力分别位 5 , 6 , 3 , 5 , 1 5,6,3,5,1 5,6,3,5,1,那么对这个学校的学生排序后为: 1 , 2 , 5 , 5 , 6 1,2,5,5,6 1,2,5,5,6。如果 2 2 2人或 4 4 4人组队,就要少去一个人,即战力最小的第一个人不去,这个学校的总战力为 2 + 5 + 5 + 6 = 18 2+5+5+6=18 2+5+5+6=18。每次都计算肯定不行,因此提前计算好这个学校的总战力,如果少去了一个战力为 1 1 1的学生,这个学校的战力就是 总 战 力 − 1 总战力-1 1。这样还不够,我们还需要用一个前缀数组 b b b,其中 b [ i ] b[i] b[i]代表这个学校有 i i i个人不去的话所损失的总战力。

然后对于每组样例,有 n n n个人,来一个循环, i i i 1 1 1 n n n,代表 i i i人一队。第二层循环是 j j j 1 1 1 学 校 总 数 学校总数 ,分别计算学校 j j j i i i人组队的情况下,所损失的战力(假如学校 j j j l l l个人,那么 i i i人一队的话就需要少去 l % i l\\%i l%i个人,损失战力为 b [ l % i ] b[l\\%i] b[l%i])。

然后代码如下:(先别抄)

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27