最近深陷一道计算几何题无法自拔。。。一片WA换不回一个AC,找点其他题目做一下
按照任务要求完成时间排序,然后使用优先队列,每次累加任务消耗时间,如果所用时间超过了限定时间,则从优先队列中取出时间消耗最大的任务。
Keep the Customer Satisfied
Time Limit: 10000MS | Memory Limit: 65536K | |
Total Submissions: 967 | Accepted: 404 |
Description
Simon and Garfunkel Corporation (SG Corp.) is a large steel-making company with thousand of customers. Keeping the customer satisfied is one of the major objective of Paul and Art, the managers.
Customers issue orders that are characterized by two integer values q , the amount of steel required (in tons) and d , the due date (a calender date converted in seconds). The due date has to be met if SG Corp. accepts the order. Stated another way, when an order is accepted, the corresponding amount of steel has to be produced before its due date. Of course, the factory can process no more than one order at a time.
Although the manufacturing process is rather complex, it can be seen as a single production line with a constant throughput. In the following, we assume that producing q tons of steel takes exactly q seconds (i.e., throughput is 1). The factory runs on a monthly production plan. Before the beginning of the month, all customers' orders are collected and Paul and Art determine which of them are going to be accepted and which ones are to be rejected in the next production period. A production schedule is then designed. To keep customers satisfied, Paul and Art want to minimize the total number of orders that are rejected. In the following, we assume that the beginning of the next production plan (i.e., the first day of the next month) corresponds to date 0.
Hogdson and Moore have been appointed as Chief Scientific Officers and you are requested to help them to compute an optimal solution and to build a schedule of all accepted orders (starting time and completion time).
Small Example
Consider the following data set made of 6 orders J1,..., J6 . For a given order, Jj , qj denotes the amount of steel required and dj is the associated due date.
You can check by hand that all orders cannot be accepted and it's very unlikely you could find a solution with less than two rejected orders. Here is an optimal solution: Reject J1 and J4 , accept all other orders and process them as follows.
Note that the production line is never idle.
Customers issue orders that are characterized by two integer values q , the amount of steel required (in tons) and d , the due date (a calender date converted in seconds). The due date has to be met if SG Corp. accepts the order. Stated another way, when an order is accepted, the corresponding amount of steel has to be produced before its due date. Of course, the factory can process no more than one order at a time.
Although the manufacturing process is rather complex, it can be seen as a single production line with a constant throughput. In the following, we assume that producing q tons of steel takes exactly q seconds (i.e., throughput is 1). The factory runs on a monthly production plan. Before the beginning of the month, all customers' orders are collected and Paul and Art determine which of them are going to be accepted and which ones are to be rejected in the next production period. A production schedule is then designed. To keep customers satisfied, Paul and Art want to minimize the total number of orders that are rejected. In the following, we assume that the beginning of the next production plan (i.e., the first day of the next month) corresponds to date 0.
Hogdson and Moore have been appointed as Chief Scientific Officers and you are requested to help them to compute an optimal solution and to build a schedule of all accepted orders (starting time and completion time).
Small Example
Consider the following data set made of 6 orders J1,..., J6 . For a given order, Jj , qj denotes the amount of steel required and dj is the associated due date.
![](https://i-blog.csdnimg.cn/blog_migrate/6c6608ca3b0590b46fb4bc6b9fb11f8c.jpeg)
You can check by hand that all orders cannot be accepted and it's very unlikely you could find a solution with less than two rejected orders. Here is an optimal solution: Reject J1 and J4 , accept all other orders and process them as follows.
![](https://i-blog.csdnimg.cn/blog_migrate/86a7cc679262f00e83d59af4d92969d2.jpeg)
Note that the production line is never idle.
Input
The first line contains the number n of orders (n can be as large as 800000 for some test cases). It is followed by n lines. Each of which describes an order made of two integer values: the amount of steel (in tons) required for the order (lower than 1000) and its due date (in seconds; lower than 2 x 10
6 ).
Output
You are required to compute an optimal solution and your program has to write the number of orders that are accepted.
Sample Input
6 7 15 8 20 6 8 4 9 3 21 5 22
Sample Output
4
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<cstdio>
using namespace std;
struct task
{
int a,b;
}t[800100];
bool cmp(task a,task b)
{
return a.b<b.b;
}
int n;
int greedy()
{
priority_queue<int> pq;
int cur=0,ans=0;
for(int i=0;i<n;i++)
{
pq.push(t[i].a);
cur+=t[i].a;
if(cur>t[i].b)
{
ans++;
cur-=pq.top();
pq.pop();
}
}
return ans;
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
scanf("%d%d",&t[i].a,&t[i].b);
sort(t,t+n,cmp);
printf("%d\n",n-greedy());
}
return 0;
}