牛客网暑假训练第二场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][2];
ll INF=1ll<<60;int main()
{ios::sync_with_stdio(0);cin.tie(0);int T;cin>>T;while(T--){int n;cin>>n;for(int i=1;i<=n;i++)cin>>a[i];dp[0][0]=mp(0,0);dp[0][1]=mp(INF,0);for(int i=1;i<=n;i++){dp[i][0]=min(dp[i-1][0],mp(dp[i-1][1].fi-a[i],dp[i-1][1].se+1));dp[i][1]=min(dp[i-1][1],mp(dp[i-1][0].fi+a[i],dp[i-1][0].se+1));}cout<<-dp[n][0].fi<<" "<<dp[n][0].se<<endl;}return 0;
}
思路二:
#include<bits/stdc++.h>
using namespace std;
#define MAX 100005
int a[MAX];int main()
{ios::sync_with_stdio(0);cin.tie(0);int T;cin>>T;while(T--){int n,s=1,s1=0;long long sum=0;cin>>n;for(int i=0;i<n;i++)cin>>a[i];for(int i=0;i<n-1;i++){if(a[i]==a[i+1])continue;else if(a[i]<a[i+1]){sum+=a[i+1]-a[i];s++;}else{s=1;}if(s==2)s1++;}cout<<sum<<" "<<s1*2<<endl;}return 0;
}
参考博客:https://blog.csdn.net/qq_36782366/article/details/81336067