Topic Links:
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289
There are n fish each fish's position and size are different, they swim along the x-axis, some to the left, some to the right. Swimming speed is the same, two fish meet big fish will eat small fish. The size of each fish and the direction of the swim are given from left to right (0 means left and 1 for right). Ask how many fish you have left after a long enough time. Input
Line 1th: 1 number N, indicating the number of fish (1 <= n <= 100000).
2-n + 1 lines: Two numbers per line a[i], B[i], separated by a space, respectively, the size of the fish and the direction of the swimming (1 <= a[i] <= 10^9,b[i] = 0 or 1,0 means left, 1 to the right).
Output
Output 1 numbers, indicating the number of fish that are eventually left.
Input example
5
4 0
3 1
2 0
1 0
5 0
Output example
2
Analysis: (with a stack to maintain)
For article I fish, if the direction is toward the right into the stack. To the left, compare each element J to the stack, and if I>j is so J dead, continue to access the elements in front of J (know the stack is empty or dead),,, otherwise I die, continue to traverse i+1 and the elements behind
Code
<span style= "FONT-SIZE:24PX;" > #include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "algorithm"
#include " iostream "
#include" math.h "
#include" ctype.h "
#include" stack "
using namespace std;
int main ()
{
stack<int> s;
int n;
scanf ("%d", &n);
int ans=n;
while (n--)
{
int x, y;
scanf ("%d%d", &x,&y);
if (y==1) s.push (x);
else
{
while (!s.empty ())
{
if (X>s.top ()) {s.pop (); ans--;}
else {ans--; Break
;
}}}} printf ("%d\n", ans);
return 0;
} </span>