首页 > 其他分享> > Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020 - Elimination Round, Engine)A(模拟,并查集)
Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020 - Elimination Round, Engine)A(模拟,并查集)
作者:互联网
1 #define HAVE_STRUCT_TIMESPEC 2 #include<bits/stdc++.h> 3 using namespace std; 4 pair<int,int>a[200007]; 5 bool cmp(pair<int,int>a,pair<int,int>b){ 6 if(a.second!=b.second) 7 return a.second>b.second; 8 return a.first<b.first; 9 } 10 map<int,int>fa; 11 int find_(int x){ 12 if(!fa[x]) 13 return x; 14 return fa[x]=find_(fa[x]); 15 } 16 int main(){ 17 ios::sync_with_stdio(false); 18 cin.tie(NULL); 19 cout.tie(NULL); 20 int n; 21 cin>>n; 22 for(int i=1;i<=n;++i) 23 cin>>a[i].first; 24 for(int i=1;i<=n;++i) 25 cin>>a[i].second; 26 sort(a+1,a+1+n,cmp); 27 long long ans=0; 28 for(int i=1;i<=n;++i){ 29 int x=find_(a[i].first); 30 if(a[i].first==x){ 31 int y=find_(a[i].first+1); 32 if(x!=y) 33 fa[x]=y; 34 } 35 else{ 36 ans+=1ll*(x-a[i].first)*a[i].second; 37 int y=find_(x+1); 38 if(x!=y) 39 fa[x]=y; 40 } 41 } 42 cout<<ans; 43 return 0; 44 }
标签:623,Engine,return,int,find,fa,second,NULL,Round 来源: https://www.cnblogs.com/ldudxy/p/12365315.html