傳送門:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289
emmmmmm……
海底大作戰大魚吃小魚小游戲。?大概就是……①棧空:向左右,最終必然生存(嘛~畢竟速度都是一樣的,位置靠左向左游,當然追不上啦~)
? ? ? ? ? ? ? ? ? ? ? ②棧非空:a.向右游,進棧;b.向左游,判斷與棧中魚的大小(直至向左游的魚被吃或必然生存到最后),棧中的大,魚的數量-1,不對棧做處理,直接跳出循環,棧中的 ? ? ? ? ? ? ? ? ? ? ? ? ? ? 魚小,出棧,數量-1,繼續循環
#include<iostream> #include<algorithm> #include<vector> #include<string.h> #include<stack> using namespace std; typedef long long ll; const int MAX=5e4+5; string s; int n,t; int main() {while(cin>>n){stack<int>fish;int cnt=n,v,d;for(int i=0;i<n;i++){cin>>v>>d;if(d==1) fish.push(v);else if(d==0){while(!fish.empty()){if(v>fish.top()) {fish.pop();cnt--;}else {cnt--;break;}}}}cout<<cnt<<endl;} return 0; }
?