牛客网暑假训练第二场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)的更多相关文章

  1. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  2. 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 ...

  3. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  4. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  5. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  6. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  7. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

  8. px、dp和sp,这些单位有什么区别?

    DP 这个是最常用但也最难理解的尺寸单位.它与“像素密度”密切相关,所以 首先我们解释一下什么是像素密度.假设有一部手机,屏幕的物理尺寸为1.5英寸x2英寸,屏幕分辨率为240x320,则我们可以计算 ...

  9. android px转换为dip/dp

    /** * 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ public int dipTopx(Context context, float dpValue) { final floa ...

  10. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

随机推荐

  1. k3 cloud成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据

    成本调整单提示期末余额不存在调整单分录的维度,请先出库核算确认是否存在核算维度的数据,如下图所示: 解决办法:先做出库核算,然后做成本调整单,再做出库核算(出库成本核算)

  2. 你创不出伟大的事业,因为……

    你认为自己是对的,而别人都不了解.你忙著实现自己的梦想,却不管你的梦想对这世界有什么意义.你成天想著自己的问题,对别人的问题却提不起任何兴趣. 你活在自己的世界 你认为自己是对的,而别人都不了解.你忙 ...

  3. Vue PC端图片预览插件

    *手上的项目刚刚搞完了,记录一下项目中遇到的问题,留做笔记: 需求: 在项目中,需要展示用户上传的一些图片,我从后台接口拿到图片url后放在页面上展示,因为被图片我设置了宽度限制(150px),所以图 ...

  4. elasticsearch 深入 —— 地理位置

    地理位置 我们拿着纸质地图漫步城市的日子一去不返了.得益于智能手机,我们现在总是可以知道 自己所处的准确位置,也预料到网站会使用这些信息.我想知道从当前位置步行 5 分钟内可到的那些餐馆,对伦敦更大范 ...

  5. R语言子集

    取子集方法 x[]:适用于所有r语言 x[[ ]]:适用于list或者data.frame中提取元素 x$:使用元素名做索引,提取list或者data.frame中的某个元素 注意,取出的子集数据类型 ...

  6. [转载]解决Android studio新建项目慢的问题

    原文地址为:https://blog.csdn.net/easion_zms/article/details/73181402 Android Studio 好处很多,但是当从github上或者导入其 ...

  7. 【Leetcode周赛】从contest-81开始。(一般是10个contest写一篇文章)

    Contest 81 (2018年11月8日,周四,凌晨) 链接:https://leetcode.com/contest/weekly-contest-81 比赛情况记录:结果:3/4, ranki ...

  8. spring整合消息队列rabbitmq

    ps:本文只是简单一个整合介绍,属于抛砖引玉,具体实现还需大家深入研究哈.. 1.首先是生产者配置 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ...

  9. 请求体中需要的true和requests包put请求冲突了

    python  put请求,添加请求头 不知道怎么解决

  10. boost array

    boost::array is similar to std::array, which was added to the standard library with C++11. With boos ...