题目: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.

案例:

输入:

1
5
9 10 7 6 8

输出:

3 4

大概题意:

有标号为1~N的N间商铺,小白兔每到一个商铺可以选择以a[i]的价格购入或卖出,也可以选择不买不卖,小白兔只能携带一件商品,求最大收益和最小交易次数。

思路:

一、DP

状态:

dp[i][0] 走了 i 间商铺之后,手上没有商品的最大收益

dp[i][1] 走了 i 间商铺之后,手上有商品的最大收益

g[i][0] 走了 i 间商铺之后,手上没有商品的最大收益的最少交易次数

g[i][1] 走了 i 间商铺之后,手上有商品的最大收益的最少交易次数

状态转移方程:

dp[i][0] = max(dp[i-1][0], dp[i-1][1] + a[i]);

dp[i][1] = max(dp[i-1][0] - a[i], dp[i-1][1]);

g随着dp的更新而更新。

dp数组:

 
  1 2 3 4 5
0 0 1 1 1 3
1 -9 -9 -6 -5 -5

g数组:

 
  1 2 3 4 5
0 0 2 2 2 4
1 1 1 3 3 3

!!!注意数据范围,dp和g都要定义为long long,第一次dp不够大卡在了60%,第二次g不够大卡在了80%!!!

AC code:

  #include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std; const int MAXN = 1e5+; int N, T;
long long int a[MAXN];
long long int dp[MAXN][];
long long int g[MAXN][]; void init()
{
memset(dp, , sizeof(dp));
memset(g, , sizeof(g));
} int main()
{
scanf("%d", &T);
while(T--)
{
init();
scanf("%d", &N);
for(int i = ; i <= N; i++)
{
scanf("%lld", &a[i]);
}
///dp
dp[][] = ;
dp[][] = -a[];
g[][] = ;
g[][] = ; for(int i = ; i <= N; i++)
{
if(dp[i-][] >= (dp[i-][]+a[i]))
{
dp[i][] = dp[i-][];
g[i][] = g[i-][];
}
else
{
dp[i][] = dp[i-][] + a[i];
g[i][] = (g[i-][]+);
} if((dp[i-][]-a[i]) > dp[i-][])
{
dp[i][] = dp[i-][] - a[i];
g[i][] = (g[i-][]+);
//printf("%d %d %d %d\n", i, dp[i][1], g[i][1], g[i-1][0]);
}
else
{
dp[i][] = dp[i-][];
g[i][] = g[i-][];
}
} ///debug
/*
for(int i = 1; i <= N; i++)
printf("%d ", g[i][0]);
puts("");
for(int i = 1; i <= N; i++)
printf("%d ", g[i][1]);
puts("");
*/ if(dp[N][] > dp[N][])
printf("%lld %lld\n", dp[N][], g[N][]);
else if(dp[N][] < dp[N][])
printf("%lld %lld\n", dp[N][], g[N][]);
else
{
if(g[N][] > g[N][]) printf("%lld %lld\n", dp[N][], g[N][]);
else printf("%lld %lld\n", dp[N][], g[N][]);
}
} return ; }
												

最新文章

  1. Android Studio 插件整理
  2. 田渊栋:AlphaGo系统即使在单机上也有职业水平
  3. QCopChannel的用法
  4. webshell提权20种思路
  5. LitDB文章
  6. [转载]关于CSDN, cnblog, iteye和51cto四个博客网站的比较与分析
  7. echarts标准饼图(二)——标题(title)配置
  8. MVC4数据访问EF查询linq语句的时候报错找不到表名问题
  9. Python 模块的一般处理
  10. Oracle EBS-SQL (SYS-13):查询DBA在系统中的打Patch的信息.SQL
  11. android raw与assets资源
  12. Spring装配bean--01组件扫描和自动装配
  13. JavaScript浏览器解析原理
  14. [转]SDN与OpenFlow技术简介
  15. BZOJ4613 : [Wf2016]Longest Rivers
  16. 解决Android studio生成H文件时报找不到类文件错误
  17. 【消息】linux之消息队列
  18. C++STL3--queue
  19. bzoj4247挂饰——压缩的动态规划
  20. web前端js 实现打印操作

热门文章

  1. docker创建nginx镜像
  2. PHP代码语法验证插件:SublimeLinter
  3. Jmeter进行性能测试时多台负载机的配置方法
  4. Make sure that the controller has a parameterless public constructor.
  5. Java生成验证码(二)
  6. new date()标准时间转yyyy-mm-dd hh:mm 24小时制
  7. js为什么放到head中有时候失效
  8. css3打包后自动追加前缀插件:autoprefixer
  9. Object.preventExtensions()使用技巧
  10. 远程连接Redis服务器