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 ...
随机推荐
- 关于正则表达式RegExp
常用元字符串 元字符 说明 \d 匹配 数字 \D 匹配 非数字 \w 匹配 数字,字母,下划线 \W 匹配 任意不是字母,数字,下划线 \s 匹配 空白符 \S 匹配 任意不 ...
- smbspool - 将一个打印文件发送到一台SMB打印机
总览 SYNOPSIS smbspool {job} {user} {title} {copies} {options} [filename] 描述 DESCRIPTION 此程序是Samba(7)套 ...
- Centos7 安装 clamav
环境 CentOS: 7.x 下载 下载地址 :http://www.clamav.net/downloads,使用目前最新版本为:clamav-0.101.3 使用 wget 下载 wget htt ...
- HTML5 绘制阴影
代码: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8 ...
- P4707 重返现世 扩展 MinMax 容斥+DP
题目传送门 https://www.luogu.org/problem/P4707 题解 很容易想到这是一个 MinMax 容斥的题目. 设每一个物品被收集的时间为 \(t_i\),那么集齐 \(k\ ...
- webRTC脱坑笔记(四)— windows下Nginx对Node服务的反向代理
Nginx反向代理 1.什么是反向代理 当我们有一个服务器集群,并且服务器集群中的每台服务器的内容一样的时候,同样我们要直接从个人电脑访问到服务器集群服务器的时候无法访问,必须通过第三方服务器才能访问 ...
- Sublime Text 快捷键汇总
1. 常用快捷键 Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行 ...
- pytorch数据预处理错误
出错: Traceback (most recent call last): File , in <module> train_model(model_conv, criterion, o ...
- Linux 统计文件夹下文件个数及目录个数
1. 统计文件夹下文件的个数 ls -l | grep "^-" | wc -l 2.统计文件夹下目录的个数 ls -l | grep "^d" | wc -l ...
- 拦截Restful API的三种方式
如题, 方式有三种. (1). 过滤器filter javaEE规范 (2). 拦截器interceptor springmvc提供 (3). 切片 aspect 一. Filter使用示例 impo ...