zoukankan      html  css  js  c++  java
  • 牛客模拟买卖东西

    链接:https://ac.nowcoder.com/acm/challenge/terminal

            money
    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

    题目解析:可以来模拟买卖东西
    #pragma GCC optimize(2)
    #include<bits/stdc++.h>
    using namespace std;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    typedef unsigned long long ll;
    const int maxn = 1e6+10;
    ll a[maxn];
    int main()
    {
        ll t;
        cin>>t;
        while(t--){
            ll n;
            cin>>n;
            for(int i=1;i<=n;i++){
                cin>>a[i];
            }
            a[n+1]=0;
            ll i,p=0,cnt=0,f=0;//p来模拟钱数//cnt来模拟交换次数//f来模拟交换 
            for(i=1;i<=n;i++){
                if(a[i]<a[i+1]&&f==0){
                    p-=a[i];
                    cnt++;
                    f=1; 
                }
                else if(a[i]>a[i+1]&&f==1){
                    p+=a[i];
                    cnt++;
                    f=0;
                }
            }
            printf("%lld %lld
    ",p,cnt);
        }
        return 0;
    }
    View Code
    
    
    



  • 相关阅读:
    mysql主从只同步部分库或表
    springboot~aspect通过@annotation进行拦截
    ELK~fluentd将日志文件增量读到es里
    怎么自学 Java ?和盘托出
    一个后端开发的 Vue 笔记【入门级】
    企业级数据大屏设计如何实现
    Vue.js官方中文教程
    vue学习笔记 ---- 系列文章
    基于Asp.net core Kestrel的超迷你http服务器 ---- AServer
    Docker学习—概念及基本应用
  • 原文地址:https://www.cnblogs.com/lipu123/p/12144621.html
Copyright © 2011-2022 走看看