Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruqi #2

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions 2DP.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <stdio.h>
#include <stdlib.h>
//#include <math.h>

void cumulative_sum(int *lst, int n, int *csum) {
csum[0] = 0;
for (int i = 1; i <= n; i++) {
csum[i] = csum[i - 1] + lst[i - 1];
}
}

void calculate_sump_matrix(int *lst, int n, int **result) {
int *csum = (int *)malloc((n + 1) * sizeof(int));
cumulative_sum(lst, n, csum);

for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
result[i][j] = csum[j + 1] - csum[i];
}
}

free(csum);
}

int DP(int n, int *list_p) {
int **R = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++) {
R[i] = (int *)calloc(n, sizeof(int));
}

for (int i = 0; i < n - 1; i++) {
R[i][i + 1] = list_p[i] < list_p[i + 1] ? list_p[i] : list_p[i + 1];
}

int **sump_matrix = (int **)malloc(n * sizeof(int *));
for (int i = 0; i < n; i++) {
sump_matrix[i] = (int *)malloc(n * sizeof(int));
}

calculate_sump_matrix(list_p, n, sump_matrix);

for (int offset = 2; offset < n; offset++) {
for (int i = 0; i < n - offset; i++) {
int j = i + offset;
int left_k = i;
int right_k = j - 1;

while (left_k < right_k - 1) {
int mid_k = (left_k + right_k) / 2;
int left_value = sump_matrix[i][mid_k] + R[i][mid_k];
int right_value = sump_matrix[mid_k + 1][j] + R[mid_k + 1][j];

if (left_value > right_value) {
right_k = mid_k;
} else {
left_k = mid_k;
}
}

int tmp1 = sump_matrix[left_k + 1][j] + R[left_k + 1][j];
if (sump_matrix[i][left_k] + R[i][left_k] < tmp1) {
tmp1 = sump_matrix[i][left_k] + R[i][left_k];
}

int tmp2 = sump_matrix[right_k + 1][j] + R[right_k + 1][j];
if (sump_matrix[i][right_k] + R[i][right_k] < tmp2) {
tmp2 = sump_matrix[i][right_k] + R[i][right_k];
}

R[i][j] = tmp1 > tmp2 ? tmp1 : tmp2;
}
}

int result = R[0][n - 1];

for (int i = 0; i < n; i++) {
free(R[i]);
free(sump_matrix[i]);
}
free(R);
free(sump_matrix);

return result;
}

int main() {
int n;
scanf("%d", &n);

int *list_p = (int *)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
scanf("%d", &list_p[i]);
}

printf("%d\n", DP(n, list_p));

free(list_p);
return 0;
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# GithubDemo
Learning how to use github for teams

Nov 22, 2024