牛客网暑期ACM多校训练营(第二场)money

链接:https://www.nowcoder.com/acm/contest/140/D
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld

题目描述

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

题意:有一个人要逛n个商店,每个商店他可以选择买或者卖掉物品,问可以获得的最大利润和最小的交易次数;

思路:用一个数组是s[i]记录a[i]和a[i -1]的差值,若大于0就加,而次数则看若s[i]<0,才会增加两次;

下面是代码:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#define LL long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
LL a[100005];
LL s[100005];
int main()
{
	int T, n;
	cin >> T;
	while(T--)
	{
		memset(a, 0, sizeof(a));
		memset(s, 0, sizeof(a));
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
		{
			scanf("%lld", &a[i]);
			s[i] = a[i] - a[i - 1];
		}
			
		LL ans = 0, ams = 0, flag = 0, aus = 0, k = 0;
		for(int i = 2; i <= n; i++)
		{
			if(s[i] > 0)
			{
				if(!flag)
					aus += 2;
				flag = 1;
				ans += s[i];
				
			}
			if(s[i] < 0)
				flag = 0;
		}
		printf("%lld %lld\n", ans, aus);
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gtuif/article/details/81481691