Codeforces:( Recommendations)(并查集+贪心)

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

首先按照时间从大到小排序,若时间相同,则标号小的在前面
本来想丢进优先队列里一个一个出队的,发现直接结构体排序就行了

然后用并查集维护每本书标号的祖先,比如一本书的标号为5,那他的祖先就是6
因为标号范围是1e9 ,没法初始化,而且数组也开不了这么大
所以用map,map[i]=0表示 i 的祖先是自己

代码:

#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;
}

发布了36 篇原创文章 · 获赞 4 · 访问量 1386

猜你喜欢

转载自blog.csdn.net/qq_43781431/article/details/104711470