原文轉(zhuǎn)載至:http://blog.csdn.net/fenrir1205/article/details/8275011
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct Schedule
{
int profit;
int dt;
//先處理高利潤的
bool operator<(const Schedule &rhs)const {
return profit<rhs.profit;
}
};
bool used[10001];
priority_queue<Schedule> que;
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
while(!que.empty())que.pop();
int max = 0;
for (int i=0;i<n;i++){
Schedule prod;
scanf("%d %d",&prod.profit,&prod.dt);
if (prod.dt>max) max = prod.dt;
que.push(prod);
}
memset(used,false,sizeof(bool)*(max+1));
long long sum =0;
int current = 0;
//從前面往后面去檢查,對于每一個products肘习,從其deadtime開始往前尋找沒有被標(biāo)記過的時間點直至?xí)r間0點或者找到?jīng)]有被標(biāo)記的际乘,然后標(biāo)記,并把這個profit加起來井厌。
while(!que.empty()){
Schedule prod = que.top();
que.pop();
while(prod.dt--){
if (!used[prod.dt]){
used[prod.dt] = true;
sum+=prod.profit;
break;
}
}
}
printf("%d\n",sum);
}
return 0;
}