您的位置:首页 > 其它

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

2017-07-04 15:10 190 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐