DP。
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 5 #define MAXNUM 1005 6 7 typedef struct { 8 int w, s; 9 int index; 10 } mouse_st; 11 12 mouse_st mouses[MAXNUM]; 13 int dp[MAXNUM], next[MAXNUM]; 14 15 int comp(const void *a, const void *b) { 16 mouse_st *p = (mouse_st *)a; 17 mouse_st *q = (mouse_st *)b; 18 if (p->w == q->w) 19 return p->s - q->s; 20 else 21 return q->w - p->w; 22 } 23 24 int main() { 25 int n=0, m, beg; 26 int i, j, max, index; 27 28 while (scanf("%d %d", &mouses[n].w, &mouses[n].s) != EOF) { 29 mouses[n].index = n; 30 ++n; 31 } 32 33 memset(dp, 0, sizeof(dp)); 34 qsort(mouses, n, sizeof(mouse_st), comp); 35 for (i=0; i<n; ++i) 36 next[i] = i; 37 m = 0; 38 39 for (i=0; i<n; ++i) { 40 max = 0; 41 for (j=0; j<i; ++j) { 42 if (mouses[j].w>mouses[i].w && mouses[j].s<mouses[i].s) { 43 if (max < dp[j]) { 44 max = dp[j]; 45 index = mouses[j].index; 46 } 47 } 48 } 49 if (max) 50 next[mouses[i].index] = index; 51 dp[i] = max + 1; 52 if (m < dp[i]) { 53 m = dp[i]; 54 beg = mouses[i].index; 55 } 56 } 57 if (m == 1) 58 printf("1 1 "); 59 else { 60 printf("%d ", m); 61 while (next[beg] != beg) { 62 printf("%d ", beg+1); 63 beg = next[beg]; 64 } 65 printf("%d ", beg+1); 66 } 67 68 return 0; 69 }