标签:
1 10 1 2 2 4 5 8 6 10 7 9 3 1 5 8 12 10 9 7 2 2
5
1 //C v0.1 WA! 2 #include<stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 struct rectangle { 6 int length; 7 int width; 8 } rctg[1000]; 9 10 11 //order by length asc ,or order by width asc 12 int compare(const void *a, const void *b) 13 { 14 struct rectangle *rct1 = (struct rectangle *)a; 15 struct rectangle *rct2 = (struct rectangle *)b; 16 if (rct1->length == rct2->length) 17 return (rct1->width - rct2->width); 18 return (rct1->length - rct2->length); 19 } 20 21 int main(void) 22 { 23 int i; 24 int n; 25 26 scanf("%d", &n); 27 getchar(); 28 29 while (n--) { 30 int m; 31 scanf("%d", &m); 32 getchar(); 33 34 memset(&rctg, 0x00, sizeof(rctg)); 35 36 for (i = 0; i < m; i++) { 37 scanf("%d%d", &rctg[i].length, &rctg[i].width); 38 if (rctg[i].length < rctg[i].width) { 39 int tmp; 40 tmp = rctg[i].length; 41 rctg[i].length = rctg[i].width; 42 rctg[i].width = tmp; 43 } 44 } 45 46 qsort(rctg, m, sizeof(rctg[0]), compare); 47 printf("after asc sort\n"); 48 49 for (i = 1; i < m; i++) { 50 printf("%d %d\n", rctg[i].length, rctg[i].width); 51 } 52 53 int sum = 1; 54 int idx = 0; 55 for (i = 1; i < m; i++) { 56 //printf("%d %d\n", rctg[i].length, rctg[i].width); 57 if (rctg[idx].length < rctg[i].length && rctg[idx].width < rctg[i].width) { 58 ++sum; 59 idx = i; 60 } else { 61 continue; 62 } 63 } 64 65 printf("%d\n", sum); 66 } 67 return 0; 68 }
1 //C v0.1 AC 2 #include<stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <alloca.h> 6 struct rectangle { 7 int length; 8 int width; 9 } rctg[1000]; 10 11 int dplis(struct rectangle rctg[], int n); 12 //order by length asc ,or order by width asc 13 int compare(const void *a, const void *b) 14 { 15 struct rectangle *rct1 = (struct rectangle *)a; 16 struct rectangle *rct2 = (struct rectangle *)b; 17 if (rct1->length == rct2->length) 18 return (rct1->width - rct2->width); 19 return (rct1->length - rct2->length); 20 } 21 22 int main(void) 23 { 24 int i; 25 int n; 26 27 scanf("%d", &n); 28 getchar(); 29 30 while (n--) { 31 int m; 32 scanf("%d", &m); 33 getchar(); 34 35 memset(&rctg, 0x00, sizeof(rctg)); 36 37 for (i = 0; i < m; i++) { 38 scanf("%d%d", &rctg[i].length, &rctg[i].width); 39 if (rctg[i].length < rctg[i].width) { 40 int tmp; 41 tmp = rctg[i].length; 42 rctg[i].length = rctg[i].width; 43 rctg[i].width = tmp; 44 } 45 } 46 47 qsort(rctg, m, sizeof(rctg[0]), compare); 48 49 int rctgmax = dplis(rctg, m); 50 51 printf("%d\n", rctgmax); 52 } 53 return 0; 54 } 55 56 int dplis(struct rectangle rctg[], int n) 57 { 58 int *d = (int *)alloca(sizeof(int) * (n)); 59 int len = 1; 60 int i; 61 int j; 62 for (i = 0; i < n; ++i) { 63 d[i] = 1; 64 for (j = 0; j < i; ++j) { 65 if (rctg[j].width < rctg[i].width && rctg[j].length < rctg[i].length && d[j] + 1 > d[i]) { 66 d[i] = d[j] + 1; 67 } 68 } 69 70 if (d[i] > len) { 71 len = d[i]; 72 } 73 74 } 75 76 return len; 77 }
标签:
原文地址:http://www.cnblogs.com/guxuanqing/p/5658233.html