优先队列+贪心

Buy and Resell

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1444    Accepted Submission(s): 477


 

Problem Description

The Power Cube is used as a stash of Exotic Power. There are n cities numbered 1,2,…,n where allowed to trade it. The trading price of the Power Cube in the i-th city is ai dollars per cube. Noswal is a foxy businessman and wants to quietly make a fortune by buying and reselling Power Cubes. To avoid being discovered by the police, Noswal will go to the i-th city and choose exactly one of the following three options on the i-th day:

1. spend ai dollars to buy a Power Cube
2. resell a Power Cube and get ai dollars if he has at least one Power Cube
3. do nothing

Obviously, Noswal can own more than one Power Cubes at the same time. After going to the n cities, he will go back home and stay away from the cops. He wants to know the maximum profit he can earn. In the meanwhile, to lower the risks, he wants to minimize the times of trading (include buy and sell) to get the maximum profit. Noswal is a foxy and successful businessman so you can assume that he has infinity money at the beginning.

Input

There are multiple test cases. The first line of input contains a positive integer T (T≤250), indicating the number of test cases. For each test case:
The first line has an integer n. (1≤n≤105)
The second line has n integers a1,a2,…,an where ai means the trading price (buy or sell) of the Power Cube in the i-th city. (1≤ai≤109)
It is guaranteed that the sum of all n is no more than 5×105.

Output

For each case, print one line with two integers —— the maximum profit and the minimum times of trading to get the maximum profit.

Sample Input

3

4

1 2 10 9

5

9 5 9 10 5

2

2 1

Sample Output

16 4

5 2

0 0

Hint

In the first case, he will buy in 1, 2 and resell in 3, 4. profit = - 1 - 2 + 10 + 9 = 16 In the second case, he will buy in 2 and resell in 4. profit = - 5 + 10 = 5 In the third case, he will do nothing and earn nothing. profit = 0

 

Source

2018中国大学生程序设计竞赛 - 网络选拔赛

 题意:有n天,每天都有一个股票价格ai,你每天可以花ai买下当天的股票,也可以以ai价格卖掉你手中的股票(但是最多只能卖一张或者买一张,且不能又买又卖,可以不买不卖) 求最大收益,且在最大收益情况下最小操作次数

分析:

这道题就是求一个收益最高且最短的合法括号序列,左括号表示当天买入,右括号表示当天卖出

开两个优先队列,其中一个存每一对括号,按照右括号的值排序,顶端最小,另一个优先队列存单独的左括号,仍然按照值排序,顶端最小

这样对于当前第i天的股票ai,有三种方案:

①作为单一的左括号

②最顶端的一对括号中,将右括号替换成当前这个括号,并将原来的右括号单独拉出来作为一个单一的左括号;

③和顶端的单一左括号匹配成为一对括号;

AC code:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
using namespace std;
typedef long long ll;
struct Node{
int x,y;
bool operator<(const Node&a)const
{
    return y>a.y;
}
};
priority_queueq1,q2;
Node now,temp;
int main()
{
    int t,n;
    int s1,s2;
    ll val;
    scanf("%d",&t);
    while(t--){
        while(!q1.empty()) q1.pop();
        while(!q2.empty()) q2.pop();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            s1=s2=0;
            scanf("%lld",&val);
            if(!q1.empty())
              s1=val-q1.top().y;///s1为和左括号匹配后增加的利益
            if(!q2.empty())
              s2=val-q2.top().y;///s2为替换右括号后增加的利益
            if(s1<=0&&s2<=0)
            {
                now.y=val;
                q1.push(now);///当两种方案都不能产生利益时,说明val很小,于是把它作为左括号
            }
            else if(s2>=s1)///显然s2>s1时应替换右括号,但原本的右括号不能舍弃,它可能成为下一个左括号
            {           ///当s2==s1时两种方案产生利益相同,但替换右括号可以产生新的左括号,因此方案二好一些
                now=q2.top();
                q2.pop();    
                temp=now;    
                now.y=val;   
                q2.push(now);
                q1.push(temp);
            }
            else           ///否则直接和左括号匹配
            {
                now=q1.top();
                q1.pop();
                now.x=now.y;
                now.y=val;
                q2.push(now);
            }
        }
        ll ans=0,cnt=0;
        while(!q2.empty())
        {
            ans+=q2.top().y-q2.top().x;
            cnt++;
            q2.pop();
        }
        printf("%lld %lld\n",ans,2*cnt);
    }
    return 0;
}

最后还贴出一个和它比较像的题:

链接: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 
  

输出描述:

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

本题与上面的题目不同在于上面的题拿在手上的可以是多个物品,而这题拿在手上的至多一个物品,所以这题就是一个简单的贪心,

Ac code:

#include
#include
using namespace std;
const int maxn=100001;
typedef long long ll;
int main()
{
    ll dp[maxn];
    int t;
    int n;
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>t;
    while(t--)
    {
    memset(dp,0,sizeof dp);
    cin>>n;
    for(ll i=1;i<=n;i++)
        cin>>dp[i];
    ll cnt=0,cas=0;
    int state=0;
    for(ll i=1;i<=n;i++)
    {
        if(dp[i]dp[i+1]&&state)///卖,并且手中有一个物品
        {
            cnt+=dp[i];
            state=0;
        }
        else continue;
    }
    cout<

 

你可能感兴趣的:(优先队列+贪心)