南邮 OJ 1567 Suspicious Stocks

Suspicious Stocks

时间限制(普通/Java) :  1000 MS/ 3000 MS          运行内存限制 : 65536 KByte
总提交 : 26            测试通过 : 17 

比赛描述

The corruption is always extremely difficult to prove. Even if we know that some people accept bribes, it is almost impossible to punish them, because the only witnesses are them and the people who offered the bribe. Neither of them is likely to testify.
One way to prove that something illegal has happened is to count the money a person has and then give evidence that it was impossible for him/her to earn that money legally. However, this is not so easy. For example, the person may easily claim that the money have been donated by their uncle or earned by trading stocks.
ACM would like to be able to verify these explanations. Since it is impossible to verify the existence of “uncles”, we will concentrate on the stocks. You are given the history of stock prices, and your task is to compute the maximal profit that could possibly be earned by buying and selling the stock.



输入

The input will consist of several test scenarios, each of them consisting of two lines of text. The first line of a scenario contains two positive integers and (1 ≤ ≤ 70000, 1 ≤ ≤ 40000) separated by a space. is the number of days when the trading took place, is the amount of money available at the beginning.
The second line of each scenario contains positive integers p1, p2, p3, . . . , p(1 ≤ p≤ 40000) separated by a space. The number pidenotes the price of one piece of stock at day i, meaning it is possible to buy or sell one piece for that price on that day.
The last scenario is followed by a line containing single zero.

输出

For each scenario, output one line containing one number: The maximum profit that could be achieved by at most one Buy operation followed by one Sell operation. Assume that there is no stock at the beginning and that it is possible to do only one Buy during the whole trading.
For simplicity, assume that it is possible to buy as many pieces of stock as there are money for, but we can only buy and sell whole pieces (integer number) of stock, not fractions. For example, if the stock price is $3 and we have $11, we can only buy 3 pieces.
The Buy operation is followed by exactly one Sell operation on some of the following days. Of course, it is possible (and recommended) to sell all the pieces of stock to maximize profit.

样例输入

3 1
1 2 3
3 1 0 0 0
1 2 0 0 4 0 1 0
3 1 0
3 4 5
5 1 0
2 3 7 1 4
0

样例输出

2
0
6
30

提示

undefined

题目来源

CTU Open 2009




#include<iostream>
#define N 70000
int a[N];

int main(){
	int d,m,i,maxPrice,maxProfit,temp;
	while(scanf("%d",&d)==1 && d){
		scanf("%d",&m);
		for(i=0;i<d;i++){
			scanf("%d",a+i);
		}
		maxPrice = a[d-1];
		maxProfit = 0;
		for(i=d-2;i>=0;i--){
			if(a[i]<maxPrice && maxProfit<(temp=m/a[i]*(maxPrice-a[i]))){
				maxProfit = temp;
			}
			if(a[i]>maxPrice){
				maxPrice = a[i];
			}
		}
		printf("%d\n",maxProfit);
	}
}



你可能感兴趣的:(ACM,suspicious,南邮OJ,Stocks)