求问如何优化
查看原帖
求问如何优化
588042
astrading楼主2022/7/14 19:19

rt,蒟蒻不会优化,TLE了,求帮助

#include <cstdio>
#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <stack>

using namespace std;
const int N = 1e6 + 10;
int n;
int b[N];
bool st[N];
stack<int> s;
int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> b[i];
        st[b[i]] = true;
    }

    for(int i = n; i >= 1; i--) s.push(b[i]);

     for(int i = 1; i <= n; i++)
    {
        int as, temp = 0;
        cin >> as;
        if(!st[as]) 
        {
            cout << 0 << " "; continue;
        }    
        while(as != s.top())
        {
            st[s.top()] = false;
            s.pop();
            temp++;
        }  
        s.pop();
        cout << temp + 1 << " ";   
    }  

    return 0;
}
2022/7/14 19:19
加载中...