P2949 [USACO09OPEN] Work Scheduling G

原题链接

题解

反悔贪心
把工作按截至时间排序,每个工作有两个决策。
如果这个工作有时间做,那就做;
如果没时间做,就在已经做过的工作里取消价值最小的工作,换成当前工作(这里有一个前提,那就是每个工作需要的时间是一样的,而且当前工作的价值大于已经做过工作里价值最小的)

code

#include<bits/stdc++.h>
using namespace std;

#define ll long long

struct node
{
    ll d, v;
} w[100005];

bool cmp(node b, node c)
{
    return b.d < c.d;
}

inline void read(ll &x) {
	x = 0;
	ll flag = 1;
	char c = getchar();
    while(c < '0' || c > '9'){
        if(c == '-') flag = -1;
        c = getchar();
    }
	while(c >= '0' && c <= '9') {
		x = (x << 3) + (x << 1) + (c ^ 48);
		c = getchar();
	}
	x *= flag;
}

inline void write(ll x)
{
    if(x < 0){
    	putchar('-');
		x = -x;
	}
    if(x > 9)
		write(x / 10);
    putchar(x % 10 + '0');
}

int main()
{
    ll n;
    read(n);
    for(ll i = 1; i <= n; i++)
    {
        read(w[i].d);
        read(w[i].v);
    }

    sort(w + 1, w + 1 + n, cmp);

    ll it = 1;
    priority_queue<ll, vector<ll>, greater<ll>> q;
    for(ll i = 1; i <= n; i++)
    {
        if(w[i].d >= it)
        {
            q.push(w[i].v);
            it++;
        }
        else
        {
            ll val = q.top();
            if(val < w[i].v)
            {
                q.pop();
                q.push(w[i].v);
            }
        }
    }

    ll sum = 0;
    while(!q.empty())
    {
        sum += q.top();
        q.pop();
    }
    write(sum);
    return 0;
}

posted @   纯粹的  阅读(22)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示