2 qq 36642012 qq_36642012 于 2017.09.15 00:23 提问

CF中第12个测验运行时错误

以下是题目内容:
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.

Input
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.

Output
Print the sum of all penalties if Arthur completes the PAs in the optimal order.
下面是我的代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class Main {

public static void main(String[] args)
{
    class Task
    {
        long l1;
        long l2;
        public Task(long l1,long l2)
        {
            this.l1=l1;
            this.l2=l2;
        }
    }
    Scanner s=new Scanner(System.in);
    int a;long b;
    a=s.nextInt();b=s.nextLong();
    //long aa[][]=new long[a][2];
    ArrayList<Task> al=new ArrayList<Task>();
    long input1,input2;
    for(int i=0;i<a;i++)
    {
        input1=s.nextLong();
        input2=s.nextLong();
    //    aa[i][0]=input1;
      //  aa[i][1]=input2;
        al.add(new Task(input1,input2));
    }
    /*for(int i=0;i<a-1;i++)
        for(int j=0;j<a-i-1;j++)
        {
            if(aa[j][0]>aa[j+1][0])
            {
                long temp1,temp2;
                temp1=aa[j+1][0];temp2=aa[j+1][1];
                aa[j+1][0]=aa[j][0];aa[j+1][1]=aa[j+1][1];
                aa[j][0]=temp1;aa[j][1]=temp2;
            }
        }*/
    Collections.sort(al,new Comparator<Task>()
    {
         public int compare(Task p1,Task p2)
         {
              return p1.l1<p2.l1?-1:1;
         }
    });

    long sum=b;
    long sum1=0L;

/* for(int i=0;i<a;i++)
{
sum+=aa[i][0];
sum1+=sum-aa[i][1];
}*/
/* for(int i=0;i<a;i++)
{
System.out.println(al.get(i).l1+" "+al.get(i).l2);
}*/
for(int i=0;i<a;i++)
{
sum+=al.get(i).l1;
sum1+=sum-al.get(i).l2;
}
/* for(int i=0;i<a;i++)
{
System.out.println(al.get(i).l1+" "+al.get(i).l2);
}*/
System.out.println(sum1);
}
}

1个回答

devmiao
devmiao   Ds   Rxr 2017.09.15 09:04
Csdn user default icon
上传中...
上传图片
插入图片