1289 大鱼吃小鱼 栈模拟 思路
题目链接
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289
思路:
- 用栈来模拟,O(n)即可
- 朝右的小鱼进栈,朝左的小鱼来攻关,看能攻几关,栈里的小鱼就GG几条,如果攻不过去,只能自己GG
代码:
#include <bits/stdc++.h>
using namespace std;
int n,dir,size,temp;
stack<int> s;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n; temp=n;
for(int i=1; i<=n; ++i) {
cin>>size>>dir;
if(dir) s.push(size);
else {
while(!s.empty()) {
if(size>s.top()) {
s.pop();
temp--;
} else {
temp--;
break;
}
}
}
}
cout<<temp<<endl;
return 0;
}