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

Envío 6286

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 22:20:04 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.103 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:14)
0.092 s 16 KBi
#3
Correcto
0.105 s 16 KBi
#4
Correcto
0.106 s 17 KBi
#5
Correcto
0.093 s 16 KBi
#6
Tiempo límite excedido
1.039 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.663 s 125 KBi
#8
Correcto
0.101 s 16 KBi
#9
Correcto
0.096 s 18 KBi
#10
Correcto
0.104 s 16 KBi
#11
Correcto
0.1 s 16 KBi
#12
Correcto
0.96 s 120 KBi
#13
Correcto
0.734 s 112 KBi
#14
Correcto
0.833 s 63 KBi
#15
Correcto
0.707 s 59 KBi
#16
Correcto
0.829 s 61 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:14)
0.089 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:14)
0.119 s 52 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:14)
0.079 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:14)
0.09 s 16 KBi
#21
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.87 s 125 KBi
#22
Tiempo límite excedido
1.038 s 94 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:14)
0.185 s 14 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:14)
0.091 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:14)
0.086 s 16 KBi
Puntos totales: 52 / 100

Código

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

public class Main {
    public static void main(String[] args) {
        HashMap<Integer, Integer> fromTo = new HashMap<Integer, Integer>();
        HashMap<Integer, Boolean> unprocessedNodes = new HashMap<Integer, Boolean>();
        HashMap<Integer, ArrayList<Integer>> subGraphIds = new HashMap<Integer, ArrayList<Integer>>();
        HashMap<Integer, Integer> nodeSubgraphId = new HashMap<Integer, Integer>();

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int r = scanner.nextInt();

        for(int from=0; from<n; from++){
            int to = scanner.nextInt();
            fromTo.put(from, to);
            unprocessedNodes.put(from, false);
        }

        int subGraphId = 0;
        while(!unprocessedNodes.isEmpty()){
            int currentNode = (int) unprocessedNodes.keySet().toArray()[0];
            unprocessedNodes.remove(currentNode);

            ArrayList<Integer> arrayList = new ArrayList<Integer>();
            arrayList.add(currentNode);
            subGraphIds.put(subGraphId, arrayList);
            nodeSubgraphId.put(currentNode, subGraphId);

            while(unprocessedNodes.containsKey(fromTo.get(currentNode))){
                currentNode = fromTo.get(currentNode);
                unprocessedNodes.remove(currentNode);
                subGraphIds.get(subGraphId).add(currentNode);
                nodeSubgraphId.put(currentNode, subGraphId);
            }
            subGraphId++;
        }

        int[] newArray = new int[n];
        for(int i=0; i<n; i++){
            subGraphId = nodeSubgraphId.get(i);
            int cycleSize = subGraphIds.get(subGraphId).size();
            int movesToNewArray = r % cycleSize;

            int newNode = i;
            for(int j=0; j<movesToNewArray; j++){
                newNode = fromTo.get(newNode);
            }

            newArray[newNode] = i;
        }

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

    }
}