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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ - 3616是一个题目,题目描述如下: 给定一组区间,每个区间有一个权重,要求选择一些区间,使得这些区间的右端点都小于等于k,并且权重之和最大。请问最大的权重和是多少? 解决这个问题的思路是使用动态规划。首先,将区间按照左端点从小到大进行排序。然后,定义一个dp数组,dp[i]表示右端点小于等于i的所有区间所能得到的最大权重。 接下来,遍历每一个区间,对于每个区间i,将dp[i]初始化为区间i的权重。然后,再遍历i之前的每个区间j,如果区间j的右端点小于等于k,并且区间j的权重加上区间i的权重大于dp[i],则更新dp[i]为dp[j]加上区间i的权重。 最后,遍历整个dp数组,找到最大的权重和,即为所求的答案。 下面是具体的代码实现: ```cpp #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct interval{ int start, end, weight; }; interval intervals[10005]; int dp[10005]; int n, m, k; bool compare(interval a, interval b) { if (a.start == b.start) { return a.end < b.end; } else { return a.start < b.start; } } int main() { while(~scanf("%d %d %d", &n, &m, &k)) { memset(dp, 0, sizeof dp); for (int i = 0; i < m; i++) { scanf("%d %d %d", &intervals[i].start, &intervals[i].end, &intervals[i].weight); } sort(intervals, intervals + m, compare); for (int i = 0; i < m; i++) { dp[i] = intervals[i].weight; for (int j = 0; j < i; j++) { if (intervals[j].end <= k && dp[j] + intervals[i].weight > dp[i]) { dp[i] = dp[j] + intervals[i].weight; } } } int maxWeight = 0; for (int i = 0; i < m; i++) { maxWeight = max(maxWeight, dp[i]); } printf("%d\n", maxWeight); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值