15.1.1
demo code
1 #include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <string> 5 #include <vector> 6 #include <cassert> 7 #include <cmath> 8 #include <algorithm> 9 using namespace std; 10 11 #define MAX 10100 12 13 int l[2][MAX] = {{1, 2, 1, 1, 2}, {1, 2, 1, 2, 2}}; 14 int l_star = 1; 15 16 void print_stations(int n) 17 { 18 int i = l_star; 19 printf("line %d: station %d\n", i, n); 20 for (int j = n - 2; j >= 0; j--) 21 { 22 //printf("%d\n", i - 1); 23 printf("line %d: station %d\n", l[i - 1][j], j + 1); 24 i = l[i - 1][j]; 25 } 26 } 27 28 int print_reversed(int n, int key) 29 { 30 if (n == 1) 31 return 0; 32 33 int new_key = l[key - 1][n - 2]; 34 print_reversed(n - 1, new_key); 35 printf("line %d: station %d\n", new_key, n - 1); 36 return 0; 37 } 38 int main(int argc, const char * argv[]) 39 { 40 print_stations(6); 41 cout << endl; 42 print_reversed(6, l_star); 43 printf("line %d: station %d\n", l_star, 6); 44 return 0; 45 }
15.1.2
按照树形画出来,然后总结计算就可以了。
15.1.3
求和即可。
15.1.4
根据FASTEST-WAY代码,对于f[i][j],只要一组旧值,一组新值(共4个)就可以了。 而l[i][j]不能缩减,因此总数是4+2n-2=2n+2
另外,根据15.3的说法,可以直接根据f[i][j]从后向前推,实际上只要O(n)的时间就可以完成。因此l[i][j]可以不要。
15.1.5
列出两个式子,相加会导致t1+t2<0,矛盾。