manijuana's blog

By manijuana, history, 5 years ago, In English

I'm having trouble implementing a typical dijikstra problem (this one). I'm having a TLE verdict in the last subtask. My submission can be found here. I'm using a set to store the nodes and having a compare function which sorts on the basis of the shortest distances stored in vector d. Everytime a relaxation is encountered, it removes the node, relaxes and changes d[] for that node and enters it again into the set. Can anyone help me get rid of this TLE and tell me why I'm getting one? Thanks in advance.

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By manijuana, history, 5 years ago, In English

I'm facing a very weird problem and I don't quite understand it. The problem is of finding the number of distinct integers in an array of numbers specifically This CSES problem. Note that the limit of N is 2e5 Hence O(N) and O(NlogN) both solutions should pass. However the funny part is that my O(N) solution gets TLE in one of the subtasks and O(NlogN) gets AC. I'm really confused and would appreciate if someone could tell me what is going on. Thanks in advance.

My O(NlogN) solution: Inserting elements in a set int main(){ ll n; cin>>n;
set s;
ll x;
for(ll i=0;i<n;i++) {cin>>x; s.insert(vec[i]);}
cout<<s.size()<<endl;
return 0;
}

My O(N) solution: Insterting elements in an unordered_map
int main(){
ll n;
cin>>n;
ll cnt=0;
ll x;
unordered_map <ll,ll> umap;
for(ll i=0;i<n;i++) {
cin>>x;
if(!umap[x]){
umap[x]++;
cnt++;
}
}
cout<<cnt<<endl;
return 0;
}

Full text and comments »

  • Vote: I like it
  • +5
  • Vote: I do not like it