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

Envío 6289

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 22:25:35 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.181 s 14 KBi
#2
Correcto
0.095 s 16 KBi
#3
Correcto
0.094 s 17 KBi
#4
Correcto
0.106 s 17 KBi
#5
Correcto
0.105 s 17 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.736 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.742 s 125 KBi
#8
Correcto
0.165 s 14 KBi
#9
Correcto
0.127 s 17 KBi
#10
Correcto
0.087 s 17 KBi
#11
Correcto
0.094 s 16 KBi
#12
Correcto
0.892 s 123 KBi
#13
Correcto
0.782 s 118 KBi
#14
Tiempo límite excedido
1.041 s 51 KBi
#15
Correcto
0.804 s 66 KBi
#16
Tiempo límite excedido
1.096 s 51 KBi
#17
Correcto
0.094 s 16 KBi
#18
Correcto
0.097 s 17 KBi
#19
Correcto
0.125 s 53 KBi
#20
Correcto
0.089 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.777 s 125 KBi
#22
Tiempo límite excedido
1.046 s 125 KBi
#23
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.897 s 125 KBi
#24
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.644 s 125 KBi
#25
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.783 s 125 KBi
Puntos totales: 64 / 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);
        long n = scanner.nextLong();
        long r = scanner.nextLong();

        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++;
        }

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

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

            //newArray[newNode] = i;
            newArray.put(newNode, i);
        }

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

    }
}