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 ...
随机推荐
- fputc, fputs, putc, putchar, puts - 输出字符和字符串
总览 (SYNOPSIS) #include <stdio.h> int fputc(int c, FILE *stream); int fputs(const char *s, FILE ...
- ltp-ddt smp_cpu_affinity
# @name SMP CPU Affinity# @desc Check that processes assigned to multiple CPUs complete without erro ...
- MySQL中常见函数
一.日期函数 1.NOW() 返回当前日期和时间 mysql> SELECT NOW(); +---------------------+ | NOW() | +-------------- ...
- JSP相关学习
动态页面技术(JSP/EL/JSTL) <!-- jsp的三种脚本方式 --> <% int i = 5; //这是单行注释 /*这是多行注释*/ %> <%=i%> ...
- freertos优秀博客收藏
https://blog.csdn.net/zhzht19861011 朱工的专栏 专注/深入/分享 https://blog.csdn.net/xukai871105 xukai871105 专注于 ...
- IO流 读写文件
读写文件 如前所述,一个流被定义为一个数据序列.输入流用于从源读取数据,输出流用于向目标写数据. 下图是一个描述输入流和输出流的类层次图. 下面将要讨论的两个重要的流是 FileInputStream ...
- Spring框架之接口实现覆盖(接口功能扩展)
在日常开发中,存在着这种一种场景,框架对接口A提供了一个种默认的实现AImpl,随着需求的变更,现今AImpl不能满足了功能需要,这时,我们该怎么办? 当然是修改AImpl的实现代码了,但是,如果它是 ...
- springmvc把对象放到session中
1:首先把创建的对象放到Map中, @RequestMapping("/testSession") public String testSession(Map<Stri ...
- SDUT 1266 出栈序列统计(卡特兰数)
这道题是回溯算法,网上一查是卡特兰数先占上代码,题解过两天会写. #include <bits/stdc++.h> using namespace std; int main() { // ...
- express中 使用session与cookie
1.express如何使用session与cookie : https://www.jianshu.com/p/1839e482274e 或 https://www.cnblogs.com/chy ...