2017上海市高校程序设计邀请赛_F

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(){
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章