"OpenJudge8464" "Serial DP" stock trading

Source: Internet
Author: User

Total stock trading time limit: 1000ms memory limit: 65536kB "description"

Recently more and more people are engaged in the stock market, Ford also a bit of a heartbeat. Remember that "the stock market is risky, the market needs to be cautious," Ford decided to first study the simplified version of the issue of stock trading.

Assuming that Ford has accurately predicted the price of a stock in the next N days, he wants to buy and sell two times, making it the most profitable. To calculate simplicity, the profit is calculated as the price of the sell minus the buy price.

You can trade multiple times on the same day. But after the first purchase, it must be sold before it can be bought for the second time.

Now, Ford wants to know how much profit he can get.

The first line of input input is an integer t (T <= 50), representing a total of T-group data.
Next to each set of data, the first line is an integer n (1 <= n <= 100, 000), representing a total of n days. The second line is an integer of N separated by a space, representing the price of the stock each day. The price of the stock will not exceed 1,000,000 in absolute terms per day. Output for each set of data, output one line. The line contains an integer that represents the maximum profit that can be obtained by a.

"Sample Input"
375 14-2 4 9 3 1766 8 7 4 1-2418 9 5 2
"Sample Output"
2820
Prompted

For the first set of examples, Ford can buy the 1th time on the 1th day (the price is 5) and then sell it on the 2nd day (the price is 14). Buy the 2nd time on the 3rd day (the price is-2) and sell it on the 7th day (the price is 17). The total profit is (14-5) + (17-(-2)) = 28
For the second set of examples, Ford can buy the 1th time on the 1th day (the price is 6) and then sell it on the 2nd day (the price is 8). The 2nd time is still bought on the 2nd day and then sold on the 2nd day. The total profit is 8-6 = 2
For the third group of samples, as the price has been falling, Ford can casually choose to buy a day after the quick sell. The maximum profit is 0

"Solution"

The idea of this problem is very clear, to find out the maximum profit on the left of each point and on the right. The trouble lies in how to ask the right and left sides of the most value, the pros and cons can be swept again.

On the code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 intT,n;6 intdata[100010],max_l[100010],max_r[100010];7 intMain () {8scanf"%d",&T);9      for(intI=1; i<=t;++i) {Ten         intans=0; memset (max_l,0,sizeof(max_l)); memset (Max_r,0,sizeof(Max_r)); Onescanf"%d", &n); for(intj=1; j<=n;++j) scanf ("%d",&data[j]); A         inthead=data[1],tail=Data[n]; -          for(intj=1; j<=n;++j) { -Max_l[j]=max (max_l[j-1],data[j]-head); theHead=min (head,data[j]); -         } -          for(intj=n;j>=1;--j) { -Max_r[j]=max (max_r[j+1],tail-data[j]); +Tail=Max (tail,data[j]); -         } +          for(intj=1; j<=n;++j) AAns=max (ans,max_l[j]+max_r[j+1]); atprintf"%d\n", ans); -     } -     return 0; -}

"OpenJudge8464" "Serial DP" stock trading

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.