2018牛客暑期ACM多校训练营第二场 - D题

D. money

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:

输入
 

1
5
9 10 7 6 8

输出
 

3 4

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
ll dp[100010][0];
ll map1[100010];
int n;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		scanf("%lld",&map1[1]);
		ll temp0=0,temp1=-map1[1],cont0=0,cont1=1;
		dp[1][0]=0;
		dp[1][1]=-map1[1];
		for(int i=2;i<=n;++i)
		{
			scanf("%lld",&map1[i]);
			if(temp0<temp1+map1[i])
				dp[i][0]=temp1+map1[i],cont0=cont1+1;
			else
				dp[i][0]=temp0;

			if(temp1<temp0-map1[i])
				dp[i][1]=temp0-map1[i],cont1=cont0+1;
			else
				dp[i][1]=temp1;

			temp0=max(dp[i][0],temp0);
			temp1=max(dp[i][1],temp1);
		}
		if(temp0>temp1)
			printf("%lld %lld\n",temp0,cont0);
		else if(temp0<temp1)
			printf("%lld %lld\n",temp1,cont1);
		else
			printf("%lld %lld\n",temp1,min(cont0,cont1));
	}
}

 

全部评论

相关推荐

09-05 12:26
仰恩大学 营销
秋招第二次就面到字节了52分钟,非常非常压力面,面试官不断打断我、否定我,认为我做的东西太简单,觉得我没学到东西感觉太压抑了后面都有点扛不住了
ALEX_BLX:压力面我一般直接反过来怼面试官你有本事压力你ld去,压力一个应届生算什么东西?服从性测试罢了,你敢压力我我就敢怼你,然后直接投诉hr
一起聊字节
点赞 评论 收藏
分享
07-24 03:49
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务