B - 优先队列 POJ - 2970

1. 题目 POJ - 2970

A new web-design studio, called SMART (Simply Masters of ART), employs two people. The first one is a web-designer and an executive director at the same time. The second one is a programmer. The director is so a nimble guy that the studio has already got N contracts for web site development. Each contract has a deadline di.
It is known that the programmer is lazy. Usually he does not work as fast as he could. Therefore, under normal conditions the programmer needs bi of time to perform the contract number i. Fortunately, the guy is very greedy for money. If the director pays him xi dollars extra, he needs only (bi − ai xi) of time to do his job. But this extra payment does not influent other contract. It means that each contract should be paid separately to be done faster. The programmer is so greedy that he can do his job almost instantly if the extra payment is (bi ⁄ ai) dollars for the contract number i.
The director has a difficult problem to solve. He needs to organize programmer’s job and, may be, assign extra payments for some of the contracts so that all contracts are performed in time. Obviously he wishes to minimize the sum of extra payments. Help the director!

Input
The first line of the input contains the number of contracts N (1 ≤ N ≤ 100 000, integer). Each of the next N lines describes one contract and contains integer numbers ai, bi, di (1 ≤ ai, bi ≤ 10 000; 1 ≤ di ≤ 1 000 000 000) separated by spaces.

Output
The output needs to contain a single real number S in the only line of file. S is the minimum sum of money which the director needs to pay extra so that the programmer could perform all contracts in time. The number must have two digits after the decimal point.

Sample Input
2
20 50 100
10 100 50
Sample Output
5.00

2.题目大意
有n个任务要完毕,每一个任务有属性 a,b,d
分别代表 额外工资,完成工作所需时间,结束时间。
假设不给钱。那么完毕时间为b,给x的额外工资,那么完毕时间b变成 b-a*x
求在全部任务在各自最后期限前完毕所须要给的最少的钱
3.解题思路
把所有任务按照结束时间d进行排序;然后依次按照结束时间的先后完成任务。如果当前这个任务无法完成,则要找到之前已经完成的任务中找到a最大的任务,把这个任务通过加钱减少完成工作所需的时间(即完成工作所需时间b变成 b-ax)。其中这个a就需要使用到优先队列来进行维护。
4.优先队列内容
https://blog.csdn.net/weixin_36888577/article/details/79937886
5.AC代码

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<cstring>
#include<string>
#include<queue>
#define LL long long int
#define mst(a) memset(a,0,sizeof(a))
using namespace std;
const int maxn=1e5+10;
struct one
{
    LL ai,bi,di;
    bool operator < (const one &s) const//在这个队列中按照ai从大到小排列,大的在前面
    {
        return s.ai > ai;
    }

}a[maxn];
bool cmp(one a, one b)
{
    return a.di < b.di;//sort排序时以此作为根据
}
//priority_queue优先队列,插入进去的元素都会从大到小排好序
//priority_queue<int, vector<int>, cmp>q;    //定义方法
//priority_queue<LL,vector<LL>,greater<LL> >q;
//其中,第二个参数为容器类型。第三个参数为比较函数。
priority_queue<one>q;
int main()
{
  int n ;   scanf("%d" ,&n);
  for(int i =0; i<n; i++)
  {
      scanf("%lld%lld%lld",&a[i].ai,&a[i].bi,&a[i].di);
  }
   double fin=0;
   sort(a,a+n,cmp);//将所有任务按照截止时间的先后排序
   LL sum =0;
   for(int i =0; i < n ; i++)
   {
       sum+=a[i].bi;// 时间的流逝
       q.push(a[i]);
       while(!q.empty() && sum > a[i].di)//如果加上这个任务所需要的时间后,会超出截止时间
       {
           one temp = q.top();
           q.pop();
           if(sum - a[i].di > temp.bi) //如果所有已经完成的任务中ai最大的任务的bi就算全部弄掉都还会超出截止时间
           {
               sum -= temp.bi; //把ai最大的这个任务的全部所需时间减掉,都还会超出截止时间。
               fin += temp.bi*1.0/temp.ai;    //额外的钱等于=b/a;
           }
           else //把ai最大的那个任务快点完成就不会超出截止时间
           {
               fin += (sum-a[i].di)*1.0/temp.ai;
               temp.bi -= (sum - a[i].di);
               sum = a[i].di;//使目前的时间正好等于目前任务的截止时间
               q.push(temp);
           }
       }
   }
   printf("%.2f\n",fin);//f,lf????????
    return 0;
}



发布了2 篇原创文章 · 获赞 0 · 访问量 50

猜你喜欢

转载自blog.csdn.net/weixin_45674799/article/details/104352995