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

Envío 6348

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-06-19 21:10:33 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Incorrecto
0.108 s 17 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.091 s 16 KBi
#3
Incorrecto
0.116 s 17 KBi
#4
Incorrecto
0.119 s 17 KBi
#5
Correcto
0.114 s 16 KBi
#6
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.77 s 125 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.725 s 125 KBi
#8
Incorrecto
0.118 s 16 KBi
#9
Incorrecto
0.123 s 17 KBi
#10
Incorrecto
0.116 s 16 KBi
#11
Incorrecto
0.105 s 17 KBi
#12
Incorrecto
0.97 s 117 KBi
#13
Tiempo límite excedido
1.063 s 123 KBi
#14
Incorrecto
0.606 s 64 KBi
#15
Incorrecto
0.807 s 62 KBi
#16
Incorrecto
0.79 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.09 s 16 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.094 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.087 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.095 s 16 KBi
#21
Tiempo límite excedido
1.097 s 115 KBi
#22
Tiempo límite excedido
1.045 s 117 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.104 s 16 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.116 s 19 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.115 s 16 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));
            }
        }
    }
}