Codeforces Round #623D. Recommendations并查集

D. Recommendations
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
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.

Examples
inputCopy
5
3 7 9 7 8
5 2 5 7 5
outputCopy
6
inputCopy
5
1 2 3 4 5
1 1 1 1 1
outputCopy
0
Note
In the first example, it is possible to find three publications of the second type, which will take 6 seconds.

In the second example, all news categories contain a different number of publications.

题意:给出n本书的数量 和 增加一本这类书籍的代价 问将这个n种不同种的书数量都置为各不相同的情况 问最小代价是多少

思路:首先最小代价用贪心的思想就是越大代价增加的次数应该越少 然后就是如何判断n本书的数量是不是不相同 这里用了个不一样的并查集

#include 

using namespace std;

const int N = 200005;

#define LL long long
struct node
{
    int num;
    int time;
}a[N];

bool cmp(node a,node b)
{
    if(a.time != b.time)
        return a.time > b.time;

    return a.num > b.num;
}

map<int,int> fa;///范围来有1e9 所以开map当做fa数组

int findx(int x)
{
    if(fa[x] == 0)///如果还是初始状态返回他本身
        return x;
    return fa[x] = findx(fa[x]);
}

void join(int x,int y)
{
    int xx = findx(x);
    int yy = findx(y);

    if(xx != yy)
        fa[xx] = yy;
}

int main()
{
    int n;

    cin >> n;

    for(int i = 1;i <= n;i ++)
        cin >> a[i].num;

    for(int i = 1;i <= n;i ++)
        cin >> a[i].time;

    sort(a + 1,a + 1 + n,cmp);

    LL res = 0;

    for(int i = 1;i <= n;i ++)
    {
        int father = findx(a[i].num);///找最初的状态

        if(father != a[i].num)
            res += 1LL*(father - a[i].num)*a[i].time;

        join(father,father + 1);///每次+1代表数量要加一
    }

    cout << res << '\n';

    return 0;
}

你可能感兴趣的:(补题,数据结构)