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

Envío 2776

Problema 0x25 - Suma de un subarreglo grande

  • Autor: hamorillo
  • Fecha: 2021-02-04 15:21:48 UTC (Hace casi 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.164 s 16 KBi
#2
Correcto
0.156 s 15 KBi
#3
Correcto
0.173 s 15 KBi
#4
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
	at Main.main(Main.java:22)
0.199 s 12 KBi
#5
Correcto
0.187 s 12 KBi
#6
Incorrecto
0.205 s 16 KBi
#7
Incorrecto
0.23 s 13 KBi
#8
Incorrecto
0.791 s 35 KBi
#9
Tiempo límite excedido
1.178 s 114 KBi
#10
Tiempo límite excedido
1.166 s 114 KBi
#11
Tiempo límite excedido
1.281 s 119 KBi
#12
Tiempo límite excedido
1.164 s 114 KBi
#13
Tiempo límite excedido
1.136 s 115 KBi
#14
Tiempo límite excedido
1.065 s 113 KBi
Puntos totales: 29 / 100

Código

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Data data = readNumsFromStdin();

        int[] results = new int[data.totalRequest];

        int totalSum = calculateTotalSum(data);
        int splitRange = data.totalNumbers / 2;

        for (int j = 0; j < data.totalRequest; j++) {
            if (data.request[j].end - data.request[j].init > splitRange) {
                results[j] = totalSum;
                if (data.request[j].init != 0) {
                    for (int i = 0; i <= data.request[j].init; i++) {
                        results[j] -= data.numbers[i];
                    }
                }
                if (data.request[j].end != data.totalNumbers - 1) {
                    for (int i = data.request[j].end; i < data.totalRequest; i++) {
                        results[j] -= data.numbers[i];
                    }
                }
            } else {
                for (int i = data.request[j].init; i <= data.request[j].end; i++) {
                    results[j] += data.numbers[i];
                }
            }
        }

        for (int result : results) {
            System.out.println(result);
        }
    }

    private static int calculateTotalSum(Data data) {
        int totalSum = 0;
        for (int i = 0; i < data.totalNumbers; i++) {
            totalSum += data.numbers[i];
        }

        return totalSum;
    }

    private static Data readNumsFromStdin() {
        Data data = new Data();
        Scanner in = new Scanner(System.in);

        data.totalNumbers = in.nextInt();
        data.numbers = new int[data.totalNumbers];
        readArrayFromStdin(data.numbers, data.totalNumbers, in);

        data.totalRequest = in.nextInt();
        data.request = new Request[data.totalRequest];
        readRequestArrayFromStdin(data.request, data.totalRequest, in);
        return data;
    }

    private static void readArrayFromStdin(int[] array, int count, Scanner in) {
        for (int i = 0; i < count; i++) {
            array[i] = in.nextInt();
        }
    }

    private static void readRequestArrayFromStdin(Request[] array, int count, Scanner in) {
        for (int i = 0; i < count; i++) {
            array[i] = new Request(in.nextInt(), in.nextInt());
        }
    }

    private static class Data {
        int totalNumbers;
        int[] numbers;
        int totalRequest;
        Request[] request;
    }

    private static class Request {
        int init;
        int end;

        public Request(int init, int end) {
            this.init = init;
            this.end = end;
        }
    }
}