zoukankan      html  css  js  c++  java
  • J

    https://blog.csdn.net/w_udixixi/article/details/104479288

    大意:n个数,每个数只能向上加,a[i]+1需要的时间是t[i],求使这n个数无重复数字的最少时间。

    题解:并查集+贪心。怎么用并查集呢?当一个数第一次出现时,将x和x+1连起来,当我们再次访问x时,直接找到的是x+1。贪心是将花费大的点尽量往前放,因为后出现的点肯定是让变化的,让它的花费尽可能的小就好了。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const ll N=2E5+7;
    map<ll ,ll >fa;
    struct stu{
        ll a,time;
        bool friend operator < (const stu &x,const stu &y){
            return x.time>y.time;
        }
    }arr[N];
    ll find(ll x){
        if(fa[x]==0) return x;
        else {
            ll c=fa[x];
            return fa[x]=find(c);
        }
    }
    void unit(ll x,ll y)
    {
        ll c = find(x);
        ll c1= find(y);
        if(c!=c1) fa[c]=c1;
    }
    int main()
    {
        ll n;
        cin>>n;fa.clear();
        for(ll i=1;i<=n;i++) cin>>arr[i].a;
        for(ll i=1;i<=n;i++) cin>>arr[i].time;
        sort(arr+1,arr+1+n);
        ll ans=0;
        for(ll i=1;i<=n;i++){
            ll x=find(arr[i].a);
            ans+=(x-arr[i].a)*arr[i].time;
            unit(x,x+1);
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    django 2.0 path
    Django
    ORM
    Django简介
    web框架
    HTTP协议
    web应用
    索引
    pyMysql模块
    视图、触发器、存储过程、函数
  • 原文地址:https://www.cnblogs.com/Accepting/p/12411743.html
Copyright © 2011-2022 走看看