Hackerrank "Poisonous Plants"

Source: Internet
Author: User

I had the same BFS idea as one of the AC code This:because dying pattern are spead from the initial dying ones:)

#include <iostream>#include<queue>#include<vector>using namespacestd;intMain () {intN CIN >>N; Vector<int> P (N +1, -1); //Dancing Linksvector<int> Pre (N +1); Vector<int> Nex (N +1); //Markersvector<int> Day (N +1);  for(inti =0; i < N; i + +) {cin>>P[i]; }            //Fill Initial Queuequeue<int>Q;  for(inti = n1; i >0; i--) {Pre[i]=i-1; Nex[i]=i+1; if(P[i] > p[i-1]) {Day[i]=1;        Q.push (i); }    }        intAns =0;  while(!Q.empty ()) {        intCur=Q.front ();        Q.pop (); Ans=Day[cur]; //Dis-link Current Indexpre[nex[cur]]=Pre[cur]; Nex[pre[cur]]=Nex[cur]; if(P[nex[cur]) >P[pre[cur]]) {Day[nex[cur]]= day[cur]+1;        Q.push (Nex[cur]); }} cout<< ans <<Endl; return 0;}

Hackerrank "Poisonous Plants"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.