답안 #648525

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
648525 2022-10-06T19:06:25 Z Lobo Kitchen (BOI19_kitchen) C++17
100 / 100
68 ms 138980 KB
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 303;

int n, m, k, a[maxn], b[maxn];
int m1, m2, b1[maxn], b2[maxn], mv1, mv2;
int dp1[maxn][maxn*maxn], dp2[maxn][maxn*maxn], wtf[maxn*maxn];

void solve() {
    cin >> n >> m >> k;

    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= m; i++) {
        cin >> b[i];
    }

    int sma = 0;
    for(int i = 1; i <= n; i++) {
        sma+= a[i];
        if(a[i] < k) {
            cout << "Impossible" << endl;
            return;
        }
    }
    int smb = 0;
    for(int i = 1; i <= m; i++) {
        smb+= b[i];
        if(b[i] <= n) {
            b1[++m1] = b[i];
            mv1+= b[i];
        }
        else {
            b2[++m2] = b[i];
            mv2+= b[i];
        }
    }

    if(m < k || smb < sma) {
        cout << "Impossible" << endl;
        return;
    }

    dp1[0][0] = 1;
    for(int i = 1; i <= m1; i++) {
        for(int j = 0; j <= mv1; j++) {
            dp1[i][j] = dp1[i-1][j];
            if(j-b1[i] >= 0) dp1[i][j]|= dp1[i-1][j-b1[i]];
        }
    }

    dp2[0][0] = 0;
    for(int i = 1; i <= mv2; i++) dp2[0][i] = -inf;
    for(int i = 1; i <= m2; i++) {
        for(int j = 0; j <= mv2; j++) {
            dp2[i][j] = dp2[i-1][j];
            if(j-b2[i] >= 0) dp2[i][j] = max(dp2[i][j],dp2[i-1][j-b2[i]]+1);
        }
    }

    int ans = inf;
    // for(int i = 0; i <= mv1; i++) {
    //     if(dp1[m1][i] == 0) continue;
    //     for(int j = 0; j <= mv2; j++) {
    //         if(j >= sma-i && dp2[m2][j]*n >= n*k-i) {
    //             ans = min(ans, i+j-sma);
    //         }
    //     }
    // }

    for(int i = 0; i <= mv1; i++) {
        wtf[i] = inf;
    }

    int r = mv2+1;
    int cur = inf;
    for(int i = 0; i <= mv1; i++) {
        while(i+r-1 >= sma && r-1 >= 0) {
            r--;
            if(dp2[m2][r] < 0) continue;
            if(n*k - dp2[m2][r]*n <= i) cur = min(cur,r);
            else wtf[n*k - dp2[m2][r]*n] = min(wtf[n*k - dp2[m2][r]*n],r);
        }
        cur = min(cur,wtf[i]);
        if(dp1[m1][i] == 1) ans = min(ans, i+cur-sma);
    }


    if(ans > smb-sma) cout << "Impossible" << endl;
    else cout << ans << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    // cin >> tt;
    while(tt--) {
        solve();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 452 KB Output is correct
13 Correct 1 ms 836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 136704 KB Output is correct
2 Correct 49 ms 102152 KB Output is correct
3 Correct 57 ms 107592 KB Output is correct
4 Correct 49 ms 106744 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 48 ms 83240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 852 KB Output is correct
2 Correct 1 ms 596 KB Output is correct
3 Correct 1 ms 852 KB Output is correct
4 Correct 1 ms 980 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 0 ms 336 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 724 KB Output is correct
10 Correct 1 ms 724 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 452 KB Output is correct
13 Correct 1 ms 836 KB Output is correct
14 Correct 64 ms 136704 KB Output is correct
15 Correct 49 ms 102152 KB Output is correct
16 Correct 57 ms 107592 KB Output is correct
17 Correct 49 ms 106744 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 48 ms 83240 KB Output is correct
20 Correct 1 ms 852 KB Output is correct
21 Correct 1 ms 596 KB Output is correct
22 Correct 1 ms 852 KB Output is correct
23 Correct 1 ms 980 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 51 ms 95668 KB Output is correct
26 Correct 43 ms 79560 KB Output is correct
27 Correct 14 ms 20692 KB Output is correct
28 Correct 28 ms 52932 KB Output is correct
29 Correct 68 ms 138980 KB Output is correct
30 Correct 49 ms 101196 KB Output is correct