码迷,mamicode.com
首页 > 其他好文 > 详细

HDU6438 Buy and Resell

时间:2018-08-26 12:31:22      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:scanf   pen   ace   open   ++   queue   i++   def   php   

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;
}
View Code

 

HDU6438 Buy and Resell

标签:scanf   pen   ace   open   ++   queue   i++   def   php   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!