Problem Statement
In Jim's Burger, n hungry burger fans are ordering burgers. The i th order is placed by the i thfan at ti time and it takes di time to procees. What is the order in which the fans will get their burgers?
Input Format
On the first line you will getn , the number of orders. Then n lines will follow. On the (i+1) thline, you will get ti and di separated by a single space.
On the first line you will get
Output Format
Print the order ( as single space separated integers ) in which the burger fans get their burgers. If two fans get the burger at the same time, then print the smallest numbered order first.(remember, the fans are numbered 1 ton ).
Print the order ( as single space separated integers ) in which the burger fans get their burgers. If two fans get the burger at the same time, then print the smallest numbered order first.(remember, the fans are numbered 1 to
Constraints
1≤n≤103
1≤ti,di≤106
Sample Input #00
3
1 3
2 3
3 3
Sample Output #00
1 2 3
Explanation #00
The first order is placed at time 1 and it takes 3 units of time to process, so the burger is sent to the customer at time 4. The 2nd and 3rd are similarly processed at time 5 and time 6. Hence the order
1 2 3
.
Sample Input #01
5
8 1
4 2
5 6
3 1
4 3
Sample Output #01
4 2 5 1 3
Explanation #01
The first order is placed at time 3 and it takes 1 unit of time to process, so the burger is sent to the customer at time 4.
The second order is placed at time 4 and it takes 2 units of time to process, the burger is sent to customer at time 6.
The third order is placed at time 4 and it takes 3 units of time to process, the burger is sent to the customer at time 7.
Similarly, the fourth and fifth orders are sent to the customer at time 9 and time 11.
The second order is placed at time 4 and it takes 2 units of time to process, the burger is sent to customer at time 6.
The third order is placed at time 4 and it takes 3 units of time to process, the burger is sent to the customer at time 7.
Similarly, the fourth and fifth orders are sent to the customer at time 9 and time 11.
So the order of delivery of burgers is,
4 2 5 1 3
.
Copyright © 2015 HackerRank.
All Rights Reserved
All Rights Reserved
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException{
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
long[] time = new long[n];
long[] delay = new long[n];
long[] sum = new long[n];
for(int i = 0 ; i < n ; i++)
{
String s = br.readLine();
String[] in = s.split(" ");
time[i] = Integer.parseInt(in[0]);
delay[i] = Integer.parseInt(in[1]);
sum[i] = time[i] + delay[i];
}
Map<Long, Integer> map = new TreeMap<Long, Integer>();
for(int i = 0 ; i < n ; i++ )
{
map.put((sum[i] * n + i), i);
}
for( Integer i : map.values())
{
System.out.print(i+1+" ");
}
}
}
No comments:
Post a Comment