zoukankan      html  css  js  c++  java
  • USACO09OPEN Work Scheduling G

    题目链接

    题目解析

    反悔贪心。

    一个比较简单的思路是,以截止时间为第一关键字,利润为第二关键字排序,依次选取。

    但这个显然是错的,因为可以不选某一个利润小的,腾出时间来给一个截止时间在后面但是利润大的。

    于是考虑反悔,按照之前的贪心方式,对于一个工作,当然是能做就做,不能做就尝试能否踢掉前面做过的一个利润没有它高的工作,然后加入当前工作。
    显然是要踢掉已经选了的工作当中利润最低的那一个,于是可以维护一个小根堆。


    ►Code View

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<cstring>
    using namespace std;
    #define N 100005
    #define LL long long
    int rd()
    {
    	int x=0,f=1;char c=getchar();
    	while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
    	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48); c=getchar();}
    	return f*x;
    }
    int n;
    LL ans;
    struct node{
    	int d,p;
    }a[N];
    bool cmp(node x,node y)
    {
    	return x.d<y.d;
    }
    priority_queue<int,vector<int>,greater<int> >Q;
    int main()
    {
    	n=rd();
    	for(int i=1;i<=n;i++)
    		a[i].d=rd(),a[i].p=rd();
    	sort(a+1,a+n+1,cmp);
    	for(int i=1;i<=n;i++)
    	{
    		if(Q.size()<a[i].d)
    		{
    			Q.push(a[i].p);
    			ans+=a[i].p;
    		}
    		else
    		{
    			int tmp=Q.top();
    			if(a[i].p<=tmp) continue;
    			Q.pop();
    			ans-=tmp;
    			Q.push(a[i].p);
    			ans+=a[i].p;
    		}
    	}
    	printf("%lld
    ",ans);
    	return 0;
    }
    /*
    能放进去就放
    不能放进去就找替代
    如果截止时间为Di
    那么在这份工作之前(含这个工作)最多只能做Di份 
    */
    
    
  • 相关阅读:
    python字符串,数组操作
    python爬虫之有道在线翻译
    英雄联盟界面
    学习photoshop心得
    linux命令总结之lsof命令
    linux命令总结之netstat命令
    linux命令总结之route命令
    linux命令总结之ip命令
    linux命令总结之dig命令
    IP地址的分类——a,b,c 类是如何划分的
  • 原文地址:https://www.cnblogs.com/lyttt/p/14087186.html
Copyright © 2011-2022 走看看