FZU 2281 Trades(贪心)

Problem 2281 Trades

Accept: 94    Submit: 511
Time Limit: 1000 mSec    Memory Limit : 262144 KB

Problem Description

This is a very easy problem.

ACMeow loves GTX1920. Now he has m RMB, but no GTX1920s. In the next n days, the unit price of GTX1920 in the ith day is Ci RMB. In other words, in the ith day, he can buy one GTX1920 with Ci RMB, or sell one GTX1920 to gain Ci 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 GTX1920 for selling.

Now he wants to know, how many 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), represents there are T test cases.

For each test case: first line contains two integers n(1 ≤ n ≤2000) and m(0 ≤ m ≤1000000000). Following n integers in one line, the ith integer represents Ci(1 ≤ Ci ≤1000000000).

Output

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

Sample Input

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

Sample Output

Case #1: 3
Case #2: 4

Source

第八届福建省大学生程序设计竞赛-重现赛(感谢承办方厦门理工学院) 、


题意:
题意有n天,有一个物品的价格会每天浮动,每天的价格ai(1<=i<=n),在一开始你有m元RMB,在每一天,你可以以ai的价格买进任意个物品,也可以以ai的价格卖出任意个物品,前提是你有足够的钱和足够的物品,问你在n天后最多能获得多少钱?最后的答案要mod1e9+7

解析:
简单的贪心,价格变化化成一张折线图就是不停的上下浮动,我们每一次总是在价格在低谷的时候买进,价格在低谷对应的高峰的时候卖出
这样整一个序列我们只需要找出每一个递增的连续子序列就可以了

这里比较实用的就是一个序列取出递增和递减连续子序列的首尾

然后坑点就是最后题目要求的取模,他不允许你中间求值的时候模,必须是把最后的答案算出来之后再取模,这样就不得不用大数来做了...
import java.math.*;
import java.util.*;

public class Main {
	
	
	
	public static void main(String[] args){
		BigInteger[] a= new BigInteger[2010];
		BigInteger[] b= new BigInteger[2010];
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		int cas=0;
		for (int k=0;k<t;k++)
		{
			cas++;
			int n;
			n=sc.nextInt();
			BigInteger m=sc.nextBigInteger();
			
			for (int i=1;i<=n;i++)
			{
				a[i]=sc.nextBigInteger();
			}
			int cnt=1;
			for(int i=2;i<=n;i++)
			{
				if(a[cnt].compareTo(a[i])!=0)
				{
					++cnt;
					a[cnt]=a[i];
				}
			}
			a[cnt+1]=BigInteger.ZERO;  //!!去重之后注意
			int cmm=0;
			b[1]=a[1];
			cmm=2;
			for(int i=2;i<=cnt;i++)
			{
				if(a[i].compareTo(a[i-1])>0&&a[i].compareTo(a[i+1])>0)
				{
					b[cmm++]=a[i];
				}
				else if (a[i].compareTo(a[i-1])<0&&a[i].compareTo(a[i+1])<0)
				{
					b[cmm++]=a[i];
				}
			}
			int beg=1;
			if(cmm>2&&b[1].compareTo(b[2])>0) beg=2;
			for(int i=beg;i+1<cmm;i+=2)
			{
				BigInteger tmp=b[i+1].subtract(b[i]);
				m=m.add(m.divide(b[i]).multiply(tmp));
			}
			m=m.mod(BigInteger.valueOf(1000000007));
			System.out.println("Case #"+cas+": "+m);
			
		}
		
	}

}

对应C++版
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
#define  MOD 1000000007

typedef unsigned long long ll;
const int MAXN = 1e5+10;

ll a[MAXN];
ll b[MAXN];


int main()
{
    int t;
    scanf("%d",&t);
    int cas=0;
    while(t--)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        cas++;
        int n;
        ll m;
        scanf("%d%llu",&n,&m);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%llu",&a[i]);
        }
        cnt=1;
        for(int i=2;i<=n;i++)
        {
            if(a[cnt]!=a[i])
            {
                a[++cnt]=a[i];
            }
        }
        a[cnt+1]=0;  //!!!去重之后注意
        int cmm=0;
        b[1]=a[1];
        cmm=2;
        for(int i=2;i<=cnt;i++)
        {
            if(a[i]>a[i-1]&&a[i]>a[i+1])
            {
                b[cmm++]=a[i];
            }
            else if(a[i]<a[i-1]&&a[i]<a[i+1])
            {
                b[cmm++]=a[i];
            }
        }
        int beg=1;
        if(b[2]<b[1]) beg=2;

        for(int i=beg;i+1<cmm;i+=2)
        {
               m=(m+((m/b[i])%MOD*((b[i+1]-b[i]))%MOD)%MOD)%MOD;
        }
        printf("Case #%d: ",cas);
        printf("%llu\n",m%MOD);

    }


    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值