首页 > 试题广场 >

Calculate survival fishes

[编程题]Calculate survival fishes
  • 热度指数:6 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
Given two zero-indexed arrays A and B consisting of N (N >= 0) integers, which represent N fishes in a river, ordered from west to east.
The fish are numbered from 0 to N-1, and Fish number X is represented by A[X] and B[X]. Array A contains the sizes of the fishes, while array B contains the directions they swim, which can be 1 (from west to east) or -1 (from east to west).

If two fishes move in opposite directions meet each other, the larger fish will eat the smaller one, and the survival will still follow its original direction.

We assume that all fishes are in different sizes and swimming at the same speed. The goal is to calculate the number of fishes that will stay alive.

For example:

A is [3, 5, 9, 1, 2]
B is [1, -1, 1, 1, -1]

Fish number 1 will eat Fish number 0, Fish number 4 will eat Fish number 3 and be eaten by Fish number 2. Therefore the output should be 2 (Fish number 1 and Fish number 2 are still alive)

输入描述:
Array A and B as described above


输出描述:
The number of fishes that will stay alive
示例1

输入

3 5 9 1 2
1 -1 1 1 -1

输出

2

这道题你会答吗?花几分钟告诉大家答案吧!