-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquestion45.c
97 lines (81 loc) · 1.47 KB
/
question45.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
93
94
95
96
97
/*
http://practice.geeksforgeeks.org/problems/activity-selection/0
*/
#include <stdio.h>
#include <stdlib.h>
struct pair{
int a;
int b;
};
int cmpfnc(const void *a, const void *b){
const struct pair *p1 = a;
const struct pair *p2 = b;
if(p1->b > p2->b){
return 1;
}else if(p1->b < p2->b){
return -1;
}else{
return 0;
}
}
int maxAct(int *start, int *end, int size){
int i;
struct pair ref[size];
for(i=0;i<size;i++){
ref[i].a = start[i];
ref[i].b = end[i];
}
int j;
qsort(ref, size, sizeof(struct pair), cmpfnc);
// printf("-------------\n");
// for(i=0;i<size;i++){
// printf("(%d, %d) ", ref[i].a, ref[i].b);
// }
// printf("\n");
int lis[size];
for(i=0;i<size;i++){
lis[i] = 1;
}
int max = 1;
for(i=1;i<size;i++){
for(j=i-1;j>=0;j--){
if(ref[i].a > ref[j].b){
if(lis[i] <= lis[j] + 1){
lis[i] = lis[j] + lis[i];
}
if(max < lis[i]){
max = lis[i];
}
// lis[i] = findMax(lis[i],1 + lis[j]);
// printf("updating value of lis[%d] to %d\n", i, lis[i]);
// max = findMax(lis[i], max);
}
}
}
// printf("-------------\n");
// for(i=0;i<size;i++){
// printf("%d ", lis[i]);
// }
// printf("\n");
return max;
}
int main(){
int cases;
scanf("%d",&cases);
int i;
for(i=0;i<cases;i++){
int n;
scanf("%d",&n);
int start[n];
int end[n];
int j;
for(j=0;j<n;j++){
scanf("%d",&start[j]);
}
for(j=0;j<n;j++){
scanf("%d",&end[j]);
}
printf("%d\n", maxAct(start,end, n));
}
return 0;
}