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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章