2021牛客多校2 - Stack(单调栈+拓扑)
題目鏈接:點(diǎn)擊查看
題目大意:給出 b[i]b[i]b[i] 數(shù)組的求解過程:
Stk is an empty stack for i = 1 to n :while ( Stk is not empty ) and ( Stk's top > a[i] ) : pop Stkpush a[i]b[i]=Stk's size現(xiàn)在給出 bbb 數(shù)組的 kkk 個(gè)位置,要求構(gòu)造出一組合法的數(shù)組 aaa 作為答案
題目分析:任意解感覺沒有什么可擴(kuò)展性,所以這里主要記錄一下字典序最小的合法解該如何構(gòu)造
首先特判無解的情況,對于某個(gè)位置 b[i]b[i]b[i] 來說,如果沒有給出具體數(shù)值,那么貪心去想,令其為 b[i]=b[i?1]+1b[i]=b[i-1]+1b[i]=b[i?1]+1 一定是最優(yōu)的,如果此時(shí)仍然碰到了 b[i]?b[i?1]>1b[i]-b[i-1]>1b[i]?b[i?1]>1 的情況,那只能說明無解了
接下來一定是有解的情況,我們用單調(diào)棧維護(hù)下標(biāo)去模擬整個(gè)過程,如果 b[i]<=b[i?1]b[i]<=b[i-1]b[i]<=b[i?1] 的話,說明我們需要將單調(diào)棧進(jìn)行彈出操作,既然需要用 a[i]a[i]a[i] 去彈出,設(shè)被彈出的位置為 xxx,那么顯然不等式 a[i]<a[x]a[i]<a[x]a[i]<a[x] 成立。最后需要讓 iii 入棧,此時(shí)肯定有 a[i]>a[top]a[i]>a[top]a[i]>a[top] 成立,同時(shí)這里也涵蓋了 b[i]>b[i?1]b[i]>b[i-1]b[i]>b[i?1],即 b[i]=b[i?1]+1b[i]=b[i-1]+1b[i]=b[i?1]+1 的情況,所以討論完畢
知曉了相對大小關(guān)系后,建圖跑一遍拓?fù)渚涂梢垣@得答案了,因?yàn)槊總€(gè)點(diǎn) iii 只會入棧、出棧各一次,所以邊至多有 2?n2*n2?n 條
并且貪心去賦值的話,這樣求出來的答案一定是字典序最小的一種合法解
代碼:
// Problem: Stack // Contest: NowCoder // URL: https://ac.nowcoder.com/acm/contest/11253/K // Memory Limit: 524288 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) (x&-x) using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int ans[N],b[N],in[N],n; vector<int>node[N]; void addedge(int u,int v) {node[u].push_back(v);in[v]++; } void topo() {queue<int>q;int tot=0;for(int i=1;i<=n;i++) {if(!in[i]) {q.push(i);}}while(q.size()) {int u=q.front();q.pop();ans[u]=++tot;for(auto v:node[u]) {if(--in[v]==0) {q.push(v);}}} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int k;read(n),read(k);for(int i=1;i<=k;i++) {int x;read(x);read(b[x]);}for(int i=1;i<=n;i++) {if(!b[i]) {b[i]=b[i-1]+1;} else if(b[i]-b[i-1]>1) {return 0*puts("-1");}}stack<int>st;for(int i=1;i<=n;i++) {//b[i-1]>=b[i]:a[i]<st[b[i]]while((int)st.size()>=b[i]) {addedge(i,st.top());st.pop();}if(st.size()) {addedge(st.top(),i);}st.push(i);}topo();for(int i=1;i<=n;i++) {printf("%d ",ans[i]);}return 0; }總結(jié)
以上是生活随笔為你收集整理的2021牛客多校2 - Stack(单调栈+拓扑)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛客 - Yuki with emofu
- 下一篇: 2021牛客多校1 - Find 3-f