51nod-【1289 大鱼吃小鱼】

来源:互联网 发布:马兰币数据 编辑:程序博客网 时间:2024/05/21 07:08
1289 大鱼吃小鱼
题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
有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示例
54 03 12 01 05 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();elsebreak;}elsebreak; if(g.dir==0){sta.push(arr[i]);break;}}if(sta.empty())sta.push(arr[i]); }elsesta.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>


0 0
原创粉丝点击