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

Envío 2861

Problema 0x25 - Suma de un subarreglo grande

  • Autor: pradomaricon
  • Fecha: 2021-02-07 05:13:01 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.166 s 15 KBi
#2
Correcto
0.156 s 16 KBi
#3
Correcto
0.174 s 15 KBi
#4
Correcto
0.173 s 15 KBi
#5
Correcto
0.154 s 15 KBi
#6
Correcto
0.185 s 16 KBi
#7
Correcto
0.18 s 16 KBi
#8
Tiempo límite excedido
1.015 s 52 KBi
#9
Tiempo límite excedido
1.08 s 34 KBi
#10
Tiempo límite excedido
1.115 s 83 KBi
#11
Tiempo límite excedido
1.111 s 85 KBi
#12
Tiempo límite excedido
1.01 s 82 KBi
#13
Tiempo límite excedido
1.008 s 34 KBi
#14
Tiempo límite excedido
1.112 s 86 KBi
Puntos totales: 50 / 100

Código

import java.util.Scanner;

public class Main {
    
    public static Scanner leer = new Scanner(System.in);
    public static void main(String[] args) {

        //input dimensiones
        String dimensiones = leer.nextLine().trim();
        int filas = Integer.parseInt(dimensiones);
       
        int[] ejemplo1 = new int[filas];
        
        
        //input de datos
        
            String[] filaDada = leer.nextLine().trim().split(" ");
            int k = 0;
            for (int j = 0; j < filas; j++) {
                ejemplo1[j] = Integer.parseInt(filaDada[k]);
                k++;
                //System.out.println(ejemplo1[i][j]);
            }
            
        //crear matriz con suma acumulada
        int[] matrizAcumulada = new int[filas];
        for (int i = 0; i < filas; i++) {
            if(i==0){
                matrizAcumulada[i]=ejemplo1[i];
            }else{
                matrizAcumulada[i]=matrizAcumulada[i-1]+ejemplo1[i]; 
            }
            
            
        }
            
        

        //imput consultas
        String numc = leer.nextLine().trim();
        int numConsultas=Integer.parseInt(numc);
        
        for (int i = 0; i < numConsultas; i++) {
            String[] cons = leer.nextLine().trim().split(" ");
            int comienzo=Integer.parseInt(cons[0]);
            int finaliza=Integer.parseInt(cons[1]);
            
            int result=sumaSubarreglo(matrizAcumulada,comienzo, finaliza);
            
            System.out.println(result);
            
        }
 
    }
    
    
    public static int sumaSubarreglo(int[] matrizAcumulada, int comienzo, int finaliza){
        int toret=0;
        if(comienzo==0){
            toret=matrizAcumulada[finaliza];
        }else{
            toret=matrizAcumulada[finaliza]-matrizAcumulada[comienzo-1];
        }
        return toret;
}

    }