「FZU2281」Trades【第一次写java】

Trades

This is a very easy problem.

ACMeow loves G T X 1920. GTX1920. GTX1920. Now he has m m m R M B RMB RMB, but no G T X 1920 s GTX1920s GTX1920s. In the next n n n days, the unit price of G T X 1920 GTX1920 GTX1920 in the i − t h i-th ith day is C i C_i Ci R M B . RMB. RMB. In other words, in the i − t h i-th ith day, he can buy one G T X 1920 GTX1920 GTX1920 with C i C_i Ci R M B RMB RMB, or sell one G T X 1920 GTX1920 GTX1920 to gain C i C_i Ci R M B . RMB. RMB. He can buy or sell as many times as he wants in one day, but make sure that he has enough money for buying or enough G T X 1920 GTX1920 GTX1920 for selling.

Now he wants to know, how many R M B RMB RMB can he get after the n days. Could you please help him?

It’s really easy, yeah?

Input

First line contains an integer T ( 1 ≤ T ≤ 20 ) T(1 \leq T \leq 20) T(1T20), represents there are T T T test cases.

For each test case: first line contains two integers n ( 1 ≤ n ≤ 2000 ) n(1 \leq n \leq 2000) n(1n2000) and m ( 0 ≤ m ≤ 1000000000 ) . m(0 \leq m \leq 1000000000). m(0m1000000000). Following n n n integers in one line, the i − t h i-th ith integer represents C i ( 1 ≤ C i ≤ 1000000000 ) . C_i(1 \leq C_i \leq1000000000). Ci(1Ci1000000000).

Output

For each test case, output “Case #X: Y” in a line (without quotes), where X X X is the case number starting from 1 1 1, and Y Y Y is the maximum number of R M B RMB RMB he can get mod 1000000007. 1000000007. 1000000007.

Sample Input

2
3 1
1 2 3
4 1
1 2 1 2

Sample Output

Case #1: 3
Case #2: 4

题意

  • 就是有 n n n天,你开始有 m m m元钱,每天的显卡都有一个价格,你可以选择把手中的一些显卡卖掉或者买进一些显卡(只要钱够),问最后你手上最多有多少钱

题解

  • 昨天才开始学的一节课的 j a v a java java,今天就遇到了还是很开心 Q W Q QWQ QWQ
  • 贪心:在一段连续的单调区间内,低谷买进显卡,高峰卖掉显卡,只不过可能会爆 l o n g   l o n g long\ long long long,比如 1   1 0 9   1   1 0 9   1.... 1\ 10^9\ 1\ 10^9\ 1.... 1 109 1 109 1....这种情况,所以直接可以用 j a v a java java来写

代码

import java.util.*;
import java.math.*;

public class Main {
	public static void main(String args[]){
		int[] a=new int[2005];
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		for(int cas=1;cas<=t;cas++) {
			int n=in.nextInt(),m=in.nextInt();
			for(int i=1;i<=n;i++) a[i]=in.nextInt();
			
			int l=1,r=1;
			boolean inc=true;
			while(r<n) {
				if(inc) {
					int pos=r;
					while(r<n&&a[r+1]>=a[r]) r++;
					if(a[pos]!=a[r]) a[++l]=a[r];
					inc=false;
				}else{
					int pos=r;
					while(r<n&&a[r+1]<=a[r]) r++;
					if(a[pos]!=a[r]) a[++l]=a[r];
					inc=true;
				}
			}
			n=l;
			BigInteger money=BigInteger.valueOf(m),have=BigInteger.valueOf(0);
			System.out.print("Case #"+cas+": ");
			if(n==1) {
				System.out.println(m);
				continue;
			}
			if(a[2]>a[1]) {
				have=money.divide(BigInteger.valueOf(a[1]));
				money=money.mod(BigInteger.valueOf(a[1]));
			}
			for(int i=2;i<=n;i++) {
				BigInteger c=BigInteger.valueOf(a[i]);
				if(a[i]>a[i-1]) {
					
					money=money.add(c.multiply(have));
					have=BigInteger.valueOf(0);
				}else {
					have=have.add(money.divide(c));
					money=money.mod(c);
				}
			}
			BigInteger c=BigInteger.valueOf(a[n]);
			money=money.add(c.multiply(have));
			System.out.println(money.mod(BigInteger.valueOf(1000000007)));
		}
	}
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值