Cod sursa(job #2648282)

Utilizator OvidRata Ovidiu Ovid Data 9 septembrie 2020 18:31:19
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.06 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("lapte.in"); ofstream fout("lapte.out");
#define cin fin
#define cout fout

int N, a[110],b[110], L;
int c1[110], c2[110];
int16_t cns[101][101][101];
bool v[101][101][101];
int16_t dir[101][101][101];


int32_t main(){
INIT
cin>>N>>L;
for(int i=1; i<=N; i++){cin>>a[i]>>b[i];}

int res=101;
int l=0, r=101, m=(l+r)/2;
while(l<r){
    m=(l+r)/2;
    int t=m;
    v[0][0][0]=true;
    for(int i=1; i<=N; i++){
        for(int l1=0; l1<=L; l1++){
            for(int l2=0; l2<=L; l2++){
                if(v[i-1][l1][l2]){
                    cns[i][l1][l2]=0;v[i][l1][l2]=true;
                }
            }}

    for(int l1=0; l1<=L; l1++){
        for(int l2=0; l2<=L; l2++){
            if(!v[i][l1][l2]){
                if( (l1>0) && (v[i][l1-1][l2]) && ((cns[i][l1-1][l2]+a[i])<=t) ){
                    cns[i][l1][l2]=min(cns[i][l1-1][l2]+a[i], (cns[i][l1][l2]==0)?(100ll):(cns[i][l1][l2]) );
                    v[i][l1][l2]=true;
                }
                if( (l2>0) && (v[i][l1][l2-1]) && ((cns[i][l1][l2-1]+b[i])<=t) ){
                    cns[i][l1][l2]=min(cns[i][l1][l2-1]+b[i], (cns[i][l1][l2]==0)?(100ll):(cns[i][l1][l2]) );
                    v[i][l1][l2]=true;
                }
                if( (l1>0) && (l2>0) && (v[i][l1-1][l2-1]) && ((cns[i][l1-1][l2-1]+a[i]+b[i])<=t) ){
                    cns[i][l1][l2]=min(cns[i][l1-1][l2-1]+a[i]+b[i], (cns[i][l1][l2]==0)?(100ll):(cns[i][l1][l2]) );
                    v[i][l1][l2]=true;
                }
            }
        }
    }

    }

    if(v[N][L][L]==true){
        res=min(res, m);
        r=m;
    }else{ l=(m+1); }
    m=(l+r)/2;
    for(int i=1; i<=N; i++){
        for(int l1=0; l1<=L; l1++){
            for(int l2=0; l2<=L; l2++){
                cns[i][l1][l2]=0; v[i][l1][l2]=false;
            }
        }
    }
}
cout<<res<<"\n";


int t=res;
v[0][0][0]=true;
for(int i=1; i<=N; i++){
        for(int l1=0; l1<=L; l1++){
            for(int l2=0; l2<=L; l2++){
                if(v[i-1][l1][l2]){
                    cns[i][l1][l2]=0;v[i][l1][l2]=true;
                }
            }}

    for(int l1=0; l1<=L; l1++){
        for(int l2=0; l2<=L; l2++){
            if(!v[i][l1][l2]){
                if( (l1>0) && (v[i][l1-1][l2]) && ((cns[i][l1-1][l2]+a[i])<=t) ){
                    if( (cns[i][l1][l2]==0) || (cns[i][l1][l2]>(cns[i][l1-1][l2]+a[i])) ){
                    cns[i][l1][l2]=cns[i][l1-1][l2]+a[i]; dir[i][l1][l2]=1;
                    v[i][l1][l2]=true;}
                }
                if( (l2>0) && (v[i][l1][l2-1]) && ((cns[i][l1][l2-1]+b[i])<=t) ){
                    if( (cns[i][l1][l2]==0) || (cns[i][l1][l2]>(cns[i][l1][l2-1]+b[i]) ) ){
                    cns[i][l1][l2]=cns[i][l1][l2-1]+b[i]; dir[i][l1][l2]=3;
                    v[i][l1][l2]=true;}
                }
                if( (l1>0) && (l2>0) && (v[i][l1-1][l2-1]) && ((cns[i][l1-1][l2-1]+a[i]+b[i])<=t) ){
                    if( (cns[i][l1][l2]==0) || (cns[i][l1][l2]>(cns[i][l1-1][l2-1]+a[i]+b[i]) ) ){
                    cns[i][l1][l2]=cns[i][l1-1][l2-1]+a[i]+b[i]; dir[i][l1][l2]=2;
                    v[i][l1][l2]=true;}
                }
            }
        }
    }

}//cout<<v[N][L][L]<<"\n"<<dir[N][L][L]<<"\n";



int l1=L, l2=L;
vector<pii> pr;
for(int i=N; i>=1; i--){
    int x=l1, y=l2;
    while(dir[i][x][y]>0){
        switch(dir[i][x][y]){
            case 1:x--; break;
            case 2:x--; y--; break;
            case 3:y--; break;
        }
    }
    int d1=l1-x, d2=l2-y; pr.pb(mp(d1, d2)); l1=x; l2=y;
}
for(int i=pr.size()-1; i>=0; i--){
    cout<<pr[i].ft<<" "<<pr[i].sc<<"\n";
}



return 0;
}