显然如果同一个方向爬的不会相互吃掉,同时不会掉头,则只需要考虑从另外一方爬过来的蚂蚁吃穿的情况,从右往左爬的蚂蚁会最先和从左往右爬的蚂蚁中最靠右的蚂蚁最先相遇,符合栈的特点,将从左往右爬的蚂蚁放进栈,如果从右来的蚂蚁能把当前栈中的蚂蚁全部吃掉,则最后能活下去,如过不能全部吃点,吃掉几个pop掉几个,最后栈中剩下的就是从左往右爬能活下来的(这样写会不会太血腥了一点...)
#include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ int n,u,d,res=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d%d",&u,&d); if(d)s.push(u); else{ while(!s.empty()){ if(s.top()>u)break; else s.pop(); } if(s.empty())res+=1; } } res+=s.size(); cout<<res; }
#include<bits/stdc++.h> using namespace std; stack<int> s; int main(){ int n,u,d,res=0; scanf("%d",&n); for(int i=0;i<n;i++){ scanf("%d%d",&u,&d); if(d)s.push(u); else{ while(!s.empty()){ if(s.top()>u)break; else s.pop(); } if(s.empty())res+=1; } } res+=s.size(); cout<<res; }