咦,怎么没有STL_Stack的题解?

解释:玄学RE

因为手工模拟栈的过程中,即使栈内元素个数为0,也会有
s[top]==0s[top] == 0 这种情况存在,因此不会继续满足s的顶元素比插入的元素大

而在STL的情况下,由于元素个数为0,因此访问s.top()s.top()是非法的,解决的方法很简单,只要最开始在栈首插入一个小于等于0的元素就可以解决了。

不得不说印度女工真的办不好事,我第一次谢了10行头文件都被撤回了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<cstdio>
#include<stack>

using namespace std;

int ans,n;
stack <int> s;

int main()
{
int i,j;
scanf("%d",&n);
int t1,t2;
s.push(-100);
scanf("%d %d",&t1,&t2);
s.push(t2);
for(i=2;i<=n;i++)
{
scanf("%d %d",&t1,&t2);
while(s.top()>=t2)
{
int t=s.top();
if(t==t2) ans++;
s.pop();
}
s.push(t2);
}
printf("%d",n-ans);
}