2018中国大学生程序设计竞赛 - 网络选拔赛 题面 1001 Buy and Resell


1001 Buy and Resell
Problem
The Power Cube is used as a stash of Exotic Power. There are cities numbered
where allowed to trade it. The trading price of the Power Cube in the -th
city is dollars per cube. Noswal is a foxy businessman and wants to quietly make a
fortune by buying and reselling Power Cubes. To avoid being discovered by the
police, Noswal will go to the -th city and choose exactly one of the following three
options on the -th day:
1. spend dollars to buy a Power Cube
2. resell a Power Cube and get dollars if he has at least one Power Cube
3. do nothing
Obviously, Noswal can own more than one Power Cubes at the same time. After
going to the cities, he will go back home and stay away from the cops. He wants to
know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants
to minimize the times of trading (include buy and sell) to get the maximum profit.
Noswal is a foxy and successful businessman so you can assume that he has infinity
money at the beginning.
Input
There are multiple test cases. The first line of input contains a positive integer (
), indicating the number of test cases. For each test case:
The first line has an integer . ( )
The second line has integers where means the trading price (buy
or sell) of the Power Cube in the -th city. ( )
It is guaranteed that the sum of all is no more than .

先水一道,我可能只会水题(噗~)

大概是说一个商人,他有n天可以交易,每天物品(就看成物品吧)的价格会改变,每天可以
1.买入,2卖出去 ,3.什么都不做
求n天以后这个人的得到的最大利益(买入和卖出的差价)和最小交易次数(怕麻烦所以少交易,每买入或卖出算都算一次交易)

代码块

代码是Java的,也就几个函数

//@requires_authorization
import java.util.Scanner;

public class BuyAndResell {
    static int getmaxi(int a[],int n) {
        int i,ret=0;int s=0;
        int len=n;
        for(i=0;i<len;i++) {
            if(a[i]>ret) {
                ret=a[i];
                s=i;
            }
        }
        return s;
    }
    static boolean stop(int a[],int n) {
        int i;
        for(i=0;i<n;i++) {
            if(a[i]!=0)break;
        }
        if(i!=n)
            return false; 
        else
            return true;
    }
    static int lastmin(int a[],int max) {
        int min1=999999,i,s=-1;
        for(i=0;i<max;i++) {
            if(min1>a[i]&&a[i]!=0) {
                min1=a[i];
                s=i;
            }
        }
        return s;
    }
    public static void main(String[] args) {
        int arr[]=new int[100005];
        //int sign[]=new int [100005];
        int t;int n,i,num;int ans=0,sum=0;
        Scanner sc = new Scanner(System.in);
        t=sc.nextInt();
        while((t--)!=0) {
            ans=0;sum=0;
            n=sc.nextInt();
            for(i=0;i<n;i++) {
                arr[i]=sc.nextInt();
            }
            while(!stop(arr,n)) {
                int maxn =getmaxi(arr,n);
                int maxn2=lastmin(arr,maxn);
                if(maxn2==-1) {
                    arr[maxn]=0;
                }else {
                    ans+=arr[maxn]-arr[maxn2];
                    arr[maxn]=0;
                    arr[maxn2]=0;
                    sum+=2;
                }
            }
            System.out.println(ans+" "+sum);
        }
    }

}
/*
>>> message = '''interpreter
... prompt'''*/

输入t,代表t次试验
先输入n,代表n天,然后n个数,代表每天的价格
思路大概就是先找到所有天的最大值,然后在最大值前面找到一个比这个最大值小的最小值,
如果找到了,就代表两次交易(最小值那天买入和最大值那天卖出),这两天赋0
如果没找到,就代表在这个最大值的时候不会交易,直接赋0
当所有天全为0的时候,(最大值为0的时候),结束,输出就可以了
“`


猜你喜欢

转载自blog.csdn.net/qq_43065776/article/details/82079561