forked from matthewsamuel95/ACM-ICPC-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LIS.c
62 lines (58 loc) · 1.18 KB
/
LIS.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node* next;
}node;
node* append(node* head,int d){
node *temp=NULL;
temp=(node *)malloc(sizeof(node));
temp->data=d;
temp->next=NULL;
if(head==NULL){
head=temp;
return head;
}
temp->next=head;
return temp;
}
void printReverse(node* head){
if (head->next==NULL)
return;
printReverse(head->next);
printf("%d ", head->data);
}
int main(void) {
int n,i,j;
scanf("%d",&n);
int a[n],lis[n];
for(i=0;i<n;i++){
scanf("%d",&a[i]);
lis[i]=1;
}
node *head[n];
for(i=0;i<n;i++){
head[i]=NULL;
head[i]=(node *)malloc(sizeof(node));
head[i]=append(head[i],a[i]);
}
for(i=1;i<n;i++){
for(j=0;j<i;j++){
if(a[i]>a[j] && lis[i]<lis[j]+1){
lis[i]=lis[j]+1;
head[i]=append(head[j],a[i]);
}
}
}
int max=0,index=0;
for (i=0;i<n;i++){
if (max<lis[i]){
max=lis[i];
index=i;
}
}
// printf("%d %d\n",max,index);
printReverse(head[index]);
printf("\n");
return 0;
}