Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task:Fish N voracious fish are moving along a river. Calculate how many fish are alive. You are given two non - empty arrays A and
Task:Fish
N voracious fish are moving along a river. Calculate how many fish are alive.
You are given two nonempty arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river.
The fish are numbered from to N If P and Q are two fish and P Q then fish P is initially upstream of fish Q Initially, each fish has a unique position.
Fish number P is represented by AP and BP Array A contains the sizes of the fish. All its elements are unique. Array B contains the directions of the fish. It contains only s andor s where:
represents a fish flowing upstream,
represents a fish flowing downstream.
If two fish move in opposite directions and there are no other living fish between them, they will eventually meet each other. Then only one fish can stay alive the larger fish eats the smaller one. More precisely, we say that two fish P and Q meet each other when P Q BP and BQ and there are no living fish between them. After they meet:
If AP AQ then P eats Q and P will still be flowing downstream,
If AQ AP then Q eats P and Q will still be flowing upstream.
We assume that all the fish are flowing at the same speed. That is fish moving in the same direction never meet. The goal is to calculate the number of fish that will stay alive.
For example, consider arrays A and B such that:
A B
A B
A B
A B
A B
Initially all the fish are alive and all except fish number are moving upstream. Fish number meets fish number and eats it then it meets fish number and eats it too. Finally, it meets fish number and is eaten by it The remaining two fish, number and never meet and therefore stay alive.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started