1289 大鱼吃小鱼
- 1 秒
- 131,072 KB
- 5 分
- 1 级题
有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右。游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼。从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右)。问足够长的时间之后,能剩下多少条鱼?
收起
输入
第1行:1个数N,表示鱼的数量(1 <= N <= 100000)。 第2 - N + 1行:每行两个数A[i], B[i],中间用空格分隔,分别表示鱼的大小及游动的方向(1 <= A[i] <= 10^9,B[i] = 0 或 1,0表示向左,1表示向右)。
输出
输出1个数,表示最终剩下的鱼的数量。
输入样例
5 4 0 3 1 2 0 1 0 5 0
输出样例
2
题解: 用栈存放 向右游动的鱼,当遇到向左游动的鱼时,判断栈顶元素与当前鱼的大小,小则 出栈,鱼数减一,大则鱼数减一,跳出。
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<bitset>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#define eps (1e-8)
#define MAX 0x3f3f3f3f
#define u_max 1844674407370955161
#define l_max 9223372036854775807
#define i_max 2147483647
#define re register
#define pushup() tree[rt]=tree[rt<<1]+tree[rt<<1|1]
#define nth(k,n) nth_element(a,a+k,a+n); // 将 第K大的放在k位
#define ko() for(int i=2;i<=n;i++) s=(s+k)%i // 约瑟夫
using namespace std;
inline int read(){
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' & c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
typedef long long ll;
const double pi = atan(1.)*4.;
const int M=1e3+5;
const int N=1e6+5;
stack<int>ss;
int main(){
int n,a,b;
scanf("%d",&n);
int ans=n;
for(int i=0;i<n;i++){
scanf("%d %d",&a,&b);
if(b==1)
ss.push(a);
else {
while(!ss.empty()){
int g=ss.top();
if(g<a){
ans--;
ss.pop();
}
else{
ans--;
break;
}
}
}
}
printf("%d\n",ans);
return 0;
}