답안 #493018

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
493018 2021-12-10T02:55:22 Z wiwiho Kitchen (BOI19_kitchen) C++14
31 / 100
25 ms 460 KB
#include <bits/stdc++.h>

#define printv(a, b) {\
    for(auto pv : a) b << pv << " ";\
    b << "\n";\
}

using namespace std;

typedef long long ll;

const ll MAX = INT_MAX;

void nosol(){
    cout << "Impossible\n";
    exit(0);
}

int n, m, k;
vector<int> a;
vector<int> b;
ll sa = 0;

const int SZ = 300;

ll calc(int msk){
    //cerr << "calc " << msk << "\n";
    
    vector<ll> t(SZ + 5);
    ll sb = 0;
    
    for(int i = 1; i <= m; i++){
        if(!(1 << (i - 1) & msk)) continue;
        sb += b[i];
        t[b[i]]++;
    }

    if(sa > sb) return MAX;
    
    for(int i = SZ; i >= 1; i--) t[i] += t[i + 1];
    //printv(t, cerr);

    int pos = 1, now = 1;
    for(int i = 1; i <= n; i++){
        int v = k;
        //cerr << "test " << i << "  " << pos << " " << now << " " << v << "\n";
        if(v <= t[now] - pos + 1){
            pos += v;
        }
        else{
            v -= t[now] - pos + 1;
            if(t[now + 1] < v || v >= pos) return MAX;
            now++;
            pos = v + 1;
        }
        if(pos == t[now] + 1) now++, pos = 1;
    }

    return sb - sa;
}

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

    cin >> n >> m >> k;
    a.resize(n + 1);
    b.resize(m + 1);

    ll sb = 0;
    for(int i = 1; i <= n; i++) cin >> a[i], sa += a[i];
    for(int i = 1; i <= m; i++) cin >> b[i], sb += b[i];
    sort(a.begin() + 1, a.end(), greater<>());
    sort(b.begin() + 1, b.end());
    
    if(k > m || sa > sb){
        nosol();
    }

    for(int i = 1; i <= n; i++) if(a[i] < k) nosol();

    assert(m <= 15);

    ll ans = MAX;
    for(int i = 0; i < (1 << m); i++){
        ans = min(ans, calc(i));
    }

    if(ans == MAX) nosol();

    cout << ans << "\n";

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 20 ms 312 KB Output is correct
10 Correct 25 ms 312 KB Output is correct
11 Correct 7 ms 292 KB Output is correct
12 Correct 6 ms 304 KB Output is correct
13 Correct 8 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1 ms 460 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 0 ms 204 KB Output is correct
9 Correct 20 ms 312 KB Output is correct
10 Correct 25 ms 312 KB Output is correct
11 Correct 7 ms 292 KB Output is correct
12 Correct 6 ms 304 KB Output is correct
13 Correct 8 ms 204 KB Output is correct
14 Runtime error 1 ms 460 KB Execution killed with signal 6
15 Halted 0 ms 0 KB -