大鱼吃小鱼(运用stack的模拟)

个人心得:这一题在暑假集训的周测里做到过,当时就死模拟,然后卡了很久很久才做对。现在发现运用stack其实非常方便,

将向左向右游动的鱼分开,则往后走只要往右移动的就放入stack,往左的时候就与stack进行对比,以后后面的鱼是与此时左边的鱼

是没有关系的,所以可以很好的解决,

有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
 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<cstdio>
 5 #include<vector>
 6 #include<cmath>
 7 #include<stack>
 8 #include<queue>
 9 #include<algorithm>
10 using namespace std;
11 #define in 1000000005
12 int  n;
13 int u[100005],v[100005];
14 int main()
15 {
16     cin>>n;
17     stack<int > pq;
18      for(int i=0;i<n;i++)
19          cin>>u[i]>>v[i];
20          int sum=n;
21      for(int i=0;i<n;i++)
22      {
23          if(v[i]==1)
24             pq.push(u[i]);
25          else
26             {
27                 if(pq.empty()) continue;
28                 int t=pq.top();
29                 if(t>u[i])
30                     sum--;
31                 else
32                 {
33                     while(t<u[i])
34                     {
35                         sum--;
36                         pq.pop();
37                         if(!pq.empty())
38                         t=pq.top();
39                         else
40                             break;
41
42                     }
43                     if(pq.empty()) continue;
44                     else if(pq.top()>u[i]) sum--;
45
46                 }
47             }
48      }
49      cout<<sum<<endl;
50     return 0;
51 }
				
时间: 2025-02-01 19:39:43

大鱼吃小鱼(运用stack的模拟)的相关文章

Stack结构模拟

我们接着就开始模拟stack数据结构,发觉敲多的头昏,坚持分享 1.基础结构对象Node public class Node { private Object data;// 存放值 private Node next;// 下一个节点 public   Node(){} public Node(Object data) {// 构造值为data的结点 this(data,null); } public Node(Object data, Node next) { this.data = dat

stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram

题目传送门 1 /* 2 题意:宽度为1,高度不等,求最大矩形面积 3 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极限情况 4 st[]里是严格单调递增,若不记录的话还要O(n)的去查找L,R,用栈的话降低复杂度 5 */ 6 #include <cstdio> 7 #include <cstring> 8 #include <algorithm> 9 #include &l

Educational Codeforces Round 35 E. Stack Sorting 模拟

Educational Codeforces Round 35 E. Stack Sorting 题意:长度为 n 的序列 a[] ,a[] 里的数是 1~n,一个空栈 s,一个空序列 b[].两个操作:把 a[] 的第一个数放到 s 里: 或者把 s 的栈顶元素加到 b[] 的末尾. 如果你能通过这两个操作把 a[] 的数最后都放入 b[] 中,且 b[] 是升序的,那就可说 a[] 是 stack-sortable . 现在给出 a[] 的前 k 个数,要你确定是否有满足 stack-sor

Python模拟 堆栈,队列,链表

1. 堆栈 class my_stack(object): def __init__(self, value): self.value = value # 前驱 self.before = None # 后继 self.behind = None def __str__(self): return str(self.value) def top(stack): if isinstance(stack, my_stack): # 判断对象是否合法 if stack.behind is not No

hdu 1348(凸包)

Wall Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4903    Accepted Submission(s): 1419 Problem Description Once upon a time there was a greedy King who ordered his chief Architect to build a

有向图的深度优先遍历算法的快速实现及应用

本文介绍使用java.util.*包中的HashMap 和 LinkedList 以及 ArrayList类快速实现一个有向图,并实现有向图的深度优先遍历算法. 如何构造图? 本文根据字符串数组来构造一个图.图的顶点标识用字符串来表示,如果某个字符串A的第一个字符与另一个字符串B的最后一个字符相同,则它们之间构造一条有向边<A,B>.比如,字符串数组{"hap","peg","pmg","god"}对应的有向图如下

Google interview question: count bounded slices(min/max queue)

Question: A Slice of an array said to be a Bounded slice if Max(SliceArray)-Min(SliceArray)<=K. If Array [3,5,6,7,3] and K=2 provided .. the number of bounded slice is 9, first slice (0,0) in the array Min(0,0)=3 Max(0,0)=3 Max-Min<=K result 0<=2

最大流 Dinic + Sap 模板

不说别的,直接上模板. Dinic+当前弧优化: struct Edge{ int x,y,c,ne; }e[M*2]; int be[N],all; int d[N],q[N]; int stack[N],top;//栈存的是边 int cur[N];//当前弧优化 void add(int x, int y, int z)//需保证相反边第一个为偶数 { e[all].x=x; e[all].y=y; e[all].c=z; e[all].ne=be[x]; be[x]=all++; e[a

C语言 排序算法 - 数据结构学习笔记

/**  功能:     排序   *日期:     2017年9月24日   *作者:     yzh   *开发环境:  QT   **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_LENGTH 100     //非递归快速排序调用栈 #define MAX_LENGTH 20            //数组最大值 //对于数值型 keyType #de