money (dp)
牛客网暑假训练第二场D题:
链接: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.
题目大意:
你要按照顺序依次经过n个商店,每到达一个商店你可以购买一件商品,也可以出售你手中的商品。
同一时刻你手上最多拿一件商品。在第i个商店购买和出售的代价都是a[i]。
问你经过完n个商店后的最大收益。
同时,在最大化收益的前提下,求最小的交易次数。
题目思路:
做法一:DP
dp[i][0/1]表示已经访问完了i个商店,你手中是否有商品,此时的最大收益。1代表手上有商品,0代表没有
num[i][0/1]表示当dp[i][j]取最大值时最少的交易次数。
做法二:贪心
首先,如果a[i]=a[i+1],则可以删掉第i+1个商店。因为任何在第i+1个商店进行的交易都可以转为在第i个商店进行,且收益不变。之后,如果a[i]<a[i+1],则离开第i个商店时一定要带上一件商品。如果a[i]>a[i+1],则离开第i个商店时一定要空着手。
这样,第一问的答案就为,第二问的答案就为长度>1的极大递增连续段的数量。
做法一:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define MAX 100005
int a[MAX];
pair<ll,int>dp[MAX][];
ll INF=1ll<<; int main()
{
ios::sync_with_stdio();cin.tie();
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
cin>>a[i];
dp[][]=mp(,);
dp[][]=mp(INF,);
for(int i=;i<=n;i++)
{
dp[i][]=min(dp[i-][],mp(dp[i-][].fi-a[i],dp[i-][].se+));
dp[i][]=min(dp[i-][],mp(dp[i-][].fi+a[i],dp[i-][].se+));
}
cout<<-dp[n][].fi<<" "<<dp[n][].se<<endl;
}
return ;
}
思路二:
#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int a[MAX]; int main()
{
ios::sync_with_stdio();cin.tie();
int T;
cin>>T;
while(T--)
{
int n,s=,s1=;
long long sum=;
cin>>n;
for(int i=;i<n;i++)
cin>>a[i];
for(int i=;i<n-;i++)
{
if(a[i]==a[i+])continue;
else if(a[i]<a[i+])
{
sum+=a[i+]-a[i];
s++;
}
else
{
s=;
}
if(s==)s1++;
}
cout<<sum<<" "<<s1*<<endl;
}
return ;
}
参考博客:https://blog.csdn.net/qq_36782366/article/details/81336067
money (dp)的更多相关文章
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
- px、dp和sp,这些单位有什么区别?
DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...
- android px转换为dip/dp
/** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
随机推荐
- Linux学习笔记之磁盘与文件系统的管理
三.Linux磁盘与文件系统的管理 MBR扇区(512B) 磁盘的分区组成 Boot sector 扇区(用来装引导程序) Super block 记录inode与Block的信息 Inod ...
- Mongodb使用shell实现与javascript的动态交互
关于利用mongodb的shell执行脚本,这点在以前的文章中有点遗漏:现在在此篇博客中做个补充: 一.在命令行中传入脚本文件 定义一个javasciprt文件,名称为:script1.js,内容如下 ...
- html5 带声音的导航
代码实例: <!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN""http://www.w3. ...
- Linux 给用户 赋某个文件夹操作的权限
https://my.oschina.net/cqyj/blog/1796047 在root用户登录的情况,赋予opt目录给liuhai这个用户权限 示例代码: # 将目录/opt 及其下面的所有文件 ...
- python基础:7.求结果
求结果: v1 = 1 or 3 v2 = 1 and 3 v3 = 0 and 2 and 1 v4 = 0 and 2 or 1 v5 = 0 and 2 or 1 or 4 v6 = 0 o ...
- Spring----事件(Application Event)
1.概述 1.1.Spring的事件 为Bean与Bean之间的消息通信提供了支持: 当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要 让另一个Bean ...
- 【leetcode】636. Exclusive Time of Functions
题目如下: 解题思路:本题和括号匹配问题有点像,用栈比较适合.一个元素入栈前,如果自己的状态是“start”,则直接入栈:如果是end则判断和栈顶的元素是否id相同并且状态是“start”,如果满足这 ...
- 英语单词Repository
Repository 来源——查看仓库的名称内容 [root@centos7 ~]# yum repolist Loaded plugins: fastestmirror Repository bas ...
- 阿里云基于OSS的云上统一数据保护方案2.0技术解析
近年来,随着越来越多的企业从传统经济向数字经济转型,云已经渐渐成为数据经济IT新常态.核心业务系统上云,云上的业务创新,这些都产生了大量的业务数据,这些数据也成为了企业最重要的资产.资源. 阿里云基于 ...
- setNeedsDisplay和setNeedsLayout方法
参考:https://blog.csdn.net/sunnyboy9/article/details/51458401 . UIView的setNeedsDisplay和setNeedsLayout方 ...