题目: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
5
9 10 7 6 8

输出:

3 4

大概题意:

有标号为1~N的N间商铺,小白兔每到一个商铺可以选择以a[i]的价格购入或卖出,也可以选择不买不卖,小白兔只能携带一件商品,求最大收益和最小交易次数。

思路:

一、DP

状态:

dp[i][0] 走了 i 间商铺之后,手上没有商品的最大收益

dp[i][1] 走了 i 间商铺之后,手上有商品的最大收益

g[i][0] 走了 i 间商铺之后,手上没有商品的最大收益的最少交易次数

g[i][1] 走了 i 间商铺之后,手上有商品的最大收益的最少交易次数

状态转移方程:

dp[i][0] = max(dp[i-1][0], dp[i-1][1] + a[i]);

dp[i][1] = max(dp[i-1][0] - a[i], dp[i-1][1]);

g随着dp的更新而更新。

dp数组:

 
  1 2 3 4 5
0 0 1 1 1 3
1 -9 -9 -6 -5 -5

g数组:

 
  1 2 3 4 5
0 0 2 2 2 4
1 1 1 3 3 3

!!!注意数据范围,dp和g都要定义为long long,第一次dp不够大卡在了60%,第二次g不够大卡在了80%!!!

AC code:

  #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e5+; int N, T;
long long int a[MAXN];
long long int dp[MAXN][];
long long int g[MAXN][]; void init()
{
memset(dp, , sizeof(dp));
memset(g, , sizeof(g));
} int main()
{
scanf("%d", &T);
while(T--)
{
init();
scanf("%d", &N);
for(int i = ; i <= N; i++)
{
scanf("%lld", &a[i]);
}
///dp
dp[][] = ;
dp[][] = -a[];
g[][] = ;
g[][] = ; for(int i = ; i <= N; i++)
{
if(dp[i-][] >= (dp[i-][]+a[i]))
{
dp[i][] = dp[i-][];
g[i][] = g[i-][];
}
else
{
dp[i][] = dp[i-][] + a[i];
g[i][] = (g[i-][]+);
} if((dp[i-][]-a[i]) > dp[i-][])
{
dp[i][] = dp[i-][] - a[i];
g[i][] = (g[i-][]+);
//printf("%d %d %d %d\n", i, dp[i][1], g[i][1], g[i-1][0]);
}
else
{
dp[i][] = dp[i-][];
g[i][] = g[i-][];
}
} ///debug
/*
for(int i = 1; i <= N; i++)
printf("%d ", g[i][0]);
puts("");
for(int i = 1; i <= N; i++)
printf("%d ", g[i][1]);
puts("");
*/ if(dp[N][] > dp[N][])
printf("%lld %lld\n", dp[N][], g[N][]);
else if(dp[N][] < dp[N][])
printf("%lld %lld\n", dp[N][], g[N][]);
else
{
if(g[N][] > g[N][]) printf("%lld %lld\n", dp[N][], g[N][]);
else printf("%lld %lld\n", dp[N][], g[N][]);
}
} return ; }
												

(第二场)D Money 【dp\贪心】的更多相关文章

  1. run (牛客多校第二场)计数DP

    链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 题目描述 White Cloud is exercising in the playground ...

  2. hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】

    题目链接 可以贪心写,先把b数组按从小到大的顺序排个序,根据b[i]的值来产生a[n+i] 借助一个c数组,c[i]记录,j从i到n,a[j]-j的最大值,再加上一个实时更新的变量ma,记录从n+1到 ...

  3. 2014百度之星预赛(第二场)——Best Financing

    2014百度之星预赛(第二场)--Best Financing Problem Description 小A想通过合理投资银行理財产品达到收益最大化.已知小A在未来一段时间中的收入情况,描写叙述为两个 ...

  4. Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)

    Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...

  5. 2018牛客暑期ACM多校训练营第二场(有坑未填)

    第二场终于等来学弟 开始(被队友带飞)的开心(被虐)多校之旅 A   run A题是一个递推(dp?)+前缀和 因为看数据量比较大 就直接上前缀和了 一个比较简单的递推 没有太多难点 签到题 需要注意 ...

  6. 2018牛客网暑假ACM多校训练赛(第二场)E tree 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round2-E.html 题目传送门 - 2018牛客多校赛第二场 E ...

  7. [比赛|考试]nowcoder NOIPpj组第二场

    nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...

  8. 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?

    牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...

  9. 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告

    目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...

  10. 本周进步要点20161023(含李笑来第二场live笔记要点)

    本周主要忙于去武汉参加iDOF2016智能数字油田会议,会上做了题为“油田SOA及云平台的系统思考与实践”的报告,为了准备这篇报告,用到了一些以前学过的知识,具体内容见“参加iDOF2016会议的收获 ...

随机推荐

  1. centos6.x硬件信息统计脚本

    #!/bin/bash Line='===========' #linux发行版名称 if [[ -f /usr/bin/lsb_release ]]; then OS=$(/usr/bin/lsb_ ...

  2. Eclipse使用快捷键总结

    1.为方法添加注释:Alt + Shift + J

  3. Effective C++ .17 函数调用时的资源管理

    以书上的代码为例 processWidget(shared_ptr<Widget>(new Widget), priority()) 虽然使用了智能指针来管理资源但是,由于参数值计算顺序的 ...

  4. [TJOI2007]小朋友

    题面 Luogu Sol 弦图最大独立集 做法见上篇博客 # include <bits/stdc++.h> # define RG register # define IL inline ...

  5. PowerDesigner设置所有int主键自增脚本

    '*****************************************************************************dim model 'current mod ...

  6. C++类继承--基类new和用派生类new的区别

    实际上无论是用基类还是派生类New, 结果是一样的: #include <stdio.h> class Base { public: int a; Base(){ a=0; } virtu ...

  7. UNIX/Linux系统管理技术手册(3)----bash 数组和算术运算

    复杂的数据结构和计算不是 bash 的特长.但它的确至少提供了数组和算术运算. 1.算术运算 所有的 bash 变量的值都是字符串,所以 bash 在赋值的时候并不区分数字 1 和 字符串 " ...

  8. CentOS 7运维管理笔记(5)----源代码安装Apache 2.4,搭建LAMP服务器

    ##########################    2016-07-07-Thu--20:34 补充 ##################### 编译安装OpenSSL笔记: 如果系统要使用 ...

  9. hiho 1015 KMP算法 && CF 625 B. War of the Corporations

    #1015 : KMP算法 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在 ...

  10. 05_ActiveMQ的selectors

    [ JMS Selectors ] JMS Selectors用于在订阅中,基于消息属性对消息进行过滤. 以下是个Selectors的例子:Java代码 consumer = session.crea ...