zoukankan      html  css  js  c++  java
  • money(牛客网暑期ACM多校训练营第二场-D题)

    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 131072K,其他语言262144K
    64bit IO Format: %lld

    题目描述

    White Cloud has built n stores numbered from 1 to n.
    White Rabbit wants to visit these stores in the order from 1 to n.
    The store numbered i has a price a[i] representing that White Rabbit can spend a[i] dollars to buy a product or sell a product to get a[i] dollars when it is in the i-th store.
    The product is too heavy so that White Rabbit can only take one product at the same time.
    White Rabbit wants to know the maximum profit after visiting all stores.
    Also, White Rabbit wants to know the minimum number of transactions while geting the maximum profit.
    Notice that White Rabbit has infinite money initially.

    输入描述:

    The first line contains an integer T(0<T<=5), denoting the number of test cases.
    In each test case, there is one integer n(0<n<=100000) in the first line,denoting the number of stores.
    For the next line, There are n integers in range [0,2147483648), denoting a[1..n].

    输出描述:

    For each test case, print a single line containing 2 integers, denoting the maximum profit and the minimum number of transactions.

    示例1

    输入:

    1
    5
    9 10 7 6 8

    输出:

    3 4

    题解:

    贪心。每次读进来一个价格,如果小于当前价格就更新当前价格,如果大于则将差值加进结果收益同时更新当前价格并加两次结果交易次数。

    坑点:

    注意如果用到INF一定是2147483647。我因为用0x3f3f3f3f习惯了WR了好几发。

    如果遇到连续的大于当前价格的情况,交易次数只加一次。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    const int INF = 2147483647;
    
    int main(){
    	
    	int T,N;
    	scanf("%d",&T);
    	while(T--){
    		scanf("%d",&N);
    		long long now,last;
    		last = INF;
    		long long sum = 0,num = 0;
    		bool flag = true;
    		for(int i=1 ; i<=N ; ++i){
    			scanf("%lld",&now);
    			if(now > last){
    				sum += now-last;
    				last = now;
    				if(flag)num += 2;
    				flag = false;
    			}
    			else if(now < last){
    				last = now;
    				flag = true;
    			}
    		}
    		printf("%lld %lld
    ",sum,num);
    	}
    	
    	return 0;
    }
  • 相关阅读:
    5R(康奈尔)笔记法
    【软件】MyEclipse 显示行数及Eclipse修改字体
    Linux运维工程师必备的十个技能点
    centos下部署LAMP环境(Linux+Apache+mysql+php)(转载文章:https://www.cnblogs.com/apro-abra/p/4862285.html)
    MySQL之show命令 (转载https://www.cnblogs.com/andy6/p/6171945.html)
    MySQL安装完成后重置密码
    MySQL面试题
    mysql运维必备知识点(转载至其他作者)
    centos 7 下安装部署mysql 5.7
    Linux服务部署之NTP时间服务器
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514044.html
Copyright © 2011-2022 走看看