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

Envío 5116

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

  • Autor: cams2692
  • Fecha: 2021-10-18 02:19:42 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Incorrecto
0.005 s 14 KBi
#2
Incorrecto
0.004 s 5 KBi
#3
Incorrecto
0.005 s 5 KBi
#4
Incorrecto
0.005 s 22 KBi
#5
Incorrecto
0.003 s 16 KBi
#6
Incorrecto
0.003 s 4 KBi
#7
Incorrecto
0.003 s 4 KBi
#8
Incorrecto
0.005 s 5 KBi
#9
Incorrecto
0.005 s 18 KBi
#10
Incorrecto
0.004 s 14 KBi
#11
Incorrecto
0.007 s 54 KBi
#12
Incorrecto
0.079 s 15 KBi
#13
Incorrecto
0.087 s 15 KBi
#14
Incorrecto
0.264 s 22 KBi
#15
Incorrecto
0.256 s 24 KBi
#16
Incorrecto
0.277 s 25 KBi
#17
Incorrecto
0.09 s 21 KBi
Puntos totales: 0 / 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)

	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] = len(numbers)

	}
	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 {
			mapResult[fmt.Sprintf("%d", numberCase)] = i
			indexCases++
		}

	}

	for _, value := range mapResult {
		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
}

/*func quickSort(array []string) []string {
	if len(array) <= 1 {
		return array
	}
	i := 0
	j := len(array) - 1

	for i < j {
		pivotS := array[i]
		pivot, _ := strconv.ParseInt(pivotS, 10, 64)
		item := array[j]
		itemNumber, _ := strconv.ParseInt(item, 10, 64)
		if pivot < itemNumber {
			j--
		} else {
			itemAux := array[i]
			array[i] = array[j]
			array[j] = itemAux
			j--
			i++
		}
	}

	arrayNew := make([]string, 0)
	arrayNew = append(arrayNew, quickSort(array[:j])...)
	arrayNew = append(arrayNew, quickSort(array[i:])...)
	return arrayNew
}*/