Jim and the Orders
Problem Statement :
Jim's Burgers has a line of hungry customers. Orders vary in the time it takes to prepare them. Determine the order the customers receive their orders. Start by numbering each of the customers from to , front of the line to the back. You will then be given an order number and a preparation time for each customer. The time of delivery is calculated as the sum of the order number and the preparation time. If two orders are delivered at the same time, assume they are delivered in ascending customer number order. For example, there are customers in line. They each receive an order number and a preparation time .: Customer 1 2 3 4 5 Order # 8 5 6 2 4 Prep time 3 6 2 3 3 Calculate: Serve time 11 11 8 5 7 We see that the orders are delivered to customers in the following order: Order by: Serve time 5 7 8 11 11 Customer 4 5 3 1 2 Function Description Complete the jimOrders function in the editor below. It should return an array of integers that represent the order that customers' orders are delivered. jimOrders has the following parameter(s): orders: a 2D integer array where each is in the form . Input Format The first line contains an integer , the number of customers. Each of the next lines contains two space-separated integers, an order number and prep time for . Output Format Print a single line of space-separated customer numbers (recall that customers are numbered from to ) that describes the sequence in which the customers receive their burgers. If two or more customers receive their burgers at the same time, print their numbers in ascending order.
Solution :
Solution in C :
in C :
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
typedef long long int ll;
typedef struct list
{
ll t;
ll d;
ll pos
}L;
int compare(const void *a,const void *b)
{
L *e1 = (L *)a;
L *e2 = (L *)b;
return (e1->d+e1->t)-(e2->t+e2->d);
}
L val[1005];
int main()
{
ll n,i;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld%lld",&val[i].t,&val[i].d);
val[i].pos=i+1;
}
qsort(val,n,sizeof(L),compare);
for(i=0;i<n;i++)
{
printf("%lld ",val[i].pos);
}
printf("\n");
return 0;
}
Solution in C++ :
in C++ :
#include <bits/stdc++.h>
using namespace std;
int N;
pair<int, int> A[1000];
int main()
{
scanf("%d", &N);
int a, b;
for(int i=0; i<N; i++)
{
scanf("%d%d", &a, &b);
A[i]=make_pair(a+b, i+1);
}
sort(A, A+N);
for(int i=0; i<N; i++)
printf("%d ", A[i].second);
printf("\n");
return 0;
}
Solution in Java :
In Java :
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;
public class A {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
int n = ni();
int[][] a = new int[n][];
for(int i = 0;i < n;i++){
a[i] = new int[]{ni() + ni(), i};
}
Arrays.sort(a, new Comparator<int[]>() {
public int compare(int[] a, int[] b) {
if(a[0] != b[0])return a[0] - b[0];
return a[1] - b[1];
}
});
for(int i = 0;i < n;i++){
if(i > 0)out.print(" ");
out.print(a[i][1]+1);
}
out.println();
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
Solution in Python :
tuples = []
a = int(input())
for i in range(a):
x, y = map(int, input().split(' '))
tuples.append((x+y, i+1))
tuples.sort(key = lambda x:x[0])
print(' '.join([str(x[1]) for x in tuples]))
View More Similar Problems
Truck Tour
Suppose there is a circle. There are N petrol pumps on that circle. Petrol pumps are numbered 0 to (N-1) (both inclusive). You have two pieces of information corresponding to each of the petrol pump: (1) the amount of petrol that particular petrol pump will give, and (2) the distance from that petrol pump to the next petrol pump. Initially, you have a tank of infinite capacity carrying no petr
View Solution →Queries with Fixed Length
Consider an -integer sequence, . We perform a query on by using an integer, , to calculate the result of the following expression: In other words, if we let , then you need to calculate . Given and queries, return a list of answers to each query. Example The first query uses all of the subarrays of length : . The maxima of the subarrays are . The minimum of these is . The secon
View Solution →QHEAP1
This question is designed to help you get a better understanding of basic heap operations. You will be given queries of types: " 1 v " - Add an element to the heap. " 2 v " - Delete the element from the heap. "3" - Print the minimum of all the elements in the heap. NOTE: It is guaranteed that the element to be deleted will be there in the heap. Also, at any instant, only distinct element
View Solution →Jesse and Cookies
Jesse loves cookies. He wants the sweetness of all his cookies to be greater than value K. To do this, Jesse repeatedly mixes two cookies with the least sweetness. He creates a special combined cookie with: sweetness Least sweet cookie 2nd least sweet cookie). He repeats this procedure until all the cookies in his collection have a sweetness > = K. You are given Jesse's cookies. Print t
View Solution →Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →