有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右。游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右)。问足够长的时间之后,能剩下多少条鱼?
Input
第1行:1个数N,表示鱼的数量(1 <= N <= 100000)。 第2 - N + 1行:每行两个数A[i], B[i],中间用空格分隔,分别表示鱼的大小及游动的方向(1 <= A[i] <= 10^9,B[i] = 0 或 1,0表示向左,1表示向右)。
Output
输出1个数,表示最终剩下的鱼的数量。
Input示例
5 4 0 3 1 2 0 1 0 5 0
Output示例
2
模拟一下
One
<span style="font-size:18px;">#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;
struct fish
{
int size;
int dir;//0代表向下,1代表向上
}arr[150000];
int main()
{
int n;
while(~scanf("%d",&n))
{
int i,glag;
fish g;
stack<fish>sta;
scanf("%d%d",&arr[0].size,&arr[0].dir);
sta.push(arr[0]);
for(i=1;i<n;++i)
{
scanf("%d%d",&arr[i].size,&arr[i].dir);
g=sta.top();
if(g.dir==1&&arr[i].dir==0)
{
while(!sta.empty())
{
if(g.size<arr[i].size)
{
sta.pop();
if(!sta.empty())
g=sta.top();
else
break;
}
else
break;
if(g.dir==0)
{
sta.push(arr[i]);
break;
}
}
if(sta.empty())
sta.push(arr[i]);
}
else
sta.push(arr[i]);
}
int sum=sta.size();
printf("%d\n",sum);
}
return 0;
} </span>
Two 看的网上的代码,别人就是牛,自己……
<span style="font-size:18px;">#include<cstdio>
#include<stack>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
stack<int>sta;
int size,dir,sum=n;
while(n--)
{
scanf("%d%d",&size,&dir);
if(dir)
sta.push(size);
else
{
while(!sta.empty())
{
if(size>sta.top())
{
sta.pop();
--sum;
}
else
{
--sum;
break;
}
}
}
}
printf("%d\n",sum);
}
return 0;
}</span>