【CF1073B】Vasya and Books(模拟)

题意:给你一个栈里书的编号,每次能捞出栈顶的一本书,每次询问捞出某本编号的书需要捞几次

n<=2e5

思路:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<vector>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned int uint;
14 typedef unsigned long long ull;
15 typedef pair<int,int> PII;
16 typedef vector<int> VI;
17 #define fi first
18 #define se second 
19 #define MP make_pair
20 #define N   210000
21 #define MOD 1000000007
22 #define eps 1e-8 
23 #define pi acos(-1)
24 #define oo 1e18
25 
26 int a[N],b[N],c[N],inq[N],stk[N];
27 
28 
29 int main()
30 {
31     int n;
32     scanf("%d",&n);
33     for(int i=1;i<=n;i++) scanf("%d",&a[i]);
34     for(int i=1;i<=n;i++) scanf("%d",&b[i]);
35     int top=n;
36     for(int i=1;i<=n;i++) 
37     {
38         stk[i]=a[n-i+1];
39         inq[i]=1;
40     }
41     for(int i=1;i<=n;i++)
42     {
43         c[i]=0;
44         while(inq[b[i]]&&stk[top]!=b[i])
45         {
46             c[i]++;
47             inq[stk[top--]]=0;
48         }
49         if(inq[b[i]]){c[i]++; inq[stk[top--]]=0;}
50     }
51     for(int i=1;i<=n-1;i++) printf("%d ",c[i]);
52     printf("%d\n",c[n]); 
53     return 0;
54 }

猜你喜欢

转载自www.cnblogs.com/myx12345/p/9853779.html