C. Berland Regional | Edu Round 108 Div.2

LONGNEW·2021년 7월 12일
0

CP

목록 보기
37/155

https://codeforces.com/contest/1519/problem/C
시간 2초, 메모리 256MB

input :

  • t (1 ≤ t ≤1000)
  • n (1 ≤ n ≤ 2⋅105)
  • u1, u2, …, un (1 ≤ ui ≤ n)
  • s1, s2, …, sn (1 ≤ si ≤ 109)

output :

  • For each testcase print 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명으로 팀을 구성 할 때 얻을 수 있는 수치를 출력하시오.

조건 :

  • Polycarp knows that if he chooses the size of the team to be some integer k, each university will send their k strongest (with the highest programming skill s) students in the first team, the next k strongest students in the second team and so on. If there are fewer than k students left, then the team can't be formed. Note that there might be universities that send zero teams.
    Polycarp이 선택한 k인원에 따라서 각 대학교는 가장 잘하는 k명씩 팀을 구성해서 보낼 것이다. 이때, k보다 적은 인원이 남은 경우에는 팀을 구성하지 못한다. 그렇기에 0명을 보낼 수도 있다.

대학별

대학별로 가장 강한 놈들이 다르다. 일단 여러개의 대학을 분류해둬야 한다.
이걸 저장하는 것에는 딕셔너리를 스는게 좋을 것 같다. 그리고 리스트로 그 안에 학생들을 저장하자

가장 잘하는 애들 순서대로 보낼 거다. 그러니까 내림차순으로 정렬한 다음에
모든 학생들의 합을 저장 해두자.

k

k명을 고르는 것도 중요하다. 남는 경우를 생각해야 한다.

이를 나머지를 구해서 가장 뒤에서부터 자르는 형식을 사용하자.

반복?

합을 구해놓은 상황에서는 반복문이 필요없다.
어차피 이 순서대로 팀을 구성할 거라서 팀을 못 구성하는 인덱스를 찾아 그 합을
정답으로 보내도록 하자.

import sys

t = int(sys.stdin.readline())
for _ in range(t):
    n = int(sys.stdin.readline())
    univ = list(map(int, sys.stdin.readline().split()))
    skill = list(map(int, sys.stdin.readline().split()))
    schools = dict()

    for i in range(0, n):
        if univ[i] not in schools:
            schools[univ[i]] = [skill[i]]
        else:
            schools[univ[i]].append(skill[i])

    for item in schools.keys():
        schools[item].sort(key=lambda x:-x)
        prev = 0
        temp = []

        for skills in schools[item]:
            prev += skills
            temp.append(prev)

        schools[item] = temp

    ans = [0] * (n + 1)
    for item in schools.values():
        for i in range(1, len(item) + 1):
            remain = len(item) % i
            if remain == 0:
                ans[i] += item[-1]
            else:
                ans[i] += item[(-1-remain)]
    print(*ans[1:])

0개의 댓글