(第二场)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会议的收获 ...
随机推荐
- MySQL触发器基本使用
文章参考:这里 MySQL中,创建触发器的基本语法: CREATE TRIGGER trigger_name trigger_time trigger_event ON tbl_name FOR EA ...
- Centos时间查看修改命令date详解
1.查看.修改Linux时区与时间 一.linux时区的查看与修改 1,查看当前时区date -R 2,修改设置时区方法1:tzselect 方法2:仅限于RedHat Linux 和 CentOSt ...
- 巧用CheckedTextView完成自定义radiobutton的listview
因为要用自定义图片的radiobutton的listview,最开始想自己重新写BaseAdapter,重新定义BaseAdapter中的每个list的item.总之android提供了太多方便的控件 ...
- (转)The remote certificate is invalid according to the validation procedure
If you get “The remote certificate is invalid according to the validation procedure” exception while ...
- 三:Mybatis知识整理(3)
一:mybatis中模糊查询的方法: 1.直接传参法:在java传参时进行拼接 -- %keyword% 2.mysql内置函数:concart('%',#{keyword},'%') -- 拼接sq ...
- 二、IOC容器基本原理
IOC容器就是具有依赖注入功能的容器,IOC容器负责实例化.定位.配置应用程序中的对象及建立这些对象间的依赖.应用程序无需在代码中new相关的对象,应用程序由IOC容器进行组装. spring IOC ...
- Java序列话和反序列化理解(New)
public interface Serializable {} 该接口没有任何实现方法,是一种标志,instance of Serializable 会判断object类型 一.序列化和反序列化的 ...
- 《JavaWeb从入门到改行》那些年一起学习的Servlet
目录 获取ServletContext : ServletContext接口中的一些方法 application域存取数据功能 代码演示: application域获取项目文件路径 代码演示: API ...
- js-js和HTML的两种结合方式
第一种: - 使用一个标签 <script type="text/javascript"> js代码; </script> 第二种: - 使用script标 ...
- 机智的Popup,带着简单的图表感觉酷酷的
之前有提过用 InfoTemplate 类对 FeatureLayer 的基本信息进行展示,今天为大家介绍 esri/dijit/Popup 的使用,这东西还有 简单的图表展示功能呢! <!DO ...