-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlongest_increasing_subsequence_1.c
93 lines (66 loc) · 1.31 KB
/
longest_increasing_subsequence_1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
void out(int *a, int n){
for(int i=0; i<n; i++){
printf("%d ", a[i]);
}
printf("\n");
}
int check(int *a, int k, int n){
for(int i=k+1; i<n; i++){
if(a[k] > a[i]){
return 0;
}
}
return 1;
}
int max_poz(int *L, int n){
int max = 0, poz = -1;
for(int i=0; i<n; i++){
if(L[i] >= max){
max = L[i];
poz = i;
}
}
return poz;
}
void max_l(int *a, int *L, int n){
L[n-1] = 1;
for(int k=n-2; k>=0; k--){
if(check(a, k, n)){
L[k] = 1 + L[max_poz(L, n)];
}
else{
L[k] = 1;
}
}
}
void drum(int *a, int *L, int n){
int poz = max_poz(L, n), m = L[poz];
printf("Sirul strict crescator este: %d ", a[poz]);
m--;
for(int i=poz+1; i<n; i++){
if(L[i] == m){
printf("%d ", a[i]);
m--;
}
}
printf("\n");
}
int main(){
FILE *f;
int n;
f = fopen("fis.in", "r");
fscanf(f, "%d", &n);
int *a = malloc(sizeof(int)*n), *L = malloc(sizeof(int)*n);
for(int i=0; i<n; i++){
fscanf(f, "%d", &a[i]);
L[i] = 0;
}
fclose(f);
out(a, n);
max_l(a, L, n);
out(L, n);
drum(a, L, n);
return 0;
}