File Transfer - byte beating title

Because the Internet read this title, no java language to write, and I wrote a (title comes from the network) with java
topic describes
the server for data backup, you need to transfer a group of files. At any time only one file is transferred, a file transfer needs second.
There are n batch file, we know that these documents add time transmission queue (unit: seconds), and the number of these documents C.
Files in the transmission queue is transmitted at a rate of one second a file.
Now in charge of the file transfer students want to know all the time the file is completely transmitted, and transmission queue files accumulate the maximum number.

Input Description :
The first line of an integer n, n representatives batch file.
The next two integers n for each row line t, c, representative of the number of added queue time these files and these files.

For 50% of the data
. 1 <= n-<^ = 10. 3
. 1 <= T, C <^ = 10. 6

For 50% of the data
. 1 <= n-<^ = 10. 5
. 1 <= T, C <^ = 10. 9

Output Description
The maximum number of output line of two integers, all files are completely transmitted and the transmission queue file time accumulation.

Example 1
Input
. 3
1 1
2 1
. 3 1
Output
4 1
Example 1 illustrates the
first arrival time of 1 second file, a file is added to the queue;
2 Li coming second batch file, when the file has been transmitted queue is completed, after addition of the number of documents in the queue file is still one queue;
Similarly when 3 seconds;
the third batch of four seconds when the file transfer is completed.

Example 2
input
. 3
. 1. 3
2. 3
. 3. 3
Output
107
The main idea is that a file sent to a difference before and after sorting requires attention, the problem is not stated order.

import java.util.*;

public class Main7 {

    static class heap{
        int time;
        int num;

        public heap(int time, int num) {
            this.time = time;
            this.num = num;
        }
    }

    static int heap_time = 0;
    static long heap_sum = 0;

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        List<heap> list = new ArrayList<>();
        int n = in.nextInt();
        for (int i = 0; i < n; i++) {
            list.add(new heap(in.nextInt(),in.nextInt()));
        }

        Collections.sort(list, new Comparator<heap>() {
            @Override
            public int compare(heap o1, heap o2) {
                return o1.time - o2.time;
            }
        });

        heap_time = list.get(0).time + + list.get(0).num;
        heap_sum = list.get(0).num;
        for (int i = 1; i < list.size(); i++) {
            int i1 = heap_time - list.get(i).time;
            if(i1 == 0){
                heap_time += list.get(i).num;
            }else if(i1 > 0 ){
                heap_time = heap_time + list.get(i).num;
                heap_sum = Math.max(i1+list.get(i).num,heap_sum);
            }else {
                heap_time = heap_time + (-1 * i1) + list.get(i).num;
                heap_sum = Math.max(list.get(i).num,heap_sum);
            }
        }
        System.out.println(heap_time+" "+heap_sum);

    }

}

Published 11 original articles · won praise 8 · views 1583

Guess you like

Origin blog.csdn.net/weixin_44627238/article/details/104877575
Recommended