답안 #925779

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
925779 2024-02-12T08:43:31 Z vjudge1 Kitchen (BOI19_kitchen) C++17
72 / 100
83 ms 5972 KB
/*
no more temmy :(
*/

#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
// #include<icecream.hpp>
// using namespace icecream;
#define ll long long
#define int ll
#define ld long double
#define y1 cheza
#define imp cout<<"Impossible\n"
// mt19937 rng(1983413);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T> using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
const int N=500;
const int M=300*300+100;
const int B=40*40+100;
const int mod=998244353;
const int INF=1e18;
const int lg=64;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
const double eps=1e-9;
int n,m,k;
int a[N];
int b[N];
int w[N];
int c[N];
int d[N];
int dp2[M];
void solvek1(){
    sort(b+1,b+m+1);
    dp2[0]=1;
    for(int i=1;i<=m;i++){
        for(int j=M-100;j>=0;j--){
            if(dp2[j]){
                if(j+b[i]<M){
                    dp2[j+b[i]]=1;
                }
            }
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++){
        sum+=a[i];
    }
    for(int i=sum;i<=M-100;i++){
        if(dp2[i]){
            cout<<i-sum<<'\n';
            return;
        }
    }
    assert(false);
}
int dp[B][B];
void solve2(){
    dp[0][0]=1;
    for(int i=1;i<=m;i++){
        int r=min(b[i],n);
        for(int j=B-10;j>=0;j--){
            if(j+a[i]<=B-10){
                for(int p=B-10;p>=0;p--){
                    if(dp[j][p]){
                        if(p+r<=B-10){
                            dp[j+b[i]][p+r]=1;
                        }
                    }
                }
            }
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++){
        sum+=a[i];
    }
    int ans=INF;
    for(int i=n*k;i<=B-10;i++){
        for(int j=sum;j<=B-10;j++){
            if(dp[j][i]){
                ans=min(ans,j-sum);
                break;
            }
        }
    }
    cout<<ans<<'\n';
}
void test(){
    cin>>n>>m>>k;
    if(m<k){
        imp;
        return;
    }
    int sum=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        sum+=a[i];
        if(a[i]<k){
            imp;
            return;
        }
    }
    sort(a+1,a+n+1);
    for(int i=1;i<=m;i++){
        cin>>b[i];
        sum-=b[i];
    }
    sort(b+1,b+m+1);
    if(sum>0){
        imp;
        return;
    }
    if(n<=40&&15<m&&m<=40&&k<=40&&a[n]<=40&&b[m]<=40){
        solve2();
        return;
    }
    if(k==1){
        solvek1();
        return;
    }
    int res=INF;
    for(int i=0;i<(1ll<<m);i++){
        for(int j=1;j<=n;j++){
            c[j]=a[j];
        }
        for(int j=1;j<=m;j++){
            w[j]=b[j];
        }
        {
        int sum=0;
        for(int j=1;j<=n;j++){
            sum+=a[j];
        }
        for(int j=0;j<m;j++){
            if((1ll<<j)&(i)){
                sum-=b[j+1];
            }
        }
        if(sum>0){
            continue;
        }
        sum=n*k;
        for(int j=0;j<m;j++){
            if((1ll<<j)&(i)){
                
                sum-=min(b[j+1],n);
            }
        }
        if(sum>0){
            continue;
        }
        }
        for(int j=0;j<m;j++){
            if((1ll<<j)&(i)){
                if(w[j+1]>0){
                    for(int u=1;u<=n;u++){
                        if(c[u]>0){
                            int d=min(w[j+1],c[u]);
                            w[j+1]-=d;
                            c[u]-=d;
                        }
                    }
                }
            }
        }
        int ans=1;
        for(int i=1;i<=n;i++){
            // cout<<c[i]<<' '<<d[i]<<'\n';
            if(c[i]!=0){
                ans=0;
            }
        }
        if(ans){
            int sum=0;
            for(int i=1;i<=n;i++){
                sum+=a[i];
            }
            for(int j=0;j<m;j++){
                if((1ll<<j)&(i)){
                    sum-=b[j+1];
                }
            }
            sum=abs(sum);
            res=min(sum,res);
        }
    }
    if(res==INF){
        imp;
        return;
    }
    cout<<res<<'\n';

}
/*

*/  
 
signed main(){
    // ic.prefix("debug->| ");
    // freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    // cout.tie(nullptr);
    long long t2=1;
    //  cin>>t2;
    for(int i=1;i<=t2;i++){
        test();
    }
    
    return 0;
 
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 6 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 2908 KB Output is correct
2 Correct 10 ms 2652 KB Output is correct
3 Correct 12 ms 2652 KB Output is correct
4 Correct 13 ms 2908 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 8 ms 2652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 54 ms 5972 KB Output is correct
2 Correct 49 ms 4180 KB Output is correct
3 Correct 48 ms 4900 KB Output is correct
4 Correct 47 ms 5204 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2392 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 4 ms 348 KB Output is correct
10 Correct 6 ms 348 KB Output is correct
11 Correct 2 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 6 ms 348 KB Output is correct
14 Correct 10 ms 2908 KB Output is correct
15 Correct 10 ms 2652 KB Output is correct
16 Correct 12 ms 2652 KB Output is correct
17 Correct 13 ms 2908 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 8 ms 2652 KB Output is correct
20 Correct 54 ms 5972 KB Output is correct
21 Correct 49 ms 4180 KB Output is correct
22 Correct 48 ms 4900 KB Output is correct
23 Correct 47 ms 5204 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Incorrect 83 ms 348 KB Output isn't correct
26 Halted 0 ms 0 KB -