51Nod - 1289 Big fish eat small fish (stack)

51Nod - 1289 Big fish eat small fish
. There are N fish. Each fish is different in position and size. They swim along the X axis, some to the left, some to the right. The swimming speed is the same, two fish meet the big fish will eat the small fish. The size and swimming direction of each fish are given from left to right (0 means left, 1 means right). After enough time, how many fish can be left?


Input Line 1: 1 number N, representing the number of fish (1 <= N <= 100000).
Lines 2 - N + 1: two numbers A i per line , B i , separated by spaces, indicating the size of the fish and the direction of swimming respectively (1 <= A i <= 10^9,B i = 0 or 1, 0 means left, 1 means right).
Output outputs 1 number, indicating the number of fish left in the end. Sample Input
5
4 0
3 1
2 0
1 0
5 0
Sample Output
2

analyze:

When I do it myself, I use a vector, and I also separate the left and right ones first, and then traverse them one by one.

This question is very convenient to use stack , very simple!

The stack is last in first out,

You can put the fish to the right into the stack, and the one that is eaten first, or the one that is judged first before the next fish to the left, is the fish that is stored in the stack later, which is the top of the stack. .

In this way, you can input and operate at the same time,

If the fish to the left is particularly large, ! ! Then keep q.pop() ~~~ until   q is empty . At this point, cnt ++ (cnt is used to record the fish that can swim to the left)

If the fish to the left encounters a relatively large q.top(), then there is a way, continue to the next set of input! ! !

Finally q.size () + cnt is the final remaining fish


pay attention! ! :

 while( !q.empty() && a > q.top()) This sentence determines the size of the fish to the left and the fish on the top of the stack. cannot be written as while(a > q.top()&& !q.empty())

Because q.top () is empty when the execution is wrong! ! ! And if you want to judge! q.empty () , because of the short-circuit principle, the subsequent judgment will not be performed.


#include <stack>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

intmain()
{
    int n;
    int cnt = 0;
    stack<int> q;
    cin>>n;
    while(n--)
    {
        int a,b;

        cin>>a>>b;
        if(b) // b == 1 means the fish swims to the right
        {
            q.push(a);
        }

        else // if the fish swims to the left
        {
            while(!q.empty() && a > q.top()) // If there is a fish to the right in front of the fish to the left and it is bigger than it, if the fish to the left is smaller and eaten, go to the next group entered
                q.pop(); // The fish in front to the right is eaten.
            if(q.empty()) // If q is empty, it means that the left is eaten by the right,
                cnt ++; // cnt records the fish that can finally swim to the left
        }
    }
    cout<<q.size() + cnt<<endl;
    return 0;
}

 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326483254&siteId=291194637