zoukankan      html  css  js  c++  java
  • 牛客多校2 D-money(dp记录/贪心)

    D-money

    链接:https://www.nowcoder.com/acm/contest/140/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



    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #define MAX 100005
    #define INF 0x3f3f3f3f
    #define MOD 1000000007
    using namespace std;
    typedef long long ll;
    
    int a[MAX],dpg[MAX][2];
    ll dpf[MAX][2];
    
    int main()
    {
        int t,n,i,j;
        scanf("%d",&t); 
        while(t--){
            scanf("%d",&n);
            for(i=1;i<=n;i++){
                scanf("%d",&a[i]);
            }
            memset(dpf,0,sizeof(dpf));
            memset(dpg,0,sizeof(dpg));
            dpf[1][0]=0;
            dpf[1][1]=-a[1];
            dpg[1][0]=0;
            dpg[1][1]=1;
            for(i=2;i<=n;i++){
                if(dpf[i-1][0]>=dpf[i-1][1]+a[i]){
                    dpf[i][0]=dpf[i-1][0];
                    dpg[i][0]=dpg[i-1][0];
                }
                else{
                    dpf[i][0]=dpf[i-1][1]+a[i];
                    dpg[i][0]=dpg[i-1][1]+1;
                }
                if(dpf[i-1][0]-a[i]>dpf[i-1][1]){
                    dpf[i][1]=dpf[i-1][0]-a[i];
                    dpg[i][1]=dpg[i-1][0]+1;
                }
                else{
                    dpf[i][1]=dpf[i-1][1];
                    dpg[i][1]=dpg[i-1][1];
                }
            }
            printf("%lld %d
    ",dpf[n][0],dpg[n][0]);
        }
        return 0;
    }
  • 相关阅读:
    Angular Universal教学-将现有专案导入Server Side Render
    [.NET] 使用ValidationContext快速进行模型资料的验证
    FINS/TCP_OMRON(1)
    C#中字段、属性、只读、构造函数赋值、反射赋值的相关
    async异步方法
    C# GetHashCode、Equals函数和键值对集合的关系
    JS三个编码函数和net编码System.Web.HttpUtility.UrlEncode比较
    ES6摘抄
    js基础
    js自执行函数、调用递归函数、圆括号运算符、函数声明的提升
  • 原文地址:https://www.cnblogs.com/yzm10/p/9362700.html
Copyright © 2011-2022 走看看