nord vpnnord vpn
Ad

minimal big sum

mail@pastecode.io avatar
unknown
plain_text
5 months ago
3.5 kB
1
Indexable
Never
Minimal Big Sum
Suppose 'A' is a non empty zero indexed array of 'N' integers and 'K' is another Integer.
Array 'A' needs to be divided into 'K' blocks of consecutive elements.  

Size of the block is any integer such that , 0 <= size of block <= N. Every element of the array should belong to some block. 
The sum of the block from X to Y equals A[X] + A[X + 1] + ... + A[Y]. The sum of empty block equals 0.
The big sum is defined as the maximal sum of any block.
 
For example, you are given integers 'K' = 3 and array A such that:
  A[0] = 2
  A[1] = 1
  A[2] = 5
  A[3] = 1
  A[4] = 2
  A[5] = 2
  A[6] = 2 
The array can be divided, for example, into the following blocks: 
        [2, 1, 5, 1, 2, 2, 2], [], [] with a big sum of 15;
        [2], [1, 5, 1, 2], [2, 2] with a big sum of 9;
        [2, 1, 5], [], [1, 2, 2, 2] with a big sum of 8;
        [2, 1], [5, 1], [2, 2, 2] with a big sum of 6.
 
The goal is to minimize the big sum. In the above example, 6 is the minimal big sum. 
 
Given integer K and a non-empty zero-indexed array A consisting of N integers, returns the minimal big sum. 
 
For example, given K = 3 and array A such that:
  A[0] = 2
  A[1] = 1
  A[2] = 5
  A[3] = 1
  A[4] = 2
  A[5] = 2
  A[6] = 2 
the function should return 6, as explained above.
 
Assume that: 
        N and K are integers within the range [1..100,000];
        each element of array A is an integer within the range [0..M]. 
Input :
First Line : T : Number of test cases
Second Line : K : Number of Blocks.
Third Line : N : Number of elements in the input array
Fourth Line : Elements of array separated by space.
Fifth Line : next test case follows 
 
Output
#Test_Case minimized_big_sum 
 
Sample Input
2
3
7
2 1 5 1 2 2 2
4
10
10 2 1 5 1 2 2 2 9 11 
 
Sample Output  
#1 6
#2 12


10
3
7
2 1 5 1 2 2 2
4
10
10 2 1 5 1 2 2 2 9 11
10
10
0 0 0 0 0 0 0 0 0 0
1
10
10 10 10 10 10 10 10 10 10 10
1
1
0
1
10
1 2 3 4 5 6 7 8 9 10
10
10
10 9 8 7 6 5 4 3 2 1
9
10
1 2 3 4 5 6 7 8 9 10
3
10
20 19 18 17 16 15 14 13 12 11
14
100
1 2 3 4 5 6 7 8 9 0 11 22 33 44 55 66 77 88 99 12 12 13 14 15 16 17 18 19 10 0 9 12 12 34 23 25 27 28 29 10 10 1 2 3 4 5 6 7 8 9 0 99 12 12 13 14 15 16 17 18 19 77 88 99 12 12 13 14 15 16 17 29 10 10 1 2 3 4 5 6 7 12 13 14 15 16 17 18 19 20 23 24 25 26 2 3 41 8 0 10


package day18_2206;

import java.util.Scanner;

public class minimal_big_sum {
	
	static int k, n, ans, total, max;
	static int[] numbers, blocks;
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
		int testcase = sc.nextInt();
		for(int tc = 1; tc <= testcase; tc++){
//			k = sc.nextInt() + 1;
			k = sc.nextInt();
			n = sc.nextInt();
//			blocks = new int[k];
			
			total = 0; max = 0;
			numbers = new int[n];
			for(int i = 0; i < n; i++){
				numbers[i] = sc.nextInt();
				total += numbers[i];
				max = max < numbers[i] ? numbers[i] : max;
			}
			ans = total;
//			sinh(1, 0);
			
			for(int i = max; i <= total; i++){
				int sum = 0, cnt = 1;
				for(int j = 0; j < n; j++){
					if(sum + numbers[j] > i){
						sum = numbers[j];
						cnt++;
					}
					else{
						sum += numbers[j];
					}
				}
				
				if(cnt <= k){
					ans = i;
					break;
				}
			}
			
			System.out.println("#" + tc + " " + ans);
		}
		
	}

}

nord vpnnord vpn
Ad