51nod 1289 大鱼吃小鱼(栈模拟)
将往左的鱼看成静止的,就模拟往右的鱼吃与被吃。
如果往右的鱼存在,那么往栈里加,被吃了栈里推出,栈为空那么往左的鱼不会被吃,跳过好了。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stack>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
int a[N];
int main()
{
int n;
scanf("%d",&n);
stack<int> Q;
int ans = 0;
for(int i = 0; i < n; i++)
{
int a,to;
scanf("%d%d",&a,&to);
if(to == 0 && Q.empty())
continue;
else if(to == 0 && !Q.empty())
{
while(!Q.empty())
{
if(Q.top() < a)
{
Q.pop();
ans++;
}
else
break;
}
if(!Q.empty())
ans++;
}
else
Q.push(a);
}
printf("%d\n",n-ans);
return 0;
}