0% found this document useful (0 votes)
49 views

Int Int Int Int Int Int For: #Include #Include #Include

The document defines a function Max_min_Grouping that takes an array A, number of elements N, number of groups M, and output array G. It fills the output array G with the optimal group sizes such that the maximum sum of elements within each group is minimized. It uses dynamic programming to solve the problem in O(N*M) time. The main function tests it on a sample input array and prints the results.

Uploaded by

Yifan Bao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Int Int Int Int Int Int For: #Include #Include #Include

The document defines a function Max_min_Grouping that takes an array A, number of elements N, number of groups M, and output array G. It fills the output array G with the optimal group sizes such that the maximum sum of elements within each group is minimized. It uses dynamic programming to solve the problem in O(N*M) time. The main function tests it on a sample input array and prints the results.

Uploaded by

Yifan Bao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

#include<limits>
#include <windows.h>

5 int sum(int a[], int l, int h){


int s = 0;
int i;
for (i = l; i <= h; i++){
s += a[i-1];
10 }
return s;
}

int Max_min_Grouping(int A[], int N, int M, int G[]){


15 int i,j,k;
int s;
int c[M+1][N+1];
int B[M+1][N+1];
int Max_B_min;
20 int num_temp = N;
for (j = 0; j <= M; j++){
for (i = 0; i <= N; i++){
c[j][i] = INT32_MAX;
}
25 }
for (j = 0; j <= M; j++){
for (i = 0; i <= N; i++){
B[j][i] = 0;
}
30 }
for (j = 1; j <= M; j++){
for (i=1; i<=N; i++){
if (i>=j){
Max_B_min = 0;
35 for (k = j-1; k <= i-1; k++){
s = sum(A, k+1, i);
if (c[j-1][k] <= s && c[j-1][k] > Max_B_min){
Max_B_min = c[j-1][k];
B[j][i] = k;
40 }
else if (c[j-1][k] > s && s > Max_B_min)
{
Max_B_min = s;
B[j][i] = k;
}
5 c[j][i] = Max_B_min;
}
}
else
{
10 c[j][i] = 0;
}
}
}
// for (j=1; j<=M; j++){
15 // for (i=1; i<=N; i++){
// std::cout << c[j][i] << '\t';
// }
// std::cout << std::endl;
// }
20 // for (j=1; j<=M; j++){
// for (i=1; i<=N; i++){
// std::cout << B[j][i] << '\t';
// }
// std::cout << std::endl;
25 // }
for (i=M; i>0; i--){
G[i-1] = num_temp - B[i][num_temp];
num_temp -= G[i-1];
}
30 return 0;
}

int main(){
int A[] = {3,9,7,8,2,6,5,10,1,7,6,4};
35 int M = 3;
int N = sizeof(A)/sizeof(A[0]);
int G[N];
LARGE_INTEGER nFreq;
LARGE_INTEGER nBeginTime;
40 LARGE_INTEGER nEndTime;
QueryPerformanceFrequency(&nFreq); // get the frequency of the counter
double t;
QueryPerformanceCounter(&nBeginTime);
Max_min_Grouping(A, N, M, G);
QueryPerformanceCounter(&nEndTime);
5 t = (double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;
for (int i=0; i<M; i++){
std::cout << G[i] << '\t';
}
std::cout << std::endl;
10 std::cout << "Running time: " << t << " s" << std::endl;
}

You might also like