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

Envío 4367

Problema 0x25 - Suma de un subarreglo grande

  • Autor: saris123
  • Fecha: 2021-06-13 14:25:13 UTC (Hace casi 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.133 s 15 KBi
#2
Correcto
0.144 s 17 KBi
#3
Correcto
0.14 s 17 KBi
#4
Correcto
0.188 s 15 KBi
#5
Correcto
0.138 s 17 KBi
#6
Correcto
0.159 s 14 KBi
#7
Correcto
0.15 s 15 KBi
#8
Tiempo límite excedido
1.106 s 35 KBi
#9
Tiempo límite excedido
1.04 s 57 KBi
#10
Tiempo límite excedido
1.171 s 58 KBi
#11
Tiempo límite excedido
1.07 s 53 KBi
#12
Tiempo límite excedido
1.012 s 48 KBi
#13
Tiempo límite excedido
1.116 s 98 KBi
#14
Tiempo límite excedido
1.071 s 45 KBi
Puntos totales: 50 / 100

Código

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
    	Scanner sc= new Scanner(System.in);
        int N = sc.nextInt();
        int numberArray[] = new int[N];
        int totalSum = 0;
        for(int i = 0; i < N; i++)
        {
            numberArray[i] = sc.nextInt();
            totalSum += numberArray[i];
        }
        
        int C = sc.nextInt();
        int resultArray[] = new int[C];
        for(int i = 0; i < C; i++)
        {
        	int p = sc.nextInt();
        	int q = sc.nextInt();
        	int size = p - q;
        	
        	if (size == N) {
        		resultArray[i] = totalSum;
        	}
        	
        	if (size < N/2) {
        		for (int j = p; j <= q; j++) {
        			resultArray[i] += numberArray[j] ;
                }
        	} else {
            	int inicialTail = 0;
            	int finalTail = 0;
        		for (int j = 0; j < p; j++) {
                    inicialTail += numberArray[j] ;
                }
        		for (int j = q +1 ; j < N; j++) {
                    finalTail += numberArray[j] ;
                }
        		resultArray[i] = totalSum - inicialTail - finalTail;
        	}
        }
        sc.close();
        
        Arrays.stream(resultArray).forEach(System.out::println);
    }
}