Stock Trading
Time limit1sMemory limit128 MB
Given known daily prices for n stocks over D days, starting capital C, and at most t buy or sell actions, maximize final cash.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
Stock trading is a great way to make money if you think the high-stakes tables in Las Vegas are not risky enough for you. The idea is simple: buy stock when the price is low, and sell when it is high. The only catch is that you do not know whether the price will go up or down next. But if you did, the problem would be much easier. Here we assume all prices are known in advance and explore how to find an optimal investment strategy in retrospect.
There are stocks in total and days. On each day you are given the price of every stock . You start with a capital of dollars. On each day you may buy or sell arbitrary amounts of any combination of stocks, of course never spending more than your current capital. There is one more rule: you may make at most trades in total. A trade is the act of buying or selling one kind of stock. You do not have to trade in integer amounts; for example you may buy units of a stock if you want. Buying several kinds of stock on the same day uses several trades. Your goal is to maximize the total amount of cash you hold after days. (Only cash counts; any stock you still hold after days is worthless.)
Input
The first line contains the number of data sets, followed by the data sets, each in the following form.
The first line of a data set contains integers , , and a floating point number . Here is the number of stocks, is the number of days, is the number of trades you may make, and is your starting capital.
This is followed by lines, each containing floating point numbers . The value is the price at which stock trades on day . All prices for one day appear on the same line.
Output
For each data set, print a line Data Set x: by itself, where x is the data set number (starting from ). On the next line, print the maximum amount of money you can have after the days, rounded to two decimals, having made at most trades in total. Print a blank line between consecutive data sets.