Codeforces 1315 D. Recommendations(贪心+并查集)

Description:

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

The latest A / B 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 n — the number of news categories ( 1 n 200000 ) (1≤n≤200000) .

The second line of input consists of n integers a i a_i — the number of publications of i t h i-th category selected by the batch algorithm ( 1 a i 1 0 9 ) (1≤a_i≤10^9)

The third line of input consists of n integers t i t_i — time it takes for targeted algorithm to find one new publication of category i ( 1 t i 1 0 5 ) i (1≤t_i≤10^5) .

Output

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

Examples

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

扫描二维码关注公众号,回复: 9389977 查看本文章

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 个出版物的数量 a i a_i 和修改时间 t i t_i .让你把所有的出版物的数量都变成数量不相等的,每次数量 + 1 +1 花费时间为 t i t_i ,求最小的时间。

我们知道有相同的肯定操作时间少的那一个。所有我们按照时间从大到小,数量从小到大,排序。如果操作时间小的那个,其实就是相当于把大的给 + 1 +1 ,花的是短的时间,这样我们只要使用并查集来找是否有相同的时间,然后在一直操作那个小的就行了。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
    if (a > 9)
        Out(a / 10);
    putchar(a % 10 + '0');
}

ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll x, ll n, ll mod)
{
    if (n == 0)
        return 1;
    ll res = qpow((x * x) % mod, n / 2, mod) % mod;
    if (n & 1)
        res = (res * x) % mod;
    return res % mod;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a, p - 2, p);
}

///扩展欧几里得
ll exgcd(ll a, ll b, ll &x, ll &y)
{
    if (b == 0)
    {
        x = 1;
        y = 0;
        return a;
    }
    ll g = exgcd(b, a % b, x, y);
    ll t = x;
    x = y;
    y = t - a / b * y;
    return g;
}
const int N = 200010;
int n, m, k, q;
int ans, tmp, cnt;
int flag;
map<int, int> fa;

struct node
{
    int num;
    int t;
} a[N];

bool cmp(node a, node b)
{
    return a.t == b.t ? a.num < b.num : a.t > b.t;
}

int find(int x)
{
    return fa[x] == 0 ? x : fa[x] = find(fa[x]);
}
void mix(int x, int y)
{
    int xx = find(x);
    int yy = find(y);
    if (xx != yy)
        fa[xx] = yy;
}

int main()
{

    sd(n);
    rep(i, 1, n)
        sd(a[i].num);
    rep(i, 1, n)
        sd(a[i].t);
    sort(a + 1, a + 1 + n, cmp);
    ll ans = 0;
    rep(i, 1, n)
    {
        int res = find(a[i].num);
        if (res == a[i].num)
        {
            mix(res, res + 1);
        }
        else
        {
            ans += 1ll * (res - a[i].num) * a[i].t;
            mix(res, res + 1);
        }
    }
    pld(ans);
    return 0;
}

发布了679 篇原创文章 · 获赞 410 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/qq_43627087/article/details/104474752