51nod 1289 Feeding Frenzy

Source: Internet
Author: User
1289 Feeding Frenzy

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
The fish moving to the right moves into the stack, moving to the left and comparing the elements in the stack until the stack is empty or the fish is eaten.
#include <iostream>
#include <stack>
using namespace std;
struct fish
{
	int weight;
	int direction;
} A[100005];
int main ()
{
	int i,n;
	stack<fish>s;
	cin>>n;
	int ans=n;
	for (i=0; i<n; i++)
	{
		cin>>a[i].weight>>a[i].direction;
	}
	for (i=0; i<n; i++)
	{
		if (a[i].direction==1)
		{
			s.push (a[i]);
		}
		else
		{
			while (!s.empty ())
			{
				if (a[i].weight>s.top (). Weight)
				{
					s.pop (); ans--;
				}
				else
				{
					ans--;	Break
   ;
	}}}} cout<<ans<<endl;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.