답안 #118575

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
118575 2019-06-19T09:10:02 Z IOrtroiii Skyscraper (JOI16_skyscraper) C++14
100 / 100
144 ms 19468 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 105;
const int M = 1005;
const int mod = 1e9 + 7;

void add(int &x, int y) {
   x += y; if (x >= mod) x -= mod;
}

int mul(int x, int y) {
   return (long long) x * y % mod;
}

int a[N];
int f[N][N][M][2][2];

int main() {
   int n, m;
   scanf("%d %d", &n, &m);
   if (n == 1) {
      puts("1");
      return 0;
   }
   for (int i = 1; i <= n; ++i) {
      scanf("%d", a + i);
   }
   sort(a + 1, a + 1 + n);
   for (int i = n; i > 1; --i) {
      a[i] -= a[i - 1];
   }
   a[1] = 0;
   f[0][0][0][0][0] = 1;
   for (int i = 0; i < n; ++i) {
      for (int j = 0; j <= i; ++j) {
         for (int k = 0; k <= m; ++k) {
            for (int l = 0; l < 2; ++l) {
               for (int r = 0; r < 2; ++r) {
                  int ways = f[i][j][k][l][r];
                  if (!ways) continue;
                  int nk = k + a[i + 1] * (2 * j - l - r);
                  if (nk > m) continue;
                  // fix left
                  if (!l) {
                     int coef = j - r;
                     if (i == n - 1) coef += r;
                     add(f[i + 1][j][nk][1][r], mul(coef, ways));
                     add(f[i + 1][j + 1][nk][1][r], ways);
                  }
                  // fix right
                  if (!r) {
                     int coef = j - l;
                     if (i == n - 1) coef += l;
                     add(f[i + 1][j][nk][l][1], mul(coef, ways));
                     add(f[i + 1][j + 1][nk][l][1], ways);
                  }
                  // merge one (not fix)
                  add(f[i + 1][j][nk][l][r], mul(2 * j - l - r, ways));
                  // make new (now fix)
                  add(f[i + 1][j + 1][nk][l][r], ways);
                  // merge two
                  if (j > 1) {
                     int coef = (j - l - r) * (j - 1);
                     // left
                     if (i == n - 1) {
                        coef += (l && r);
                     }
                     add(f[i + 1][j - 1][nk][l][r], mul(coef, ways));
                  }
               }
            }
         }
      }
   }
   int ans = 0;
   for (int i = 0; i <= m; ++i) {
      add(ans, f[n][1][i][1][1]);
   }
   printf("%d\n", ans);
}

Compilation message

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:22:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf("%d %d", &n, &m);
    ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:28:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
       scanf("%d", a + i);
       ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 768 KB Output is correct
2 Correct 3 ms 716 KB Output is correct
3 Correct 2 ms 640 KB Output is correct
4 Correct 2 ms 640 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 3 ms 768 KB Output is correct
9 Correct 3 ms 768 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 256 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 512 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 3 ms 512 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 512 KB Output is correct
8 Correct 2 ms 512 KB Output is correct
9 Correct 3 ms 640 KB Output is correct
10 Correct 2 ms 512 KB Output is correct
11 Correct 3 ms 768 KB Output is correct
12 Correct 3 ms 716 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 640 KB Output is correct
17 Correct 2 ms 512 KB Output is correct
18 Correct 3 ms 768 KB Output is correct
19 Correct 3 ms 768 KB Output is correct
20 Correct 2 ms 640 KB Output is correct
21 Correct 4 ms 1280 KB Output is correct
22 Correct 144 ms 19468 KB Output is correct
23 Correct 93 ms 7416 KB Output is correct
24 Correct 96 ms 10572 KB Output is correct
25 Correct 99 ms 8312 KB Output is correct
26 Correct 86 ms 7772 KB Output is correct
27 Correct 48 ms 9080 KB Output is correct
28 Correct 63 ms 10836 KB Output is correct
29 Correct 104 ms 14044 KB Output is correct
30 Correct 99 ms 8416 KB Output is correct