C - hivalric Blossom 栈模拟 stack

题目:https://vjudge.z180.cn/contest/435646#problem/C
题意:每一个请求都需要有一个优先权,现在让你用最少的优先权来满足它的条件

题解:堆栈模拟:
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int > PII;
const double eps = 1e-7;
const int N = 1e6 + 50, mod = 998244353;

int l[N], r[N], col[N];

int main()
{
    
    
    int n, m; cin >> n >> m;

    while (m--) {
    
    
        int a, b; cin >> a >> b;
        l[b] = a, r[a] = true;
    }

    stack<int > sk;
    for (int i = n; i >= 1; i--) sk.push(i); 

    for (int i = 1; i <= n; i++) {
    
    
        if (!l[i]) {
    
      // it maybe have right or not
       // if not we don't pop,  while pop
            col[i] = sk.top(); 
            if (r[i]) sk.pop();
        } else {
    
    
            col[i] = col[l[i]];  // if it has the left, be same
            if (!r[i]) sk.push(col[i]); // if it is the last, release.
        }
    }

    for (int i = 1; i <= n; i++) cout << col[i] << ' ';
    cout << endl;

    return 0;
}

Guess you like

Origin blog.csdn.net/YingShen_xyz/article/details/116243302