답안 #578749

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
578749 2022-06-17T20:26:29 Z kideso Kpart (eJOI21_kpart) C++17
30 / 100
2000 ms 444 KB
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

vector<int> x, ans;
vector<bool> y;
int N;

bool check(int l, int r, int sum) {
    vector<int> dp(sum + 1, 0);

    dp[0] = 1;

    for (int i = l; i <= r; ++i) {
        for (int j = sum; j >= 0; --j)
            if (dp[j] != 0 && j + x[i] <= sum) {
                dp[j + x[i]] = 1;
                if (j + x[i] == sum) return true;
            }
    }

    return false;
}

vector<int> solve() {
    vector<int> A;

    cin >> N;
    x.assign(N + 1, 0);
    y.assign(N + 1, false);
    y[0] = true;

    for (int i = 1; i <= N; ++i) cin >> x[i];

    for (int K = 2; K <= N; ++K) 
        if (!y[K]) {
            int sum = 0;
            bool ok = true;

            for (int i = 1; i <= K; ++i)
                sum += x[i];

            for (int i = K + 1; i <= N; ++i) {
                if (x[i] == x[i - K]) continue;

                if (sum % 2 != 0) {
                    ok = false;
                    break;
                }

                if (!check(i - K, i - 1, sum / 2)) {
                    ok = false;
                    break;
                }

                sum -= x[i - K];
                sum += x[i];
            }

            if (sum % 2 == 1 || !check(N - K + 1, N, sum / 2)) ok = false;

            if (ok) {
                for (int i = 0; i <= N; i++) 
                    if (y[i]) {
                        if (i + K <= N && !y[i + K]) {
                            A.push_back(i + K);
                            y[i + K] = true;
                        }
                }
            }
        }

    return A;
}

int main()
{
    int T;
    cin >> T;

    while (T--) {
        ans = solve();

        sort(ans.begin(), ans.end());

        cout << ans.size() << ' ';
        for (auto e : ans) cout << e << ' ';
        cout << '\n';
        ans.clear();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 212 KB Output is correct
2 Correct 146 ms 316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 317 ms 320 KB Output is correct
2 Correct 890 ms 300 KB Output is correct
3 Correct 813 ms 412 KB Output is correct
4 Correct 1368 ms 328 KB Output is correct
5 Correct 1856 ms 444 KB Output is correct
6 Execution timed out 2078 ms 416 KB Time limit exceeded
7 Halted 0 ms 0 KB -