█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 5117

Problema 0x63 - Encontrar el primer elemento mayor a X en un arreglo ordenado

  • Autor: cams2692
  • Fecha: 2021-10-18 04:42:29 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.007 s 11 KBi
#2
Incorrecto
0.004 s 5 KBi
#3
Incorrecto
0.003 s 3 KBi
#4
Incorrecto
0.003 s 5 KBi
#5
Incorrecto
0.003 s 8 KBi
#6
Incorrecto
0.004 s 5 KBi
#7
Incorrecto
0.004 s 5 KBi
#8
Incorrecto
0.003 s 4 KBi
#9
Incorrecto
0.005 s 5 KBi
#10
Incorrecto
0.003 s 4 KBi
#11
Incorrecto
0.003 s 6 KBi
#12
Incorrecto
0.231 s 16 KBi
#13
Incorrecto
0.212 s 13 KBi
#14
Incorrecto
0.265 s 22 KBi
#15
Correcto
0.258 s 20 KBi
#16
Incorrecto
0.294 s 22 KBi
#17
Incorrecto
0.25 s 16 KBi
Puntos totales: 12 / 100

Código

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {

	reader := bufio.NewReader(os.Stdin)

	nString, _ := reader.ReadString('\n')
	nString = strings.ReplaceAll(nString, "\n", "")
	n, _ := strconv.Atoi(nString)
	numbersS, _ := reader.ReadString('\n')
	numbersS = strings.ReplaceAll(numbersS, "\n", "")
	numbers := strings.Split(numbersS, " ")

	cString, _ := reader.ReadString('\n')
	cString = strings.ReplaceAll(cString, "\n", "")

	c, _ := strconv.Atoi(cString)

	mapResult := make(map[string]int)
	results := make([]int, c)
	orderedArray := make([]int64, 0)

	for i := 0; i < c; i++ {
		numberString, _ := reader.ReadString('\n')

		numberString = strings.ReplaceAll(numberString, "\n", "")
		number, _ := strconv.ParseInt(numberString, 10, 64)
		orderedArray = append(orderedArray, number)

		mapResult[numberString] = i
		results[i] = n

	}
	orderedArray = mergeSortNumbers(orderedArray)
	indexCases := 0
	for i := 0; i < n && indexCases < c; i++ {
		number, _ := strconv.ParseInt(numbers[i], 10, 64)
		numberCase := orderedArray[indexCases]
		if number > numberCase {
			results[mapResult[fmt.Sprintf("%d", numberCase)]] = i
			indexCases++
		}

	}
	for _, value := range results {
		fmt.Println(value)
	}

}

func mergeSortNumbers(array []int64) []int64 {
	if len(array) == 1 {
		return array
	}

	lenArrayMid := int(len(array) / 2)

	array1 := mergeSortNumbers(array[:lenArrayMid])
	array2 := mergeSortNumbers(array[lenArrayMid:])

	mergeArray := make([]int64, 0)
	i := 0
	j := 0
	for i < len(array1) && j < len(array2) {
		numberOne := array1[i]
		numberTwo := array2[j]
		if numberOne < numberTwo {
			mergeArray = append(mergeArray, array1[i])
			i++
		} else {
			mergeArray = append(mergeArray, array2[j])
			j++
		}
	}

	if i < len(array1) {
		mergeArray = append(mergeArray, array1[i:]...)
	} else if j < len(array2) {
		mergeArray = append(mergeArray, array2[j:]...)
	}

	return mergeArray
}