Caso # | Resultado | Tiempo | Memoria |
---|---|---|---|
#1 |
Correcto
|
0.088 s | 16 KBi |
#2 |
Correcto
|
0.089 s | 16 KBi |
#3 |
Correcto
|
0.139 s | 18 KBi |
#4 |
Correcto
|
0.082 s | 16 KBi |
#5 |
Correcto
|
0.081 s | 16 KBi |
#6 |
Correcto
|
0.085 s | 16 KBi |
#7 |
Correcto
|
0.098 s | 16 KBi |
#8 |
Correcto
|
0.161 s | 15 KBi |
#9 |
Correcto
|
0.1 s | 16 KBi |
#10 |
Correcto
|
0.09 s | 17 KBi |
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] a = new int[n]; for(int i=0; i<n; i++){ a[i] = scanner.nextInt(); } Arrays.sort(a); int c = scanner.nextInt(); for(int i=0; i<c; i++){ int consulta = scanner.nextInt(); int index = findSuccesor(a, 0, n-1, consulta); System.out.println(n - index); } } private static int findSuccesor(int[] a, int start, int end, int search) { if(end == start + 1 || end == start){ if(search < a[start]){ return start; } if(search < a[end]){ return end; } return a.length; } int middle = (end+start)/2; boolean flag = true; if(middle - 1 >= 0){ if(search < a[middle-1]){ flag = false; } } if(search >= a[middle]){ flag= false; } if(flag) { return middle; } if (search<a[middle]){ return findSuccesor(a, start, middle-1, search); } else { return findSuccesor(a, middle+1, end, search); } } }