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 ...
随机推荐
- 基于6U VPX的 SRIO 接口, 和PCIe 接口的msata 固态存储卡
基于6U VPX 的mSATA高性能数据存储板 一.板卡概述 该产品系我司自主研发.基于标准6U VPX架构. 二.产品特性 最大存储容量8TB 读写方式RAID0 ...
- Git--04 Github使用
目录 Github使用 1.注册用户 2.配置ssh-key 3.创建项目 4.克隆项目到本地 5.推送新代码到github Github使用 Github顾名思义是一个Git版本库的托管服务,是目前 ...
- [C++] 所有该类的对象共享静态类成员变量
问:智能指针可以对指针的引用数量进行计数,一个智能指针释放时,别的智能指针怎么知道的? 同一类的对象共享同一变量最简单的方法是静态变量: 不像普通的变量,静态成员变量是被所有类对象共享的,不同的对象可 ...
- 嵌入式系统的性能测试(1) – lmbench篇
要评价一个系统的性能,通常有不同的指标,相应的会有不同的测试方法和测试工具.既有比较成熟的商业测试软件,也有许多优秀的开源工具来完成这个任务.本文简要介绍如何使用lmbench来完成系统综合性能测试. ...
- CF883J 2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest - J. Renovation 贪心+树状数组
首先对于一个月的预算,如果非常小的话,我们可以留到后面的 \(a_i\) 最大的月来用,因为 \(a_i\) 越大能够拆建筑的越多. 于是我们把 \(a_i\) 合并给 \(i\) 后面的 \(a\) ...
- Codefroces 958C2 - Encryption (medium) 区间dp
转自:https://www.cnblogs.com/widsom/p/8857777.html 略有修改 题目大意: n个数,划分为k段,每一段的和mod p,求出每一段的并相加,求最大是多 ...
- boost phoenix
In functional programming, functions are objects and can be processed like objects. With Boost.Phoen ...
- wangeditor 粘贴word内容带样式
这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...
- Oracle or Question Solve(一)
Oracle查看版本命令:select * from v$version; ORACLE_BASE和ORACLE_HOME路径查看su - oracleecho $ORACLE_BASEecho $O ...
- MIS(管理信息系统)
MIS 管理信息系统(Management Information System,简称MIS) 是一个以人为主导,利用计算机硬件.软件.网络通信设备以及其他办公设备,进行信息的收集.传输.加工.储存. ...