• 牛客第二场Dmoney


    链接:https://www.nowcoder.com/acm/contest/140/D
    来源:牛客网
    
    题目描述 
    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

    贪心。下一家店变贵了手里没有拿就买。下一家店便宜了,手里有货就卖出。

     1 #include <iostream>
     2 #include <string>
     3 #include <cstring>
     4 #include <iomanip>
     5 #include <cmath>
     6 #include <cstdlib>
     7 #include <cstdio>
     8 #include <algorithm>
     9 #include <map>
    10 #include <queue>
    11 #include <vector>
    12 #include <set>
    13 #include <stack>
    14 using namespace std;
    15 #define ll long long
    16 int a[100005];
    17 int main()
    18 {
    19     int T;
    20     scanf("%d",&T);
    21     while(T--)
    22     {
    23         int m;
    24         scanf("%d",&m);
    25         for(int i=1;i<=m;i++)
    26         {
    27             scanf("%d",&a[i]);
    28         }
    29         ll flag=0;
    30         ll ans=0;
    31         ll  mo=0;
    32         a[m+1]=0;
    33         for(int i=1;i<=m;i++)
    34         {
    35             if(a[i]<a[i+1]&&flag==0)
    36             {
    37                 flag=1;
    38                 ans++;
    39                 mo=mo-a[i];
    40             }
    41             else
    42             {
    43                 if(flag==1&&a[i]>a[i+1])
    44                 {
    45                     ans++;
    46                     flag=0;
    47                     mo=mo+a[i];
    48                 }
    49             }
    50         }
    51         cout<<mo<<" "<<ans<<endl;
    52 
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    Unity HDRP BentNormal的理解
    c语言变长数组VLA的变通实现
    中间件目录索引:redis,git,grpc等
    MYSQL插入脚本
    Polly是一个.NET弹性和瞬态故障处理库
    grpc的.net core使用
    基于PaddleOCR实现AI发票识别的Asp.net Core应用
    Clean Architecture For RazorPage 实现多语言和本地化
    easyui-datagrid 主从表(一对多)表结构,明细在前端存json,一键保存至数据库
    下拉框级联
  • 原文地址:https://www.cnblogs.com/2014slx/p/9370097.html
Copyright © 2020-2023  润新知