HackerRank "Poisonous Plants"

时间:2022-12-10 22:32:51

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

#include <iostream>
#include <queue>
#include <vector>
using namespace std; int main()
{
int N; cin >> N;
vector<int> P(N + , -); // Dancing Links
vector<int> pre(N + );
vector<int> nex(N + ); // Markers
vector<int> day(N + ); for(int i = ; i < N; i ++)
{
cin >> P[i];
} // Fill initial queue
queue<int> q;
for(int i = N-; i > ; i--)
{
pre[i]=i-;
nex[i]=i+;
if(P[i] > P[i-])
{
day[i]=;
q.push(i);
}
} int ans = ;
while(!q.empty())
{
int cur=q.front(); q.pop();
ans = day[cur]; // Dis-link current index
pre[nex[cur]]=pre[cur];
nex[pre[cur]]=nex[cur]; if(P[nex[cur]] > P[pre[cur]]){
day[nex[cur]] = day[cur]+;
q.push(nex[cur]);
}
}
cout << ans << endl;
return ;
}