Another semester has ended and Arthur finally achieved his dream of attending Data Structures I with all professors in the Mathematics Department. Now, he can finally pass this subject, but, like everyone expected, he didn't do any PAs (programming assignments), and all deadlines have passed.
Fortunately, all PAs can still be submitted for grading, but with a penalty given by: (late submission time) - (expected deadline time) for each PA.
Arthur, having taken Data Structures I so many times, knows exactly how much time he needs to complete each assignment. Now, he wants to write a program that determines the minimum sum of penalties that can be achieved, given he can do the PAs in any order.
It's worth noting that Arthur can't do more than one assignment at a time, since that skill is only learned in Data Structures II. Therefore, if Arthur starts working on an assignment, he needs to finish it before starting any other.
There is only one problem left: Arthur believes this problem to be unsettlingly similar to a PA, and, therefore, refuses to do it.
Help Arthur complete this task and, finally, take Data Structures II.
The first line of input contains two integers 1 ≤ n ≤ 105 and 1 ≤ s ≤ 109, the amount of PAs Arthur needs to do and the time when he started to do them, respectively.
n lines follow, the i-th line contains two integers 1 ≤ ti ≤ 109 and 0 ≤ ei ≤ 109, the time Arthur takes to complete the i-th assignment and the expected deadline time for that assignment.
It is guaranteed s > ei for all i.
Print the sum of all penalties if Arthur completes the PAs in the optimal order.
2 1
2 0
1 0
6
In the first example, if Arthur does the second PA first, he finishes it at time 2, and finishes the first one at time 4, making his total penalty equals to (2-0)+(4-0) = 6.
题意:第一行输入两个数,代表 n 个任务和开始做任务的时间 s ,接下来的n 行每行输入完成任务需要的时间 t 和该任务的截至时间 d ,每个任务都有一个罚时(罚时=完成任务的时间-该任务的截止时间),
求完成所有任务的最小总罚时。
题解:贪心处理,将完成任务的时间从小到大排序,若完成时间相同,按截至时间从小到大排序
#include<iostream> #include<algorithm> #include<math.h> #define ll long long using namespace std; struct node { ll t; ll d; }p[100005]; bool cmp(node a,node b) { if(a.t!=b.t) return a.t<b.t; else return a.d<b.d; } int main() { ll n,s; cin>>n>>s; for(int i=0;i<n;i++) { cin>>p[i].t>>p[i].d; } sort(p,p+n,cmp); ll cnt=0; for(int i=0;i<n;i++) { //cout<<p[i].t<<' '<<p[i].d<<endl; s=s+p[i].t; cnt=cnt+s-p[i].d; } cout<<cnt<<endl; return 0; }