HDU 1231 HDU 2084
先发两道水题,完全是凭着N多个月前积攒的经验写的,还有校赛的那道DP,我觉得出的蛮好~
HDU 1231最大子序列和,需要标记子序列开始和结束的位置
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<iostream> 4 using namespace std; 5 6 #include<math.h> 7 #include<algorithm> 8 9 #define range 10010 10 11 int a[range]; 12 13 int main() 14 { 15 int k; 16 while(scanf("%d", &k) != EOF && k!=0) 17 { 18 int flag=true; 19 for(int i=0; i<k; ++i) 20 { 21 scanf("%d", &a[i]); 22 if(a[i] >= 0) flag=false; 23 } 24 25 if(flag) 26 { 27 printf("0 %d %d\n", a[0], a[k-1]); 28 continue; 29 } 30 31 int s,e; 32 int ts, te; 33 float maxSum = -0.1; 34 int cur=0; 35 36 for(int i=0; i<k; ++i) 37 { 38 if(cur == 0) ts=i; 39 cur += a[i]; 40 if(cur < 0) cur=0; 41 else if(cur > maxSum) 42 { 43 maxSum = cur; 44 s = ts; 45 e = i; 46 //printf("%d %d\n", s, e); 47 } 48 } 49 50 int res = maxSum+0.5; 51 printf("%d %d %d\n", res, a[s], a[e]); 52 } 53 }
HDU 2084数塔问题,直接一个DP过去
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<iostream> 4 using namespace std; 5 6 #include<math.h> 7 #include<algorithm> 8 9 int ta[110][110]; 10 11 12 int main() 13 { 14 int test; scanf("%d", &test); 15 int n; 16 while(test--) 17 { 18 scanf("%d", &n); 19 for(int i=0; i<n; ++i) 20 for(int j=0; j<=i; ++j) 21 scanf("%d", &ta[i][j]); 22 23 for(int i=1; i<n; ++i) 24 { 25 for(int j=0; j<=i; ++j) 26 { 27 if(j==0) ta[i][j] += ta[i-1][j]; 28 else if(j==i) ta[i][j] += ta[i-1][j-1]; 29 else ta[i][j] += max(ta[i-1][j-1], ta[i-1][j]); 30 } 31 } 32 33 int result = -1; 34 for(int i=0; i<n; ++i) 35 result = max(ta[n-1][i], result); 36 37 printf("%d\n", result); 38 } 39 return 0; 40 }
杭电DP专题:http://acm.hdu.edu.cn/problemclass.php?id=516&page=1
To Be The Best Of Yourself
浙公网安备 33010602011771号