zoukankan      html  css  js  c++  java
  • 并查集

    就是有n组,每组的数量是num,只能增加数字,增加的代价为t,求能使所有组的数量都不同的最小代价。

    #include<bits/stdc++.h> 
    #define N 200005
    #define endl '
    ' 
    #define _for(i,a,b) for(int i=a;i<b;i++)
    using namespace std;
    typedef long long ll;  
    struct Node{
        int num; ll t;
        bool operator < (const Node &o){ 
            return t>o.t;
        }
    }a[N];
    map<int,int > F;
    int find(int k){
        if(F[k]==0){
            return k;
        }
        else return F[k]=find(F[k]); 
    }
    void link(int a,int b){
        int fa=find(a),fb=find(b);
        if(fa!=fb) F[fa]=fb;
    }
    ll res;
    int main(){    
        ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);     
        int n ; cin>>n;
        _for(i,1,n+1) cin>>a[i].num;
        _for(i,1,n+1) cin>>a[i].t;
        sort(a+1,a+n+1);
        _for(i,1,n+1){
            int tem=a[i].num;
            int to= find(tem);
            link(to,to+1);
            if( to == tem ) ;
            else { 
                res+= 1ll*(to-tem)*a[i].t;
            }
        }
        cout<<res<<endl;
        return 0;
    } 

    #include<bits/stdc++.h> #define N 200005#define endl ' ' #define _for(i,a,b) for(int i=a;i<b;i++)using namespace std;typedef long long ll;  struct Node{int num; ll t;bool operator < (const Node &o){ return t>o.t;}}a[N];map<int,int > F;int find(int k){if(F[k]==0){return k;}else return F[k]=find(F[k]); }void link(int a,int b){int fa=find(a),fb=find(b);if(fa!=fb) F[fa]=fb;}ll res;int main(){    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);     int n ; cin>>n;_for(i,1,n+1) cin>>a[i].num;_for(i,1,n+1) cin>>a[i].t;sort(a+1,a+n+1);_for(i,1,n+1){int tem=a[i].num;int to= find(tem);link(to,to+1);if( to == tem ) ;else { res+= 1ll*(to-tem)*a[i].t;}}cout<<res<<endl;return 0;} 

  • 相关阅读:
    ndt histogram_direction
    rplidar & hector slam without odometry
    点云的基本几何计算
    rplidar测试
    使用ZXing.Net生成与识别二维码(QR Code)
    C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码
    .NET 二维码生成(ThoughtWorks.QRCode)
    zxing二维码的生成与解码(C#)
    netsh interface portproxy的一个简单例子
    Redis 客户端连接
  • 原文地址:https://www.cnblogs.com/SunChuangYu/p/12364501.html
Copyright © 2011-2022 走看看