zoukankan      html  css  js  c++  java
  • USACO1572[Usaco2009 Open]工作安排Job

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1572

    题目大意:

    约翰接到了N 份工作,每份工作恰好占用他一天的时间。约翰从第一天开始工作,他可以任意安排这些工作的顺序,第i 份工作有Pi 的报酬,但必须在第Di 天结束之前完成。在截止日期后完成的工作没有报酬。请帮助约翰规划每天的工作,使得他赚到的钱最多。

    题解:

    贪心

    先按截止日期排个序。

    对于每份工作都先“来者不拒”,但如果接受工作的份数大于天数,就是说无法在截止日期前把每份接受的工作都完成了,那么这个时候就要舍弃一些。贪心的思想嘛~就舍弃那些工资低的。用优先队列来维护就好了。

    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<algorithm>
    #include<iostream>
    using namespace std;
    typedef long long LL;
    #define maxn 101000
    
    struct node
    {
    	LL d,x;
    	friend bool operator < (node x,node y)
    	{
    		if (x.d!=y.d) return x.d<y.d;
    		return x.x<y.x;
    	}
    }a[maxn];
    priority_queue<LL,vector<LL>,greater<LL> > q;//按从小到大排
    int main()
    {
    	//freopen("job.in","r",stdin);
    	//freopen("job.out","w",stdout);
    	LL n,i,tot=0,ans=0;
    	scanf("%lld",&n);
    	for (i=1;i<=n;i++)
    	 scanf("%lld%lld",&a[i].d,&a[i].x);
    	sort(a+1,a+1+n);
    	for (i=1;i<=n;i++)
    	{
    		ans+=a[i].x;tot++;q.push(a[i].x);
    		if (tot>a[i].d) {ans-=q.top();q.pop();tot--;}//把工资最低的减掉
    	}printf("%lld
    ",ans);
    	return 0;
    }


  • 相关阅读:
    数据库存储语句
    数据库练习总结
    数据库练习
    数据库增添
    数据库创建
    cookie 和 session的区别 & 三种传值方式
    内置对象——cookie
    webform跨页面传值
    复合控件
    repeater(控件)数据展示
  • 原文地址:https://www.cnblogs.com/Euryale-Rose/p/6527836.html
Copyright © 2011-2022 走看看