-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dsap dynamic programming problems
- Loading branch information
1 parent
0fd0b91
commit 3be8757
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
dsap/p3-dynamic-programming/c3-dynamic-programming-problems/3.1.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <cstdio> | ||
#include <cmath> | ||
#define MIN -99999999 | ||
#define MAX 99999999 | ||
|
||
/* | ||
10 | ||
5 2 3 4 9 10 5 6 7 8 | ||
*/ | ||
int main(){ | ||
int n; | ||
scanf("%d", &n); | ||
int a[n + 2]; | ||
a[0] = MIN; | ||
a[n + 1] = MAX; | ||
for(int i = 1; i <= n; i++){ | ||
scanf("%d", &a[i]); | ||
} | ||
int t[n]; | ||
int L[n + 2]; | ||
L[n + 1] = 1; | ||
for(int i = n ; i >=0; i--){ | ||
int jmax = n + 1; | ||
for(int j = i + 1; j <= n + 1; j++){ | ||
if(a[j] > a[i] && L[j] > L[jmax]){ | ||
jmax = j; | ||
} | ||
} | ||
L[i] = L[jmax] + 1; | ||
t[i] = jmax; | ||
} | ||
int i = t[0]; | ||
printf("%d\n", L[0] - 2); | ||
while(i != (n + 1)){ | ||
printf("a[%d] = %d\n", i - 1, a[i]); | ||
i = t[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <cstdio> | ||
#include <iostream> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
#define S "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
using namespace std; | ||
|
||
void solve(char a[], bool b[], int n, int k, int i, int &c){ | ||
if(c < k){ | ||
for(int j = 0; j < n; j++){ | ||
if(b[j] == false){ | ||
a[i] = S[j]; | ||
if(i == n - 1){ | ||
c++; | ||
for(int ii = 0; ii < n; ii++){ | ||
printf("%c", a[ii]); | ||
} | ||
printf("\n"); | ||
}else{ | ||
b[j] = true; | ||
solve(a, b, n, k, i + 1, c); | ||
b[j] = false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
int main(){ | ||
int t, n, k; | ||
scanf("%d", &t); | ||
for(int i = 1; i <= t; i++){ | ||
scanf("%d %d", &n, &k); | ||
char a[n]; | ||
bool b[n]; | ||
int c = 0; | ||
memset(b, false, sizeof b); | ||
printf("Case %d:\n", i); | ||
solve(a, b, n, k, 0, c); | ||
} | ||
} |