Solution to a problem P5662 [souvenir] Luo Valley, 2019CSP-JT3, heavy title P2938 [USACO09FEB] Stock Market Stock Market

Description meaning of the questions:

Given a ⼀ D D -day S S stock price matrix, and the initial capital ⾦ M M ; every time you buy a stock only to buy the stock price of an integer multiple of, can not spend money, not to exceed the agreed profit 500000 500000 . It is largest of your total profit.

Topic analysis:

First of all we need to know in detail the intent of this question: can be used every day in your hands some money to buy shares, an unlimited number, you can also sell your own stock, the proceeds or value already in D S D*S is given in the matrix. Ask for at the end of the last day of the most money.

answer:

In fact, we can find: For each day as long as you can to maximize your earnings reach the goal. The question then is converted for the sake of equity transactions per day up. They know every stock you can buy an unlimited amount (of course, and no more than the value of their money), it is clear that a full backpack.

In fact, the title does not exceed the profit 500000 500000 has prompted the use of DP et secret algorithm, as given not by a i n t int to l o n g long l o n g long changes the data range of the memory array must be prepared.

Therefore especially after the end of the first day of the rest of the money as much as possible it is certainly the best.
Because the continuous holding stocks equivalent to every day after buying, second shot day
to sell and buy. So you can do ⼀ times backpack full day.
time complexity O ( 700000 D S ) O(700000*D*S)

Then you will not go into details, other parts will be indicated in the code, look at the code:

Code:

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int a[61][21],f[500005];
int main()
{
	int s,d,m;
	scanf("%d%d%d",&s,&d,&m);
	for(int i=1;i<=s;i++)
	{
		for(int j=1;j<=d;j++)
		{
			scanf("%d",&a[i][j]);
		}
	}//以上不需要解释
	for(int k=2;k<=d;k++)
	{
		memset(f,0,sizeof(f));
		int maxx=0;
		for(int i=1;i<=s;i++)
		{
			for(int j=a[i][k-1];j<=m;j++)//每次循环到前一天当前位置的股票交易价格。
			{
				f[j]=fmax(f[j],f[j-a[i][k-1]]+a[i][k]-a[i][k-1]);//第一种情况是不买,第二种就是买:要价格减去买入所花的钱再加上今天和昨天的价格差,因为如果不卖出相当于卖出再买入
				maxx=fmax(f[j],maxx);//取出一天股票的最大值
			}
		}
		m+=maxx;//累加收益
	}
	printf("%d",m);
	return 0;
}

These are the original title of the P2938 solution

This question was observed but only the input data sequence and fine-tuning range, and did not change substantially, so

#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int a[105][105],f[10005];
int main()
{
	int s,d,m;
	scanf("%d%d%d",&d,&s,&m);
	for(int i=1;i<=d;i++)
	{
		for(int j=1;j<=s;j++)
		{
			scanf("%d",&a[j][i]);
		}
	}//以上不需要解释
	for(int k=2;k<=d;k++)
	{
		memset(f,0,sizeof(f));
		int maxx=0;
		for(int i=1;i<=s;i++)
		{
			for(int j=a[i][k-1];j<=m;j++)//每次循环到前一天当前位置的股票交易价格。
			{
				f[j]=fmax(f[j],f[j-a[i][k-1]]+a[i][k]-a[i][k-1]);//第一种情况是不买,第二种就是买:要价格减去买入所花的钱再加上今天和昨天的价格差,因为如果不卖出相当于卖出再买入
				maxx=fmax(f[j],maxx);//取出一天股票的最大值
			}
		}
		m+=maxx;//累加收益
	}
	printf("%d",m);
	return 0;
}

It can range, to be noted here that open into the topic array f does not exceed the guaranteed range, rather than read the m

Published 376 original articles · won 157 Like · views 70000 +

Guess you like

Origin blog.csdn.net/kkkksc03/article/details/103210336