2017-11-20 16:25

# Electric wave

Problem Description
Ali was doing a physic experiment which requires him to observe an electric wave. He needs the height of each peak value and valley value for further study (a peak value means the value is strictly larger than its neighbors and a valley value means the value is strictly smaller than its neighbors). He did write these numbers down but he was too careless that he wrote them in a line without separations, such as “712495” may represent “7 12 4 9 5”. The only information he can remember was:
1. The data begins with a valley value
2. Each value is either a peak value or a valley value
Now he wants to insert blanks to make the data valid. If multiple solutions exist, he will choose the one with more blanks.

Input
The input consists several testcases.
The first line contains one integer N (1 <= N <= 100), the length of the data.
The second line contains one string S, the data he recorded.
S contains only digits.

Output
Print one integer, the maximum number of blanks he can insert.

Sample Input
6
712495

Sample Output
4

• 点赞
• 写回答
• 关注问题
• 收藏
• 邀请回答

#### 2条回答默认 最新

• blownewbee 2018-09-15 11:49
已采纳
• weixin_42444190 2018-07-05 13:19

If all peak values or valley values are available, using MATLAB can solve your problem. For example, MATLAB reads all ASCII code numbers of what you have written and some functions such as sort() from MATLAB can obtain the minimum one of these code if blanks are kicked out. You can also find more functions from matlab to get what you want. Hope this is helpful to you. Thank you.

点赞 打赏 评论