dengxinyi 6 лет назад
Родитель
Сommit
269f3a6f07
2 измененных файлов с 98 добавлено и 0 удалено
  1. 45 0
      bailian/1088.ski/main.cc
  2. 53 0
      poj/1390.blocks/main.cc

+ 45 - 0
bailian/1088.ski/main.cc

@@ -0,0 +1,45 @@
+#include <algorithm>
+#include <cstdio>
+
+using namespace std;
+
+const int dx[4] = {0, 1, 0, -1};
+const int dy[4] = {1, 0, -1, 0};
+
+const int N = 100;
+int h[N][N], dp[N][N];
+
+struct Grid {
+  int r;
+  int c;
+  int h;
+  bool operator<(const Grid that) const { return this->h < that.h; }
+};
+
+Grid area[N * N];
+
+int main() {
+  int R, C;
+  scanf("%d %d", &R, &C);
+  for (int i = 0, k = 0; i < R; i++) {
+    for (int j = 0; j < C; j++) {
+      scanf("%d", &h[i][j]);
+      dp[i][j] = 1;
+      area[k++] = {i, j, h[i][j]};
+    }
+  }
+  sort(area, area + R * C);
+  int len = 1;
+  for (int i = 0; i < R * C; i++) {
+    Grid g = area[i];
+    for (int j = 0; j < 4; j++) {
+      int nx = g.r + dx[j], ny = g.c + dy[j];
+      if (0 <= nx && nx < R && 0 <= ny && ny < C && g.h < h[nx][ny]) {
+        dp[nx][ny] = max(dp[nx][ny], dp[g.r][g.c] + 1);
+        len = max(len, dp[nx][ny]);
+      }
+    }
+  }
+  printf("%d\n", len);
+  return 0;
+}

+ 53 - 0
poj/1390.blocks/main.cc

@@ -0,0 +1,53 @@
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+
+using namespace std;
+
+const int N = 200;
+int dp[N][N][N];
+// dp[i][j][k] means the highest score for clearing blocks i ~ j.
+
+struct Segment {
+  int color;
+  int len;
+};
+
+Segment seg[N];
+
+// Clear the blocks from i to j, with extra len of block having the same color as seg[j].
+int clear(int i, int j, int len) {
+  if (dp[i][j][len] != -1) return dp[i][j][len];
+  int score = (seg[j].len + len) * (seg[j].len + len);
+  if (i == j) return score;
+  score += clear(i, j - 1, 0);
+  for (int k = i; k + 1 <= j - 1; k++) {
+    if (seg[k].color != seg[j].color) continue;
+    score = max(score, clear(k + 1, j - 1, 0) + clear(i, k, seg[j].len + len));
+  }
+  dp[i][j][len] = score;
+  return score;
+}
+
+int main() {
+  int T, n;
+  scanf("%d", &T);
+  for (int t = 1; t <= T; t++) {
+    scanf("%d", &n);
+    int cnt = -1, pre = -1;
+    for (int i = 0; i < n; i++) {
+      int color;
+      scanf("%d", &color);
+      if (color != pre) {
+        seg[++cnt].color = color;
+        seg[cnt].len = 1;
+        pre = color;
+      } else {
+        seg[cnt].len++;
+      }
+    }
+    memset(dp, 0xFF, sizeof(dp));
+    printf("Case %d: %d\n", t, clear(0, cnt, 0));
+  }
+  return 0;
+}