1289 Feeding Frenzy Title Source: codility Base time limit: 1 seconds space limit: 131072 KB Score: 5 Difficulty: 1 level algorithm topic collection concerned about n fish each fish position and size are different, they swim along the x-axis, some 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
Problem Solution: Direct write is troublesome, the situation is not good to master.
Stack to do, right into the stack, to the left and the stack within the size of the judge to eat fish or eaten.
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
# Include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR (A, B) memset (A,b,sizeof (a))
#define INF 0x3f3f3f3f
#define LL Long Long
int main ()
{
int n;
scanf ("%d", &n);
Stack <LL> s;
LL a[100100];
int num=0;
for (int i=1;i<=n;i++)
{
int t;
scanf ("%lld%d", &a[i],&t);
if (T==0&&s.empty ())
num++;
if (t==1)
{
s.push (a[i]);
num++;
}
else
{
while (!s.empty ())
{
LL tt=s.top ();
S.pop ();
num--;
if (Tt<a[i]&&!s.empty ())
continue;
else if (Tt<a[i]&&s.empty ())
num++;
else
{
s.push (TT);
num++;
Break
;
}}}} printf ("%d\n", num);
return 0;
}