牛客第二場k題stack
阿新 • • 發佈:2021-07-21
1.k題stack
題意:
給你一個數組,再給你一個棧,把數組裡面的數放到棧裡面,如果棧頭大,就彈出,每次i,記錄棧的大小。現在給你一些i對應棧的大小,求出一種符合的陣列。
思路:
模擬單調棧的過程,從大到小賦值,對於要彈出的元素,一定是大的,那麼讓他從n開始賦值,對於在棧裡面的元素,可以倒敘賦值。
利用棧st記錄每次元素的下標,對於彈出的元素,直接賦值。完了以後再對棧裡面的元素賦值。
程式碼:
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define ll long long #define INF 0x7f7f7f7f #define endl '\n' #define mem(a, b) memset(a, b, sizeof(a)) #define open freopen("ii.txt", "r", stdin) #define close freopen("oo.txt", "w", stdout) #define IO \ ios::sync_with_stdio(false); \ cin.tie(0); \ cout.tie(0) #define pb push_back using namespace std; typedef pair<ll, ll> PII; const int N = 2e6 + 10; const double PI = 3.1415926535898; const ll mod = 1e9 + 7; ll a[N]; ll b[N]; stack<ll> st; int main() { ll n, k; cin >> n >> k; for (int i = 1; i <= k; i++) { int x; cin >> x; cin >> b[x]; } ll x = n; for (int i = 1; i <= n; i++) { if (b[i]) { if (b[i] > st.size()+1) { cout << -1 << endl; return 0; } while (b[i] <= st.size()) { a[st.top()]=x--; st.pop(); } } st.push(i); } while(!st.empty()) { a[st.top()]=x--; st.pop(); } for(int i=1;i<=n;i++)cout<<a[i]<<" "; return 0; }