diff --git a/Medium/Day 11/solution.cpp b/Medium/Day 11/solution.cpp index 7056c69..a170887 100644 --- a/Medium/Day 11/solution.cpp +++ b/Medium/Day 11/solution.cpp @@ -1 +1,78 @@ -// Write your code here \ No newline at end of file +// Write your code here + #include +#include +#include +#include +#include +#include +#include + +using namespace std; + +inline int read() +{ + int x=0;char ch=getchar(); + while(ch<'0' || '9' f,b; +vector g[N]; + +typedef set::iterator it; + +int main() +{ + n=read(); + k=read(); + + for(int i=1;i<=n;i++) + q[p[i]=read()]=i; + for(int i=1;ik) + b.erase(p[i-k]); + + it cur=f.upper_bound(p[i]); + if(cur!=f.end()) + g[q[*cur]].push_back(i),ind[i]++; + cur=b.upper_bound(p[i]); + if(cur!=b.end()) + g[q[*cur]].push_back(i),ind[i]++; + } + + f.clear(); + for(int i=1;i<=n;i++) + if(!ind[i]) + f.insert(-i); + + int top=0; + while(f.size()) + { + int u=-(*f.begin()); + p[++top]=u; + f.erase(f.begin()); + for(int i=0,ed=g[u].size();i