POJ1456 贪心

1 题意:有n个商品,给出它卖出的价值以及必须在第几天之前卖出去(deadline),在某一天只能卖出一种商品。请问最佳的销售计划。

2 分析:

容易想错题意,其样例可能是具有迷惑性的,即你想错了题意其样例也是可能过的。

3

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

const int maxn=10010;
typedef long long ll;
int n;
int order[maxn];
ll ans;
struct Node{
    int value;
    int date;
}node[maxn];
priority_queue <int> que_temp;
bool cmp(struct Node n1,struct Node n2){
    if(n1.date>n2.date){
        return 1;//大日期在前
    }
    else if(n1.date==n2.date){
        return n1.value<n2.value;//小价值在前
    }
    else
        return false;
}
void Fun(){
    int temp=node[1].date;
    int cur=1;
    node[n+1].date=-1;
    for(int i=temp;i>=1;i--){
        int bj=0;
        for(int j=cur;j<=n;j++){
            if(node[j].date>=i&&node[j].date!=node[j+1].date){
                if(!que_temp.empty()&&node[j].value<que_temp.top()){
                    //cout<<"a"<<endl;
                    ans+=que_temp.top();
                    que_temp.pop();
                    que_temp.push(node[j].value);
                    cur=j+1;
                    bj=1;
                    break;
                }
                else{
                    //cout<<"b"<<endl;
                    ans+=node[j].value;
                    cur=j+1;
                    bj=1;
                    break;
                }
            }
            else if(node[j].date>=i&&node[j].date==node[j+1].date){
                //cout<<"c"<<endl;
                que_temp.push(node[j].value);
                cur=j+1;
            }
            else{
                //cout<<"d"<<endl;
                break;
            }
        }
        if(bj==0&&!que_temp.empty()){
           //cout<<"e"<<endl;
            ans+=que_temp.top();
            que_temp.pop();
        }
        bj=0;
    }
}
int main()
{
    //freopen("out.txt","w",stdout);
    while(~scanf("%d",&n)){
        ans=0;
        while(!que_temp.empty()){
            que_temp.pop();
        }
        for(int i=1;i<=n;i++){
            scanf("%d%d",&node[i].value,&node[i].date);
        }
        //cout<<""<<endl;
        sort(node+1,node+1+n,cmp);
        //for(int i=1;i<=n;i++){
           // cout<<node[i].value<<" "<<node[i].date<<endl;
        //}
        Fun();
        cout<<ans<<endl;
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值