HDU6438 Buy and Resell

HDU6438 Buy and Resell

比较经典的贪心问题了

爬山 or 堆 ?

#include <bits/stdc++.h>

using namespace std;
const int maxn = 100005;
typedef long long ll;
struct goods
{
    int f,cost;
    goods(){};
    goods(int _f,int _cost)
    {
        f = _f;cost = _cost;
    }
    bool operator < (const goods & _goods)const
    {
        if(cost == _goods.cost){
            return f < _goods.f;
        }
        return cost < _goods.cost;
    }
}a[maxn];
int t,n;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i].cost);
        }
        priority_queue<goods>q;
        while(!q.empty())q.pop();
        ll ans = 0,sum = 0;
        q.push(goods(0,a[n].cost));
        for(int i=n-1;i>=1;i--)
        {
            if(a[i].cost < q.top().cost){
                if(q.top().f == 0)
                {
                    sum += 2;
                    ans += (q.top().cost-a[i].cost);
                    q.pop();
                    q.push(goods(1,a[i].cost));
                }
                else{
                    ans += (q.top().cost-a[i].cost);
                    int ta = q.top().cost;
                    q.pop();
                    q.push(goods(1,a[i].cost));
                    q.push(goods(0,ta));
                }
            }
            else
            {
                q.push(goods(0,a[i].cost));
            }
           //cout<<"i: "<<ans<<" "<<sum<<endl;
        }
        cout<<ans<<" "<<sum<<endl;
    }

    return 0;
}

原文地址:https://www.cnblogs.com/solvit/p/9536813.html

时间: 2024-08-02 18:13:16

HDU6438 Buy and Resell的相关文章

2018中国大学生程序设计竞赛 - 网络选拔赛 hdu6438 Buy and Resell 买入卖出问题 贪心

Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1126    Accepted Submission(s): 359 Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbe

HDU 6438 Buy and Resell ( 2018 CCPC 网络赛 &amp;&amp; 贪心 )

题目链接 题意 : 给出一些数.你可以从左到右对这些数进行三种操作花费 Ai 买入东西.以 Ai 价格卖出你当前有的东西.或者什么都不做.现在问你可以获取的最大利益是多少? 分析 : 和 CF 867E 一模一样 传送门 可以去搜这题的题解.有很多 对于每个元素产生的贡献 可以先算出暂时的最优值 如果下次碰到更优的选择再进行替换 具体就是首先使用小顶堆维护枚举过的元素 然后对于当前枚举到的元素 用它和堆顶元素做对比.如果小于或等于堆顶元素 那么它无法和之前枚举过的所有元素的任何一个做减法产生贡献

HDU 6438 Buy and Resell

高卖低买,可以交易多次 维护一个优先队列,贪心 相当于每天卖出 用当前元素减优先队列最小得到收益 用0/卖出,1/买入标志是否真实进行了交易,记录次数 #include<bits/stdc++.h> using namespace std; typedef long long ll; ll A[500005]; #define P pair<ll,ll> priority_queue<P,vector<P>,greater<P> > q; int

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

传送门 A.HDU6438 Buy and Resell 题意 给你N天N个价格,每天都可以从1.买入一个,2.卖出一个,3.什么都不做,求最高获利 低买高卖问题,这题与其他的差距就是要在满足获利最多的情况下,买卖次数最小; 思路 手算一下发现价格具有传递性;例如数据是1,5,9; 5的时候买入1赚了4;9的时候买入5赚了4;相当于直接把5当成跳板直接9的时候买1;然后再把中间的5当成没有买过的; 建立一个小根堆(优先队列);把当前遇到的天数的价钱放入,然后对于第i天; 1.如果新天数的价钱比堆

我他妈怎么这么菜

昨天千里万里跑回学校打那个网络赛,然并卵,没有拿到现场赛资格.今天花了一下午一晚上时间学习树状数组,lowbit函数取x&(-x)即可得x的二进制从右往左第一个1的位置,add函数用来修改一个值以及所有包含这个元素节点的值,quiry查询是为了获得a[1]+a[2]+??????+a[x]的值,那么这个题的quiry就是找的以前的最大值加上当前的值.YJJ's SalesmanTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/6

18CCPC网赛A 贪心

Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2023    Accepted Submission(s): 738 Problem Description The Power Cube is used as a stash of Exotic Power. There are n cities numbe

2018 CCPC网络赛

2018 CCPC网络赛 Buy and Resell 题目描述:有一种物品,在\(n\)个地点的价格为\(a_i\),现在一次经过这\(n\)个地点,在每个地点可以买一个这样的物品,也可以卖出一个物品,问最终赚的钱的最大值. solution 用两个堆来维护,一个堆维护已经找到卖家的,一个堆维护还没找到卖家的. 对于第\(i\)个地点,在已经找到卖家的堆里找出卖的钱的最小值,如果最小值小于\(a_i\),则将卖家换成\(i\),然后将原来的卖家放到没找到卖家的那里:如果最小值对于\(a_i\)

121.买卖股票 Best Time to Buy and Sell Stock

Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit. Exam

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit