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

Envío 6290

Problema 0x99 - Máquina para barajar cartas

  • Autor: rpedrazacoello
  • Fecha: 2022-05-28 22:33:32 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.102 s 17 KBi
#2
Correcto
0.107 s 17 KBi
#3
Correcto
0.125 s 18 KBi
#4
Correcto
0.094 s 17 KBi
#5
Correcto
0.1 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.654 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.71 s 125 KBi
#8
Correcto
0.098 s 16 KBi
#9
Correcto
0.12 s 17 KBi
#10
Correcto
0.097 s 17 KBi
#11
Correcto
0.109 s 17 KBi
#12
Correcto
0.728 s 112 KBi
#13
Correcto
0.913 s 122 KBi
#14
Correcto
0.648 s 56 KBi
#15
Correcto
0.668 s 56 KBi
#16
Correcto
0.765 s 65 KBi
#17
Correcto
0.131 s 18 KBi
#18
Correcto
0.1 s 16 KBi
#19
Correcto
0.092 s 17 KBi
#20
Correcto
0.103 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.907 s 125 KBi
#22
Tiempo límite excedido
1.05 s 60 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.899 s 125 KBi
#24
Tiempo límite excedido
1.118 s 107 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.779 s 125 KBi
Puntos totales: 72 / 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, Integer> subGraphIdsCount = new HashMap<Integer, 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);
            subGraphIdsCount.put(subGraphId, 1);
            nodeSubgraphId.put(currentNode, subGraphId);

            while(unprocessedNodes.containsKey(fromTo.get(currentNode))){
                currentNode = fromTo.get(currentNode);
                unprocessedNodes.remove(currentNode);
                Integer count = subGraphIdsCount.get(subGraphId) + 1;
                subGraphIdsCount.put(subGraphId, count);
                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 = subGraphIdsCount.get(subGraphId);
            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) +" ");
        }
    }
}