1519C - Berland Regional - CodeForces Solution


brute force data structures greedy number theory sortings *1400

Please click on ads to support us..

Python Code:

def rt(n,r,m):
    dp=[[] for i in range(0,m)]
    for i in range(0,len(n)):
        dp[n[i]-1].append(r[i])
    q=[]
    p1=[[0 for i in range(0,len(dp[k]))] for k in range(0,m)]
    for i in range(0,len(dp)):
        dp[i].sort(reverse=True)
        for x in range(0,len(dp[i])):
            if(x==0):
                p1[i][x]=dp[i][x]
            else:
                p1[i][x]=p1[i][x-1]+dp[i][x]
        if(len(dp[i])>=1):
            q.append([len(dp[i]),i])
    s=0
    q.sort()
    l1=1
    r1=[0 for i in range(0,m)]

    while(s<len(q)):
        c1=0
        for k in range(s,len(q)):
            [a,b]=q[k]
            if(a>=l1):
                z1=(a-a%l1-1)
                c1=c1+p1[b][z1]
            else:
                s=s+1
        if(l1-1<m):
           r1[l1-1]=c1

        l1=l1+1

    return r1
s=int(input())
for i in range(0,s):
    s1=int(input())
    n=list(map(int,input().split()))
    r=list(map(int,input().split()))
    x=rt(n,r,s1)
    for j in x:
        print(j,end=" ")
    print("")

C++ Code:

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)

using namespace std;


int main() {
    int t;
    scanf("%d", &t);
    forn(_, t){
        int n;
        scanf("%d", &n);
        vector<int> s(n), u(n);
        forn(i, n){
            scanf("%d", &s[i]);
            --s[i];
        }
        forn(i, n){
            scanf("%d", &u[i]);
        }
        vector<vector<int>> bst(n);
        forn(i, n) bst[s[i]].push_back(u[i]);
        forn(i, n) sort(bst[i].begin(), bst[i].end(), greater<int>());
        vector<vector<long long>> pr(n, vector<long long>(1, 0));
        forn(i, n) for (int x : bst[i]) pr[i].push_back(pr[i].back() + x);
        vector<long long> ans(n);
        forn(i, n) for (int k = 1; k <= int(bst[i].size()); ++k)
            ans[k - 1] += pr[i][bst[i].size() / k * k];
        forn(i, n)
            printf("%lld ", ans[i]);
        puts("");
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation