problem list
F 螞蟻
- 剛開始想的是對於序列裏面數值大小打標記之類的
- 但是想想複雜度太高
- 用棧來做模擬,選擇一個方向push,另一個方向作爲pop的判定,中途計算ans
#include <bits/stdc++.h>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1000 + 10 ;
const int inf = 0x3f3f3f3f ;
const int npos = -1 ;
const double eps = 1e-20 ;
int n, a, b, ans;
std::stack< int > s;
int main(){
while(~scanf("%d",&n)){
ans=0;
while(!s.empty()){s.pop();}
for(int i=1;i<=n;i++){
scanf("%d %d",&a,&b);
if(b){
s.push(a);
}else{
while(!s.empty() && s.top()<a){s.pop();}
ans+=s.empty();
}
}
ans+=s.size();
printf("%d\n",ans);
}
return 0;
}