[关闭]
@Moritz 2019-03-29T08:13:50.000000Z 字数 837 阅读 476

L2-005 集合相似度

PTA 模拟 C++ 所有文稿


关键词:集合超时

  1. /*10:42-10:55 最后一个测试超时*/
  2. #include <iostream>
  3. #include <set>
  4. #include <algorithm>
  5. #include <iterator>
  6. #include <cstdio>
  7. using namespace std;
  8. const int maxn=55;
  9. int n;
  10. set<long long> a[maxn];
  11. int main(){
  12. cin>>n;
  13. for(int i=1;i<=n;i++){
  14. long long m,x;
  15. cin>>m;
  16. for(int j=0;j<m;j++){
  17. cin>>x;
  18. (a[i]).insert(x);
  19. }
  20. }
  21. int k;
  22. cin>>k;
  23. for(int i=0;i<k;i++){
  24. int x,y,sn=0,tn=0;
  25. cin>>x>>y;
  26. //set<long long> s,t; 第二次修改为用两个集合分别存数,在计算size,也超时了
  27. for(auto it=a[x].begin();it!=a[x].end();it++){//这次直接算数 全通过了
  28. if (a[y].count(*it)) sn++;
  29. }
  30. //for(auto it=a[y].begin();it!=a[y].end();it++) t.insert(*it);
  31. /*
  32. 一开始使用的函数,超时了
  33. set_union(a[x].begin(),a[x].end(),a[y].begin(),a[y].end(),inserter(t,t.begin())); set_intersection(a[x].begin(),a[x].end(),a[y].begin(),a[y].end(),inserter(s,s.begin()));
  34. */
  35. float r=1.0*sn/(a[x].size()+a[y].size()-sn)*100;
  36. printf("%.2f%%\n",r);
  37. }
  38. system("pause");
  39. return 0;
  40. }

2019.3.17

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注