답안 #549175

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
549175 2022-04-15T11:17:07 Z spike1236 Kpart (eJOI21_kpart) C++14
100 / 100
1515 ms 196632 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

int a[1010];
int dp[1002][50002];
int pref[1002];
int n;

void solve() {
    cin >> n;
    int sum = 0;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        sum += a[i];
        pref[i] = pref[i - 1] + a[i];
    }
    memset(dp, -1, sizeof(dp));
    for(int i = 1; i <= n; ++i) {
        dp[i][0] = i;
        for(int j = 0; j <= min(pref[i], sum / 2); ++j) {
            dp[i][j] = dp[i - 1][j];
            if(j == a[i]) dp[i][j] = i;
            if(j > a[i]) dp[i][j] = max(dp[i][j], dp[i - 1][j - a[i]]);
        }
    }
    veci ans;
    for(int len = 2; len <= n; ++len) {
        bool can = 1;
        ///cout << len << ":\n";
        for(int l = 1; l + len - 1 <= n; ++l) {
            int r = l + len - 1;
            int s = pref[r] - pref[l - 1];
            if(s & 1) {
                can = 0;
                break;
            }
            ///cout << '\t' << dp[r][s / 2] << ' ' << s << ' ' << l << ' ' << r << '\n';
            if(dp[r][s / 2] < l) {
                can = 0;
                break;
            }
        }
        if(can) ans.pb(len);
    }
    cout << sz(ans) << ' '; for(auto i : ans) cout << i << ' ';
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int CNT_TESTS = 1;
    cin >> CNT_TESTS;
    for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
        solve();
        if(NUMCASE != CNT_TESTS) cout << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 433 ms 196396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 531 ms 196404 KB Output is correct
2 Correct 498 ms 196416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 495 ms 196416 KB Output is correct
2 Correct 665 ms 196448 KB Output is correct
3 Correct 670 ms 196460 KB Output is correct
4 Correct 821 ms 196492 KB Output is correct
5 Correct 1173 ms 196604 KB Output is correct
6 Correct 1515 ms 196632 KB Output is correct
7 Correct 1413 ms 196620 KB Output is correct