답안 #438684

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
438684 2021-06-28T13:00:12 Z peijar Skyscraper (JOI16_skyscraper) C++17
100 / 100
646 ms 316848 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int MOD = 1e9 + 7;
int lenPerm, maxSum;

int dp[100][1001][101][2][2];
// dp[curPos][curSum][ccCenter][fL][fR];
int diffs[101];

void iterative() {
  for (int curPos = lenPerm - 1; curPos >= 0; --curPos)
    for (int _curSum = 0; _curSum <= maxSum; ++_curSum)
      for (int ccCenter = 0; ccCenter <= lenPerm; ++ccCenter)
        for (int fL = 0; fL < 2; ++fL)
          for (int fR = 0; fR < 2; ++fR) {
            int &cur = dp[curPos][_curSum][ccCenter][fL][fR];
            cur = 0;

            int curSum = _curSum;
            curSum += fL * diffs[curPos];
            curSum += fR * diffs[curPos];
            curSum += ccCenter * diffs[curPos] * 2;
            if (curSum > maxSum)
              continue;

            if (curPos == lenPerm - 1) {
              if (ccCenter > 0 or (!fL and !fR))
                continue;
              cur = 1;
              continue;
            }

            // Create new cc :
            if (ccCenter < lenPerm)
              cur += dp[curPos + 1][curSum][ccCenter + 1][fL][fR];
            // Create new cc on side :
            if (!fL)
              cur += dp[curPos + 1][curSum][ccCenter][true][fR];
            if (!fR)
              cur += dp[curPos + 1][curSum][ccCenter][fL][true];
            // merge center and side :
            if (fL and ccCenter)
              cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][true][fR];
            if (fR and ccCenter)
              cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][fL][true];
            // merge centers :
            if (ccCenter >= 2)
              cur += ccCenter * (ccCenter - 1) *
                     dp[curPos + 1][curSum][ccCenter - 1][fL][fR];
            // add to center :
            if (ccCenter)
              cur += 2 * ccCenter * dp[curPos + 1][curSum][ccCenter][fL][fR];
            // add to side:
            if (fL)
              cur += dp[curPos + 1][curSum][ccCenter][fL][fR];
            if (fR)
              cur += dp[curPos + 1][curSum][ccCenter][fL][fR];
            // Add to center and merge with side :
            if (!fL and ccCenter)
              cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][true][fR];
            if (!fR and ccCenter)
              cur += ccCenter * dp[curPos + 1][curSum][ccCenter - 1][fL][true];
            cur %= MOD;
          }
}

signed main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> lenPerm >> maxSum;
  for (int i = 0; i < lenPerm; ++i) {
    cin >> diffs[i];
  }
  sort(diffs, diffs + lenPerm);
  for (int i = lenPerm - 1; i > 0; --i)
    diffs[i] -= diffs[i - 1];
  if (lenPerm == 1) {
    cout << 1 << endl;
    return 0;
  }
  iterative();
  cout << dp[0][0][0][0][0] << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 2124 KB Output is correct
5 Correct 16 ms 24140 KB Output is correct
6 Correct 14 ms 21708 KB Output is correct
7 Correct 3 ms 4300 KB Output is correct
8 Correct 2 ms 2252 KB Output is correct
9 Correct 15 ms 23228 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2764 KB Output is correct
2 Correct 3 ms 4684 KB Output is correct
3 Correct 2 ms 2380 KB Output is correct
4 Correct 3 ms 4684 KB Output is correct
5 Correct 3 ms 4684 KB Output is correct
6 Correct 4 ms 4428 KB Output is correct
7 Correct 1 ms 1612 KB Output is correct
8 Correct 2 ms 2252 KB Output is correct
9 Correct 4 ms 3532 KB Output is correct
10 Correct 4 ms 4044 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 2124 KB Output is correct
5 Correct 16 ms 24140 KB Output is correct
6 Correct 14 ms 21708 KB Output is correct
7 Correct 3 ms 4300 KB Output is correct
8 Correct 2 ms 2252 KB Output is correct
9 Correct 15 ms 23228 KB Output is correct
10 Correct 3 ms 3404 KB Output is correct
11 Correct 2 ms 2764 KB Output is correct
12 Correct 3 ms 4684 KB Output is correct
13 Correct 2 ms 2380 KB Output is correct
14 Correct 3 ms 4684 KB Output is correct
15 Correct 3 ms 4684 KB Output is correct
16 Correct 4 ms 4428 KB Output is correct
17 Correct 1 ms 1612 KB Output is correct
18 Correct 2 ms 2252 KB Output is correct
19 Correct 4 ms 3532 KB Output is correct
20 Correct 4 ms 4044 KB Output is correct
21 Correct 5 ms 5580 KB Output is correct
22 Correct 456 ms 203560 KB Output is correct
23 Correct 646 ms 311496 KB Output is correct
24 Correct 458 ms 235760 KB Output is correct
25 Correct 601 ms 315368 KB Output is correct
26 Correct 541 ms 268296 KB Output is correct
27 Correct 160 ms 78500 KB Output is correct
28 Correct 203 ms 96256 KB Output is correct
29 Correct 432 ms 182204 KB Output is correct
30 Correct 576 ms 316848 KB Output is correct