-
Notifications
You must be signed in to change notification settings - Fork 79
/
IceCreamParlor.java
111 lines (79 loc) · 2.54 KB
/
IceCreamParlor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
class IceCream implements Comparable{
int flavor;
int index;
public IceCream(int flavor, int index) {
this.flavor = flavor;
this.index = index;
}
@Override
public int compareTo(Object o) {
IceCream o2 = (IceCream) o;
if(this.flavor != o2.flavor)
return this.flavor - o2.flavor;
return this.index - o2.index;
}
@Override
public boolean equals(Object o) {
if(o == null)
return false;
if(o == this)
return true;
IceCream ice = (IceCream) o;
return this.flavor == ice.flavor && this.index == ice.index;
}
public String toString() {
return this.flavor + " " + this.index + ";";
}
}
public class Solution {
public static int binarySearch(int first, int last, IceCream[] arr, int search) {
//System.out.println(Arrays.toString(arr));
while(first <= last) {
int mid = first + (last-first)/2;
//System.out.printf("Debug: %d %d\n", mid, search);
int flavor = arr[mid].flavor;
if(flavor == search)
return arr[mid].index;
if(flavor > search) {
last = mid - 1;
} else {
first = mid + 1;
}
}
//System.out.printf("Debug: -1 %d\n", search);
return -1;
}
public static void main(String[] args) {
int t;
int n, m;
Scanner in = new Scanner(System.in);
t = in.nextInt();
for(int test = 0; test < t; test++) {
m = in.nextInt();
n = in.nextInt();
IceCream[] arr = new IceCream[n];
for (int i = 0; i < n; i++) {
arr[i] = new IceCream(in.nextInt(), i + 1);
}
// System.out.println(Arrays.toString(arr));
Arrays.sort(arr);
// System.out.println(Arrays.toString(arr));
int firstIndex = 100000, secondIndex = 100000;
for(int i = 0; i < n - 1 ; i++) {
int search = m - arr[i].flavor;
if(search >= arr[i].flavor) {
int index = binarySearch( i + 1, n - 1, arr, search);
if( index != -1 ) {
System.out.println( Math.min(arr[i].index, index) + " " + Math.max(arr[i].index, index));
break;
}
}
}
}
}
}