(第二场)D Money 【dp\贪心】
题目: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\贪心】的更多相关文章
- run (牛客多校第二场)计数DP
链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 题目描述 White Cloud is exercising in the playground ...
- hdu 6047: Maximum Sequence (2017 多校第二场 1003)【贪心】
题目链接 可以贪心写,先把b数组按从小到大的顺序排个序,根据b[i]的值来产生a[n+i] 借助一个c数组,c[i]记录,j从i到n,a[j]-j的最大值,再加上一个实时更新的变量ma,记录从n+1到 ...
- 2014百度之星预赛(第二场)——Best Financing
2014百度之星预赛(第二场)--Best Financing Problem Description 小A想通过合理投资银行理財产品达到收益最大化.已知小A在未来一段时间中的收入情况,描写叙述为两个 ...
- Contest1592 - 2018-2019赛季多校联合新生训练赛第二场(部分题解)
Contest1592 - 2018-2019赛季多校联合新生训练赛第二场 D 10248 修建高楼(模拟优化) H 10252 组装玩具(贪心+二分) D 传送门 题干 题目描述 C 市有一条东西走 ...
- 2018牛客暑期ACM多校训练营第二场(有坑未填)
第二场终于等来学弟 开始(被队友带飞)的开心(被虐)多校之旅 A run A题是一个递推(dp?)+前缀和 因为看数据量比较大 就直接上前缀和了 一个比较简单的递推 没有太多难点 签到题 需要注意 ...
- 2018牛客网暑假ACM多校训练赛(第二场)E tree 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round2-E.html 题目传送门 - 2018牛客多校赛第二场 E ...
- [比赛|考试]nowcoder NOIPpj组第二场
nowcoder NOIPpj组第二场 370pts/400pts(100,100,100,70) rank3 给自己的反思:前3题都A了,T4O(N^2)不会就是不会(没准是我懒得推了),DP了70 ...
- 牛客网 暑期ACM多校训练营(第二场)A.run-动态规划 or 递推?
牛客网暑期ACM多校训练营(第二场) 水博客. A.run 题意就是一个人一秒可以走1步或者跑K步,不能连续跑2秒,他从0开始移动,移动到[L,R]的某一点就可以结束.问一共有多少种移动的方式. 个人 ...
- 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告
目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...
- 本周进步要点20161023(含李笑来第二场live笔记要点)
本周主要忙于去武汉参加iDOF2016智能数字油田会议,会上做了题为“油田SOA及云平台的系统思考与实践”的报告,为了准备这篇报告,用到了一些以前学过的知识,具体内容见“参加iDOF2016会议的收获 ...
随机推荐
- awk如何替换一个字符串的第n个字符?
方法一: echo "abcdefg" | awk 'BEGIN{FS=OFS=""}$4="h"' // ""可 ...
- [转]Using MVC 6 And AngularJS 2 With .NET Core
本文转自:http://www.c-sharpcorner.com/article/using-mvc-6-and-angularjs-2-with-net-core/ CoreMVCAngular2 ...
- sublime快捷键大全(转)
文件 File 新建文件 Ctrl + N 打开文件 Ctrl + O 打开最近关闭的文件 Ctrl + Shift + T 保存 Ctrl + S 另存为… Ctrl + Shift + S 关闭文 ...
- redis(4)事务
一.事务 一般来说,事务必须满足4个条件,也就是我们常说的ACID: 1)Atomicity 原子性:一个事务中的所有操作要么全部完成,要么全部不完成,不会结束在中间的某个环节.事务在执行过程中发生错 ...
- 提示"No 'Access-Control-Allow-Origin' header"及Spring 中解决跨域问题
问题描述 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://12 ...
- Java 8 读取文件
以前的Java版本中读取文件非常繁琐,现在比较简单.使用Java8的Files以及Lambda,几句代码就可以搞定. public static String getXml() { StringBuf ...
- php explode时间分割
<?php $str = "2017-02-27 13:40:42"; $first=explode(' ',$str); $second=explode('-', $fir ...
- 实现移动端touch事件的横向滑动列表效果
要实现手机端横向滑动效果并不难,了解实现的原理及业务逻辑就很容易实现.原理:touchstart(手指按下瞬间获取相对于页面的位置)——>touchmove(手指移动多少,元素相应移动多少). ...
- Java从入门到精通——数据库篇Mongo DB 导出,导入,备份
一.概述 本篇博客为大家讲述一下Mongo DB是如何导入导出数据,还有就是备份数据的. 在下面操作的时候需要把Mongo DB的服务端打开才能操作. 二.导出. MongoDB的导 ...
- Excellent JD
Job description About the role We are looking for a talented engineer who has excellent cloud skills ...