fork download
  1. #define _GLIBCXX_DEBUG 1
  2. #define _GLIBCXX_DEBUG_PEDANTIC 1
  3. #define _FORTIFY_SOURCE 2
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define lli long long int
  8. #define pb push_back
  9. #define loop(i,a,b) for(lli i=0;i<n;i++)
  10. int main()
  11. {lli n,index1,index2,num,f,el,c;
  12. cin>>n;
  13. vector<lli>v;
  14. map<lli,set<lli>>mp;
  15. loop(i,0,n)
  16. {cin>>num;
  17. v.pb(num);
  18. mp[num].insert(i);
  19. }
  20. set<lli>s;
  21. loop(i,0,n)
  22. {s.insert(v[i]);
  23. }
  24. f=0;
  25. while(f==0)
  26. {f=1;
  27. for(auto itr=s.begin();itr!=s.end();++itr)
  28. {if(mp[*itr].size()>=2)
  29. {f=0;
  30. el=*itr;
  31. break;
  32. }
  33. }
  34. if(f==1)
  35. {break;
  36. }
  37. auto it1=mp[el].begin();
  38. mp[el].erase(it1);
  39. index1=*it1;
  40. auto it2=++it1;
  41. mp[el].erase(it2);
  42.  
  43. index2=*it2;
  44. v[index1]=-1;
  45. v[index2]=2*el;
  46. s.insert(2*el);
  47. mp[2*el].insert(index2);
  48. }
  49. c=0;
  50. for(auto itr=v.begin();itr!=v.end();++itr)
  51. {if(*itr!=-1)
  52. {c++;
  53. }
  54. }
  55. cout<<c<<"\n";
  56. for(auto itr=v.begin();itr!=v.end();++itr)
  57. {if(*itr!=-1)
  58. {cout<<*itr<<" ";
  59. }
  60. }
  61. return 0;
  62. }
  63.  
Runtime error #stdin #stdout #stderr 0s 4488KB
stdin
7

3 4 1 2 2 1 1
stdout
Standard output is empty
stderr
/usr/include/c++/8/debug/safe_iterator.h:270:
Error: attempt to dereference a singular iterator.

Objects involved in the operation:
    iterator "this" @ 0x0x7ffc32868110 {
      type = __gnu_debug::_Safe_iterator<std::_Rb_tree_const_iterator<long long>, std::__debug::set<long long, std::less<long long>, std::allocator<long long> > > (mutable iterator);
      state = singular;
      references sequence with type 'std::__debug::set<long long, std::less<long long>, std::allocator<long long> >' @ 0x0x55664c03e078
    }