列表

详情


NC24880. [USACO 2009 Ope G]Work Scheduling

描述

Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.
His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs conveniently numbered 1..N for work to do. It is possible but extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.
Job i has deadline Di (1 <= Di <= 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 <= Pi <= 1,000,000,000).
What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.

输入描述

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains two space-separated integers: Di and Pi

输出描述

* Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn.

示例1

输入:

3 
2 10 
1 5 
1 7 

输出:

17

说明:

Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 518ms, 内存消耗: 23072K, 提交时间: 2021-06-22 20:53:51

import heapq
from operator import itemgetter
n=int(input())
heap=[]
test = [[0 for i in range(2)] for j in range(n)]
i=0
for i in range(n):
    a,b=map(int,input().split())
    test[i][0]=a
    test[i][1]=b
test.sort(key=itemgetter(0))
ans=0
t=0
for i in range(n):
    if t<test[i][0]:
        ans+=test[i][1]
        heapq.heappush(heap,test[i][1])
        t+=1
    else:
        w=heapq.heappop(heap)
        heapq.heappush(heap, w)
        if test[i][1]>=w and test[i][0]==t:
            heapq.heappop(heap)
            ans=ans+test[i][1]-w;
            heapq.heappush(heap, test[i][1])
print(ans)

C++14(g++5.4) 解法, 执行用时: 61ms, 内存消耗: 1784K, 提交时间: 2020-01-30 12:51:33

#include<bits/stdc++.h>
using namespace std;
struct edge{
    int t,v;
}e[100005];
bool cmp(edge a,edge b){
    return a.t<b.t;
}
priority_queue<int,vector<int>,greater<int> >q;
int main(){
    int n;
    long long ans=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++) scanf("%d%d",&e[i].t,&e[i].v);
    sort(e,e+n,cmp);
    for(int i=0;i<n;i++){
        if(e[i].t<=q.size()){
            if(e[i].v>q.top()) ans+=e[i].v-q.top(),q.pop(),q.push(e[i].v);
        }
        else ans+=e[i].v,q.push(e[i].v);
    }
    printf("%lld\n",ans);
}

C++11(clang++ 3.9) 解法, 执行用时: 75ms, 内存消耗: 1784K, 提交时间: 2020-02-26 17:37:48

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int now,n;
long long s;
priority_queue<int> q;
struct cow
{
	int s,v;
}c[100005];
bool cmp(cow a,cow b)
{
	return a.s<b.s;
}
void init()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	scanf("%d%d",&c[i].s,&c[i].v);
	sort(c+1,c+1+n,cmp);
}
void work()
{
	for(int i=1;i<=n;i++)
	{
		s+=c[i].v;
		now++;
		q.push(-c[i].v);
		if(now>c[i].s)
		s+=q.top(),q.pop(),now--;
	}
	printf("%lld\n",s);
}
int main()
{
	init();
	work();
	return 0;
}

上一题