Problem J: Buying and Selling
Mr. Skywind has received a request from his friend Lesley, who is a stock analyst Lesley is focusing on the buying and selling quantity now. She will found a mathematics model on these values. As you know, every day, a lot of people se stocks, and a lot of people buy. The quantities of stocks which are concluded are equal certainly. But the quantity that people want to sell and the value that people want to buy is not same. If buying quantity is more than selling, the difference indicated by a positive number is call net buying. Opposite, if the buying is small, the number is expressed using a negative number. So it can be called net-buying-selling value.
Although the difference is recoded every day, what Lesley focus is not only the daily value but also the sum of continuous days. When a series of history stock data is given, Lesley wants to know that in which days the sum of net-buying-selling is maximum and positive or the sum is minimal and negative. Although Lesley can do some easy programming, it is hard to find an effective algorithm for a lot of data. Now it is the time that you write this program to help her.
Input
There are several test cases in input file. The first line of each case is an integer n(2<=n<=100,000) followed by n numbers which are less than 10,000. The end of input is indicated by a zero.
Output
For each case, two numbers are needed. The first is the maximum and positive sum of net-buying-selling in some continuous days; the second is the minimal and negative. You can assure there is one positive and one negative value at least.
Sample Input
6
1 2 3 -3 -2 -5
0
Sample Output
6 -10
这道题是一道简单求最大子段和,最小子段和的题目,这是DP的典型题,就不做过多说明了
本文介绍了一种通过编程实现的股票买卖差额分析方法,旨在找出连续若干天内的最大及最小净买入卖出值。该算法利用动态规划原理,能够高效处理大量数据。

671

被折叠的 条评论
为什么被折叠?



