1063. Set Similarity

二路归并

#include<iostream>
#include<set>
using namespace std;
set<int> val[60];
int main(){
  int n;cin>>n;
  for(int i=1,cnt,k;i<=n;++i){
    cin>>cnt;
    while(cnt--){ cin>>k; val[i].insert(k); }
  }
  cin>>n;
  while(n--){
    int a,b,all=0,same=0; cin>>a>>b;
    auto ia=val[a].begin(),ib=val[b].begin();
    while(ia!=val[a].end()&&ib!=val[b].end()){
      ++all;
      if(*ia==*ib)++ia,++ib,++same;
      else *ia>*ib?++ib:++ia; }//while
    all+=distance(ia,val[a].end())+distance(ib,val[b].end());
    printf("%.1lf%\n",double(same*100)/all);
  }//while
}


你可能感兴趣的:(1063. Set Similarity)