Codeforces :( Recommendations) (disjoint-set + greedy)

VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category i batch algorithm selects ai publications.

The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of i-th category within ti seconds.

What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can’t remove publications recommended by the batch algorithm.

Input

The first line of input consists of single integer n — the number of news categories (1≤n≤200000).

The second line of input consists of n integers ai — the number of publications of i-th category selected by the batch algorithm (1≤ai≤109).

The third line of input consists of n integers ti — time it takes for targeted algorithm to find one new publication of category i (1≤ti≤105).

Output

Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.

input
5
3 7 9 7 8
5 2 5 7 5
output
6

input
5
1 2 3 4 5
1 1 1 1 1
output
0

First descending order according to the time, if the same time, the small numbers in the foregoing
had wanted thrown into a priority queue of a dequeue, ordered structure found directly on the line

Then disjoint-set maintenance ancestors each book label, book labels such as a 5, 6 that his ancestor is
because the numbers in the range of 1e9, not initialized, and the array can not that big
so use map, map [i] = 0 are represented by their ancestors i

Code:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct qq
{
    ll val,t;
}q[200005];
struct node
{
    ll val,t;
}e[200005];
bool cmp(qq a,qq b)
{
    if(a.t>b.t)
    {return 1;}
    if(a.t==b.t)
    {
        if(a.val<b.val)
        {return 1;}
        return 0;
    }
    return 0;
}
map<ll,ll> Map;
ll getf(ll u)
{
    if(Map[u]==0)
    {return u;}
    else
    {
        Map[u]=getf(Map[u]);
        return Map[u];
    }
}
void query(ll u,ll v)
{
    ll t1,t2;
    t1=getf(u);
    t2=getf(v);
    if(t1!=t2)
    {Map[t2]=t1;}
}
int main()
{
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
    {scanf("%lld",&q[i].val);}
    for(int i=0;i<n;i++)
    {scanf("%lld",&q[i].t);}
    sort(q,q+n,cmp);
    ll ans=0;
    for(int i=0;i<n;i++)
    {
        ll op=q[i].val;
        if(getf(op)==0)
        {
            query(op+1,op);
        }
        else
        {
            ans+=(getf(op)-op)*q[i].t;
            query(getf(op)+1,getf(op));
        }
        //cout<<Map[op]<<endl;
    }
    cout<<ans<<endl;
    return 0;
}

Published 36 original articles · won praise 4 · Views 1386

Guess you like

Origin blog.csdn.net/qq_43781431/article/details/104711470