题目链接:FZU - 2281
Java大数学习:Java中的BigInteger类和BigDecimal类
题意:
- 有一台机器,在每一天都会有对应的价格。
- 每一天你都可以选择买或者卖任意台机器(如果你有足够的钱和足够的机器)或者不进行操作。
- 假设你在刚开始的时候有m块钱,但是没有机器。
- 求最后一天之后最多能有多少钱(mod 1000000007)。
思路:
- 这个序列可以被分为多个递增序列。
- 对于每个序列,在序列开始(也就是低谷)的时候买尽量多的机器,然后在序列的最后(也就是顶峰),把这些机器卖掉。
- 简单来说就是,在低价的时候可劲买,买到没钱,在高价的时候可劲卖,买到没。
参考学长的博客【贪心 && 大数】FZU - 2281 Trades
qwq
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t;
t = in.nextInt();
for (int ca = 1; ca <= t; ca++) {
int n;
BigInteger m, num, mode;
BigInteger a[] = new BigInteger[2010];
mode = BigInteger.valueOf(1000000007);
num = BigInteger.ZERO;
n = in.nextInt();
m = in.nextBigInteger();
for (int i = 1; i <= n; i++) {
a[i] = in.nextBigInteger();
}
for (int i = 2; i <= n; i++) {
if (a[i].compareTo(a[i - 1]) == 1) {
num = num.add(m.divide(a[i - 1]));
m = m.remainder(a[i - 1]);
}
if (a[i].compareTo(a[i - 1]) == -1) {
m = m.add(a[i - 1].multiply(num));
num = BigInteger.ZERO;
}
}
if (num.compareTo(BigInteger.ZERO) == 1) {
m = m.add(a[n].multiply(num));
}
m = m.remainder(mode);
System.out.println("Case #" + ca + ": " + m);
}
in.close();
}
}