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

Envío 5118

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

  • Autor: cams2692
  • Fecha: 2021-10-18 04:50:42 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 4 KBi
#2
Correcto
0.004 s 3 KBi
#3
Correcto
0.004 s 6 KBi
#4
Correcto
0.006 s 5 KBi
#5
Correcto
0.005 s 4 KBi
#6
Correcto
0.004 s 4 KBi
#7
Correcto
0.004 s 6 KBi
#8
Correcto
0.006 s 4 KBi
#9
Correcto
0.004 s 3 KBi
#10
Correcto
0.003 s 4 KBi
#11
Correcto
0.003 s 4 KBi
#12
Correcto
0.231 s 21 KBi
#13
Correcto
0.271 s 17 KBi
#14
Correcto
0.258 s 26 KBi
#15
Correcto
0.252 s 20 KBi
#16
Correcto
0.223 s 21 KBi
#17
Correcto
0.231 s 23 KBi
Puntos totales: 100 / 100

Código

package main

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

type Case struct {
	Index int
	Case  int64
}

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)

	results := make([]int, c)
	orderedArray := make([]Case, 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, Case{
			Index: i,
			Case:  number,
		})
		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.Case {
			results[numberCase.Index] = i
			indexCases++
			i--
		}

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

}

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

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

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

	mergeArray := make([]Case, 0)
	i := 0
	j := 0
	for i < len(array1) && j < len(array2) {
		numberOne := array1[i].Case
		numberTwo := array2[j].Case
		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
}