hdu 6438 Buy and Resell

Buy and Resell

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


 

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

有n个商店,对于在每个商店的地方,他有三种选择:

1.花a_i元,买一个物品

2.卖掉一个物品,会得到a_i元

3.什么都不做

他会按顺序经过这每个商店

问你最后最多可以得到多少利润,并且输出最小的操作次数,并且假设他一开始有无限的金钱

2018年暑假网络赛的题目,当时补了,没写博客,这次总结贪心也写下来吧

 

首先我们可以遇到一个东西就放进堆里,然后每次放得时候判断一下堆头,如果大于堆我就会直接放进去,然后删除以前在堆里的并且加上价值,但是比如 1 2 9 10

首先是 2 - 1,然后我们会发现其实 9 - 1 更优,所以我们还要再把当前得2放进去,要放两个状态,一个当做买的,一个当做卖的,如果存在更大的,可以直接用后面大的减去最小的,可以知道买卖的次数是相等的,所以操作最后直接乘*2就完了

 

 

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
//int fa[maxn];
//int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
//ll a[maxn];
multiset<P>s;
int main() {
//    ios::sync_with_stdio(false);
    int t;
    scanf("%d",&t);
    while(t--){ cin >> n;
        long long ans = 0,cnt = 0;s.clear();
        for(int i = 1; i <= n;i++){
            int x;
            scanf("%d",&x);
            if(!s.empty() && (*s.begin()).first < x){
                ans += x - (*s.begin()).first;
                if((*s.begin()).second) cnt++;
                s.erase(s.begin());
                s.insert(P(x,1)),s.insert(P(x,0));
            }else s.insert(P(x,1));
        }
        cout << ans << " " << cnt * 2 << endl;
    }
//    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值