Submission #581364

# Submission time Handle Problem Language Result Execution time Memory
581364 2022-06-22T14:18:14 Z kideso Kpart (eJOI21_kpart) C++17
30 / 100
2000 ms 508 KB
#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

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

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;
}

void solve() {
    vector<int> A;

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

    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 - K; i++)
                    if (y[i]) {
                        if (!y[i + K]) {
                            y[i + K] = true;
                            ++db;
                        }
                    }
            }
        }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int T;
    cin >> T;

    while (T--) {
        solve();

        cout << db << ' ';
        for (int i = 1; i <= N; ++i)
            if (y[i]) cout << i << ' ';
        cout << '\n';
   }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 332 KB Output is correct
2 Correct 190 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 348 ms 356 KB Output is correct
2 Correct 1014 ms 488 KB Output is correct
3 Correct 1125 ms 472 KB Output is correct
4 Correct 1647 ms 508 KB Output is correct
5 Execution timed out 2081 ms 496 KB Time limit exceeded
6 Halted 0 ms 0 KB -