Stock Market (Luogu 2938)

Source: Internet
Author: User
Tags stock prices

Title Description

Despite their innate prudence, the cows took a beating in the home mortgage market and now is trying their hand at stocks . Happily, Bessie is prescient and knows not only today's S (2 <= s <=) stock prices but also the future stock pric Es for a total of D days (2 <= D <= 10).

Given The matrix of current and future stock prices on various days (1 <= pr_sd <= $) and an initial M (1 <= M <= 200,000) units of money, determine a optimal buying and selling strategy in order to maximize the gain realized B Y selling stock on the final day. Shares must is purchased in integer multiples, and you need not spend all the money. It is guaranteed so you'll not being able to earn a profit for more than 500,000 units of money.

Consider the example below of a bull (i.e., improving), the kind Bessie likes most. s=2 stocks and d=3 days. The cows has ten units of money to invest.

Today ' s price

| Tomorrow ' s price

| | hence Stock | | | 1 10 15 15

2 13 11 20

If money was to being made, the cows must purchase stock 1 on Day 1. Selling stock 1 on Day 2 and quickly buying stock 2 yields 4 money in the bank and one share of 2. Selling stock 2 on the "Final day" brings in "for a" total of $ when the "the" added to the bank.

There are stocks of s, we already know the price of each stock every day. There is a total of D days, the first to have the money for M, to find out how much money at the end.

input/output format

Input format:

* Line 1:three space-separated integers:s, D, and M

* Lines 2..s+1:line s+1 contains the D prices for stock S on days 1..D:PR_SD

Output format:

* Line 1:the maximum amount of money possible to has after selling on day D.

input/Output sampleInput Sample
Output Sample

Code (open O (2) only)

#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;Const intmx=510000;intn,d,m,f[mx],pri[ the][ -];intMain () {scanf ("%d%d%d",&n,&d,&m);  for(intI=1; i<=n;++i) for(intj=1; j<=d;++j) scanf ("%d",&Pri[i][j]);  for(intk=2; k<=d;++k) {//K days before        intmx=0; Memset (F,0,sizeof(f));  for(intI=1; i<=n;++i) {//Type I stocks             for(intj=pri[i][k-1];J&LT;=M;++J) {//Flower J YuanF[j]=max (f[j],f[j-pri[i][k-1]]+pri[i][k]-pri[i][k-1]); MX=Max (mx,f[j]); }} M+=MX; } printf ("%d", M); return 0;}

Stock Market (Luogu 2938)

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.