dengxinyi %!s(int64=6) %!d(string=hai) anos
pai
achega
d917b26a04
Modificáronse 1 ficheiros con 21 adicións e 0 borrados
  1. 21 0
      poj/1163.the-triangle/main.cc

+ 21 - 0
poj/1163.the-triangle/main.cc

@@ -0,0 +1,21 @@
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+
+using namespace std;
+
+const int N = 100;
+
+int dp[N], tri[N][N];
+// dp[i] means the max sum from the ith col of curr row to the last row.
+
+int main() {
+  int n;
+  scanf("%d", &n);
+  for (int i = 0; i < n; i++)
+    for (int j = 0; j < i + 1; j++) scanf("%d", &tri[i][j]);
+  memcpy(dp, tri[n - 1], n * sizeof(int));
+  for (int i = n - 1; 0 < i; i--)
+    for (int j = 0; j < i; j++) dp[j] = tri[i - 1][j] + max(dp[j], dp[j + 1]);
+  printf("%d\n", dp[0]);
+}