답안 #1079216

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1079216 2024-08-28T12:06:26 Z boris_mihov Skyscraper (JOI16_skyscraper) C++17
20 / 100
2000 ms 297476 KB
#include <unordered_map>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>

typedef long long llong;
const int MAXN = 100 + 10;
const int MAXL = 1000 + 10;
const int MOD  = 1e9 + 7;

int n, l;
int a[MAXN];
std::unordered_map <int,int> dp[MAXN][MAXN][2][2];
std::unordered_map <int,bool> bl[MAXN][MAXN][2][2];

int f(int idx, int count, int sum, bool foundB, bool foundE)
{
    if (idx == n + 1)
    {
        if (!foundB || !foundE) return 0;
        return (count == 1 && sum <= l);
    }

    if (bl[idx][count][foundB][foundE][sum])
    {
        return dp[idx][count][foundB][foundE][sum];
    }

    bl[idx][count][foundB][foundE][sum] = 1;
    llong res = f(idx + 1, count + 1, sum - 2 * a[idx], foundB, foundE);
    if (!foundB)
    {
        res += f(idx + 1, count + 1, sum - a[idx], 1, foundE);
    }

    if (!foundE)
    {
        res += f(idx + 1, count + 1, sum - a[idx], foundB, 1);
    }

    if (count >= 1) 
    {
        res += (2LL * count - foundB - foundE) * f(idx + 1, count, sum, foundB, foundE);
        if (!foundB)
        {
            res += (count - foundE) * f(idx + 1, count, sum + a[idx], 1, foundE);
            if (foundE && count == 1 && idx == n)
            {
                res += f(idx + 1, count, sum + a[idx], 1, foundE);
            }
        }

        if (!foundE)
        {
            res += (count - foundB) * f(idx + 1, count, sum + a[idx], foundB, 1);
            if (foundB && count == 1 && idx == n)
            {
                res += f(idx + 1, count, sum + a[idx], foundB, 1);
            }
        }
    }

    if (count >= 2) 
    {
        int cntFree = count - foundB - foundE;
        res += 1LL * cntFree * (cntFree - 1) * f(idx + 1, count - 1, sum + 2 * a[idx], foundB, foundE);

        if (foundB)
        {
            res += 1LL * (count - 1 - foundE) * f(idx + 1, count - 1, sum + 2 * a[idx], foundB, foundE);
        }

        if (foundE)
        {
            res += 1LL * (count - 1 - foundB) * f(idx + 1, count - 1, sum + 2 * a[idx], foundB, foundE);
        }

        if (foundB && foundE && idx == n)
        {
            res += f(idx + 1, count - 1, sum + 2 * a[idx], foundB, foundE);
        }
    }

    return dp[idx][count][foundB][foundE][sum] = res % MOD;
}

void solve()
{
    if (n == 1)
    {
        std::cout << 1 << '\n';
        return;
    }

    std::sort(a + 1, a + 1 + n);
    std::cout << f(1, 0, 0, 0, 0) << '\n';
}

void input()
{
    std::cin >> n >> l;
    for (int i = 1 ; i <= n ; ++i)
    {
        std::cin >> a[i];
    }
}

void fastIOI()
{
    std::ios_base:: sync_with_stdio(0);
    std::cout.tie(nullptr);
    std::cin.tie(nullptr);
}

int main()
{
    fastIOI();
    input();
    solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5720 KB Output is correct
2 Correct 4 ms 5724 KB Output is correct
3 Correct 3 ms 5680 KB Output is correct
4 Correct 3 ms 5724 KB Output is correct
5 Correct 5 ms 6492 KB Output is correct
6 Correct 5 ms 6492 KB Output is correct
7 Correct 4 ms 6232 KB Output is correct
8 Correct 3 ms 5724 KB Output is correct
9 Correct 5 ms 6492 KB Output is correct
10 Correct 3 ms 5932 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 6236 KB Output is correct
2 Correct 36 ms 12148 KB Output is correct
3 Correct 7 ms 7004 KB Output is correct
4 Correct 24 ms 12308 KB Output is correct
5 Correct 31 ms 13656 KB Output is correct
6 Correct 19 ms 9816 KB Output is correct
7 Correct 11 ms 8284 KB Output is correct
8 Correct 6 ms 7004 KB Output is correct
9 Correct 7 ms 7040 KB Output is correct
10 Correct 21 ms 10844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5720 KB Output is correct
2 Correct 4 ms 5724 KB Output is correct
3 Correct 3 ms 5680 KB Output is correct
4 Correct 3 ms 5724 KB Output is correct
5 Correct 5 ms 6492 KB Output is correct
6 Correct 5 ms 6492 KB Output is correct
7 Correct 4 ms 6232 KB Output is correct
8 Correct 3 ms 5724 KB Output is correct
9 Correct 5 ms 6492 KB Output is correct
10 Correct 3 ms 5932 KB Output is correct
11 Correct 5 ms 6236 KB Output is correct
12 Correct 36 ms 12148 KB Output is correct
13 Correct 7 ms 7004 KB Output is correct
14 Correct 24 ms 12308 KB Output is correct
15 Correct 31 ms 13656 KB Output is correct
16 Correct 19 ms 9816 KB Output is correct
17 Correct 11 ms 8284 KB Output is correct
18 Correct 6 ms 7004 KB Output is correct
19 Correct 7 ms 7040 KB Output is correct
20 Correct 21 ms 10844 KB Output is correct
21 Correct 754 ms 94364 KB Output is correct
22 Execution timed out 2066 ms 297476 KB Time limit exceeded
23 Halted 0 ms 0 KB -