Step1 Problem:
给出天数n,初始财产m
给出每天商品单价(每天商品供给 和 需求的数量是无限的),你可以在这天买商品,或者卖商品。
求你能赚取的最大财产
Step2 Involving algorithms:
贪心
Step3 Ideas:
低谷买,山峰卖是最优的。
Step4 Code:
import java.util.*;
import java.math.*;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
int T;
T = cin.nextInt();
for(int Case = 1; Case <= T; Case++)
{
System.out.printf("Case #%d: ", Case);
int n;
BigInteger M, val, last, num;
n = cin.nextInt();
M = cin.nextBigInteger();
last = cin.nextBigInteger();
num = BigInteger.valueOf(0);
int flag = 0;
for(int i = 2; i <= n; i++)
{
val = cin.nextBigInteger();
if(val.compareTo(last) == 1 && flag == 0) {
num = M.divide(last);
M = M.remainder(last);
flag = 1;
}
if(val.compareTo(last) == -1 && flag == 1)
{
M = M.add(num.multiply(last));
num = BigInteger.valueOf(0);
flag = 0;
}
last = val;
}
if(flag == 1) {
M = M.add(num.multiply(last));
}
System.out.println(M.remainder(BigInteger.valueOf(1000000007)));
}
}
}