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

Envío 6287

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 22:22:51 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Incorrecto
0.088 s 16 KBi
#2
Incorrecto
0.095 s 16 KBi
#3
Incorrecto
0.09 s 16 KBi
#4
Incorrecto
0.091 s 16 KBi
#5
Correcto
0.104 s 17 KBi
#6
Tiempo límite excedido
1.078 s 125 KBi
#7
Tiempo límite excedido
1.039 s 125 KBi
#8
Incorrecto
0.097 s 16 KBi
#9
Incorrecto
0.088 s 16 KBi
#10
Incorrecto
0.094 s 16 KBi
#11
Incorrecto
0.097 s 17 KBi
#12
Tiempo límite excedido
1.097 s 94 KBi
#13
Incorrecto
0.979 s 120 KBi
#14
Incorrecto
0.832 s 61 KBi
#15
Incorrecto
0.88 s 67 KBi
#16
Incorrecto
0.887 s 67 KBi
#17
Incorrecto
0.091 s 16 KBi
#18
Incorrecto
0.099 s 16 KBi
#19
Incorrecto
0.092 s 16 KBi
#20
Incorrecto
0.094 s 17 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.873 s 125 KBi
#22
Error en tiempo de ejecución (NZEC)
Exited with error status 137
run: line 1:     3 Killed                  /usr/local/openjdk13/bin/java Main
0.881 s 125 KBi
#23
Tiempo límite excedido
1.063 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.716 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.807 s 125 KBi
Puntos totales: 4 / 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>();
        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.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) + " "));
        }

    }
}