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

Envío 6349

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-06-19 21:10:52 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Incorrecto
0.116 s 16 KBi
#2
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "1000000000000000000"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.11 s 16 KBi
#3
Incorrecto
0.125 s 16 KBi
#4
Incorrecto
0.121 s 16 KBi
#5
Correcto
0.104 s 17 KBi
#6
Tiempo límite excedido
1.014 s 118 KBi
#7
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.726 s 125 KBi
#8
Incorrecto
0.099 s 16 KBi
#9
Incorrecto
0.128 s 17 KBi
#10
Incorrecto
0.1 s 13 KBi
#11
Incorrecto
0.102 s 16 KBi
#12
Incorrecto
0.935 s 118 KBi
#13
Incorrecto
0.995 s 121 KBi
#14
Incorrecto
0.827 s 60 KBi
#15
Incorrecto
0.99 s 62 KBi
#16
Incorrecto
0.929 s 60 KBi
#17
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "100000000000000000"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.085 s 13 KBi
#18
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "100000000000000001"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.106 s 16 KBi
#19
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "100000000000000002"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.106 s 16 KBi
#20
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "100000000000000003"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.091 s 17 KBi
#21
Tiempo límite excedido
1.095 s 110 KBi
#22
Incorrecto
0.994 s 121 KBi
#23
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "999999999999997"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.101 s 13 KBi
#24
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "1000000000000000000"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.084 s 16 KBi
#25
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.util.InputMismatchException: For input string: "999999999999999999"
	at java.base/java.util.Scanner.nextInt(Scanner.java:2264)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at Main.main(Main.java:8)
0.093 s 13 KBi
Puntos totales: 4 / 100

Código

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int r = scanner.nextInt();

        HashMap<Integer, Integer> parent = new HashMap<>();
        HashMap<Integer, Boolean> unprocessed = new HashMap<>();
        HashMap<Integer, Integer> cycleStart = new HashMap<>();
        HashMap<Integer, Integer> cycleSize = new HashMap<>();

        for(int i=0; i<n; i++){
            parent.put(i, scanner.nextInt());
            unprocessed.put(i, true);
        }

        int cycle = 0;
        while(!unprocessed.isEmpty()){
            int count = 0;
            int current = (int) unprocessed.keySet().toArray()[0];
            cycleStart.put(cycle, current);

            while(unprocessed.containsKey(current)){
                count++;
                unprocessed.remove(current);
                current = parent.get(current);
            }

            cycleSize.put(cycle, count);
            cycle++;
        }


        int[] result = new int[n];
        for(int i=0; i<cycle; i++){
            int currentStart = cycleStart.get(i);
            int rounds = r % cycleSize.get(i);

            int newStart = currentStart;
            for(int j=0; j<rounds; j++){
                newStart = parent.get(newStart);
            }

            int currentPut = newStart;
            int currentGet = currentStart;
            do{
                result[currentPut] = currentGet;
                currentPut = parent.get(currentPut);
                currentGet = parent.get(currentGet);
            }while(currentGet != currentStart);
        }

        for(int i=0; i<n; i++){
            if(i == n-1){
                System.out.print((result[i]+1) +" ");
            } else {
                System.out.print((result[i]+1));
            }
        }
    }
}