結果

問題 No.2596 Christmas Eve (Heuristic ver.)
ユーザー FplusFplusFFplusFplusF
提出日時 2023-12-24 18:29:57
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,205 ms / 1,224 ms
コード長 11,240 bytes
コンパイル時間 3,998 ms
コンパイル使用メモリ 284,136 KB
実行使用メモリ 6,676 KB
スコア 4,999,029
最終ジャッジ日時 2023-12-24 18:32:45
合計ジャッジ時間 165,699 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,204 ms
6,676 KB
testcase_01 AC 1,204 ms
6,676 KB
testcase_02 AC 1,204 ms
6,676 KB
testcase_03 AC 1,204 ms
6,676 KB
testcase_04 AC 1,204 ms
6,676 KB
testcase_05 AC 1,205 ms
6,676 KB
testcase_06 AC 1,204 ms
6,676 KB
testcase_07 AC 1,205 ms
6,676 KB
testcase_08 AC 1,205 ms
6,676 KB
testcase_09 AC 1,204 ms
6,676 KB
testcase_10 AC 1,204 ms
6,676 KB
testcase_11 AC 1,204 ms
6,676 KB
testcase_12 AC 1,204 ms
6,676 KB
testcase_13 AC 1,204 ms
6,676 KB
testcase_14 AC 1,204 ms
6,676 KB
testcase_15 AC 1,204 ms
6,676 KB
testcase_16 AC 1,205 ms
6,676 KB
testcase_17 AC 1,204 ms
6,676 KB
testcase_18 AC 1,204 ms
6,676 KB
testcase_19 AC 1,204 ms
6,676 KB
testcase_20 AC 1,204 ms
6,676 KB
testcase_21 AC 1,204 ms
6,676 KB
testcase_22 AC 1,204 ms
6,676 KB
testcase_23 AC 1,203 ms
6,676 KB
testcase_24 AC 1,204 ms
6,676 KB
testcase_25 AC 1,205 ms
6,676 KB
testcase_26 AC 1,204 ms
6,676 KB
testcase_27 AC 1,204 ms
6,676 KB
testcase_28 AC 1,204 ms
6,676 KB
testcase_29 AC 1,204 ms
6,676 KB
testcase_30 AC 1,204 ms
6,676 KB
testcase_31 AC 1,204 ms
6,676 KB
testcase_32 AC 1,204 ms
6,676 KB
testcase_33 AC 1,204 ms
6,676 KB
testcase_34 AC 1,203 ms
6,676 KB
testcase_35 AC 1,204 ms
6,676 KB
testcase_36 AC 1,203 ms
6,676 KB
testcase_37 AC 1,205 ms
6,676 KB
testcase_38 AC 1,204 ms
6,676 KB
testcase_39 AC 1,205 ms
6,676 KB
testcase_40 AC 1,204 ms
6,676 KB
testcase_41 AC 1,205 ms
6,676 KB
testcase_42 AC 1,205 ms
6,676 KB
testcase_43 AC 1,204 ms
6,676 KB
testcase_44 AC 1,204 ms
6,676 KB
testcase_45 AC 1,204 ms
6,676 KB
testcase_46 AC 1,205 ms
6,676 KB
testcase_47 AC 1,204 ms
6,676 KB
testcase_48 AC 1,204 ms
6,676 KB
testcase_49 AC 1,204 ms
6,676 KB
testcase_50 AC 1,204 ms
6,676 KB
testcase_51 AC 1,204 ms
6,676 KB
testcase_52 AC 1,205 ms
6,676 KB
testcase_53 AC 1,204 ms
6,676 KB
testcase_54 AC 1,205 ms
6,676 KB
testcase_55 AC 1,204 ms
6,676 KB
testcase_56 AC 1,204 ms
6,676 KB
testcase_57 AC 1,205 ms
6,676 KB
testcase_58 AC 1,204 ms
6,676 KB
testcase_59 AC 1,204 ms
6,676 KB
testcase_60 AC 1,204 ms
6,676 KB
testcase_61 AC 1,205 ms
6,676 KB
testcase_62 AC 1,204 ms
6,676 KB
testcase_63 AC 1,205 ms
6,676 KB
testcase_64 AC 1,204 ms
6,676 KB
testcase_65 AC 1,204 ms
6,676 KB
testcase_66 AC 1,205 ms
6,676 KB
testcase_67 AC 1,204 ms
6,676 KB
testcase_68 AC 1,203 ms
6,676 KB
testcase_69 AC 1,204 ms
6,676 KB
testcase_70 AC 1,204 ms
6,676 KB
testcase_71 AC 1,203 ms
6,676 KB
testcase_72 AC 1,205 ms
6,676 KB
testcase_73 AC 1,204 ms
6,676 KB
testcase_74 AC 1,204 ms
6,676 KB
testcase_75 AC 1,204 ms
6,676 KB
testcase_76 AC 1,205 ms
6,676 KB
testcase_77 AC 1,204 ms
6,676 KB
testcase_78 AC 1,204 ms
6,676 KB
testcase_79 AC 1,204 ms
6,676 KB
testcase_80 AC 1,205 ms
6,676 KB
testcase_81 AC 1,204 ms
6,676 KB
testcase_82 AC 1,205 ms
6,676 KB
testcase_83 AC 1,204 ms
6,676 KB
testcase_84 AC 1,205 ms
6,676 KB
testcase_85 AC 1,204 ms
6,676 KB
testcase_86 AC 1,204 ms
6,676 KB
testcase_87 AC 1,204 ms
6,676 KB
testcase_88 AC 1,204 ms
6,676 KB
testcase_89 AC 1,204 ms
6,676 KB
testcase_90 AC 1,203 ms
6,676 KB
testcase_91 AC 1,204 ms
6,676 KB
testcase_92 AC 1,205 ms
6,676 KB
testcase_93 AC 1,205 ms
6,676 KB
testcase_94 AC 1,204 ms
6,676 KB
testcase_95 AC 1,204 ms
6,676 KB
testcase_96 AC 1,205 ms
6,676 KB
testcase_97 AC 1,205 ms
6,676 KB
testcase_98 AC 1,204 ms
6,676 KB
testcase_99 AC 1,205 ms
6,676 KB
testcase_100 AC 1,204 ms
6,676 KB
testcase_101 AC 1,203 ms
6,676 KB
testcase_102 AC 1,204 ms
6,676 KB
testcase_103 AC 1,204 ms
6,676 KB
testcase_104 AC 1,205 ms
6,676 KB
testcase_105 AC 1,204 ms
6,676 KB
testcase_106 AC 1,204 ms
6,676 KB
testcase_107 AC 1,204 ms
6,676 KB
testcase_108 AC 1,205 ms
6,676 KB
testcase_109 AC 1,204 ms
6,676 KB
testcase_110 AC 1,204 ms
6,676 KB
testcase_111 AC 1,204 ms
6,676 KB
testcase_112 AC 1,204 ms
6,676 KB
testcase_113 AC 1,204 ms
6,676 KB
testcase_114 AC 1,204 ms
6,676 KB
testcase_115 AC 1,205 ms
6,676 KB
testcase_116 AC 1,204 ms
6,676 KB
testcase_117 AC 1,205 ms
6,676 KB
testcase_118 AC 1,204 ms
6,676 KB
testcase_119 AC 1,205 ms
6,676 KB
testcase_120 AC 1,204 ms
6,676 KB
testcase_121 AC 1,205 ms
6,676 KB
testcase_122 AC 1,205 ms
6,676 KB
testcase_123 AC 1,203 ms
6,676 KB
testcase_124 AC 1,205 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using pii=pair<int,int>;
using tii=tuple<int,int,int>;
using qii=tuple<int,int,int,int>;
using ll=long long;
using ld=long double;
const int INF=1e9;
#define rep(i,n) for (int i=0;i<(int)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
struct Tree{
    int w,h;
};
int N,K;
vector<vector<Tree>> tree;  //0->back,1->front->2->center
struct Solver{
    double score(vector<vector<int>> &ans){
        ll sum_h2=0;
        int sum_h=0;
        rep(i,K){
            int now=0;
            rep(j,4){
                now+=tree[min(2,j)][ans[i][j]].h;
            }
            sum_h2+=(ll)now*(ll)now;
            sum_h+=now;
        }
        return ((double)sum_h2/(double)K)-((double)sum_h/(double)K)*((double)sum_h/(double)K);
    }
    int true_score(vector<vector<int>> &ans){
        int mx=-INF,mn=INF;
        rep(i,K){
            int now=0;
            rep(j,4){
                now+=tree[min(2,j)][ans[i][j]].h;
            }
            chmax(mx,now);
            chmin(mn,now);
        }
        return mx-mn;
    }
    void solve(){
        set<pii> st_back,st_center;
        rep(i,N) st_back.insert({tree[0][i].w,i});
        rep(i,2*N) st_center.insert({tree[2][i].w,i});
        vector<vector<int>> ans(K,vector<int>(4,-10));   //back->front->center1->center2
        int cnt_tree=0;
        vector<int> front_ord(N),center_ord(2*N);
        rep(i,N) front_ord[i]=i;
        rep(i,2*N) center_ord[i]=i;
        sort(all(front_ord),[&](int i,int j){return tree[1][i].w<tree[1][j].w;});
        sort(all(center_ord),[&](int i,int j){return tree[2][i].w<tree[2][j].w;});
        for(auto &i:front_ord){
            for(auto &j:center_ord){
                if(tree[2][j].w<=tree[1][i].w) continue;
                if(st_center.find({tree[2][j].w,j})==st_center.end()) continue;
                auto itr_back=st_back.lower_bound({tree[1][i].w,-INF});
                if(itr_back==st_back.begin()) continue;
                itr_back--;
                auto itr_center=st_center.upper_bound({tree[2][j].w,INF});
                if(itr_center==st_center.end()) continue;
                ans[cnt_tree]={(*itr_back).second,i,j,(*itr_center).second};
                st_back.erase(itr_back);
                st_center.erase(itr_center);
                st_center.erase({tree[2][j].w,j});
                cnt_tree++;
                break;
            }
            if(cnt_tree==K) break;
        }
        assert(cnt_tree==K);
        vector<vector<int>> used(3,vector<int>(2*N,0));
        rep(i,K){
            rep(j,4){
                used[min(2,j)][ans[i][j]]=1;
            }
        }
        vector<vector<int>> not_used(3);
        rep(i,3){
            rep(j,2*N){
                if(i!=2&&j==N) break;
                if(!used[i][j]) not_used[i].push_back(j);
            }
        }
        mt19937 mt;
        set<pii> st;
        vector<int> sum_h(K,0);
        rep(i,K){
            rep(j,4){
                sum_h[i]+=tree[min(2,j)][ans[i][j]].h;
            }
            st.insert({sum_h[i],i});
        }
        int min_score=(*st.rbegin()).first-(*st.begin()).first;
        int limit_time=1200;
        auto start=chrono::system_clock::now();
        while(true){
            auto now=chrono::system_clock::now();
            int ms=chrono::duration_cast<chrono::milliseconds>(now-start).count();
            if(limit_time<=ms) break;
            int x=(*st.rbegin()).second,y=mt()%4;
            if(mt()%2==0) x=(*st.begin()).second;
            int r=mt()%11;
            if(r<=2){
                int p=mt()%K,q=y;
                if(x==p) continue;
                if(2<=y) q=2+mt()%2;
                st.erase({sum_h[x],x});
                st.erase({sum_h[p],p});
                int before_sum_h_x=sum_h[x],before_sum_h_p=sum_h[p];
                sum_h[x]-=tree[min(2,y)][ans[x][y]].h;
                sum_h[p]-=tree[min(2,y)][ans[p][q]].h;
                sum_h[x]+=tree[min(2,y)][ans[p][q]].h;
                sum_h[p]+=tree[min(2,y)][ans[x][y]].h;
                swap(ans[x][y],ans[p][q]);
                st.insert({sum_h[x],x});
                st.insert({sum_h[p],p});
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][ans[x][i+1]].w<=tree[min(2,i)][ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                rep(i,3){
                    if(tree[min(2,i+1)][ans[p][i+1]].w<=tree[min(2,i)][ans[p][i]].w){
                        ok=false;
                        break;
                    }
                }
                int new_score=(*st.rbegin()).first-(*st.begin()).first;
                double temp=10.0+(0.1-10.0)*(double)ms/(double)limit_time;
                double prob=exp((double)(min_score-new_score)/temp);
                if(ok&&1.0*(double)mt()/(double)mt19937::max()<prob){
                    min_score=new_score;
                }else{
                    swap(ans[x][y],ans[p][q]);
                    st.erase({sum_h[x],x});
                    st.erase({sum_h[p],p});
                    sum_h[x]=before_sum_h_x;
                    sum_h[p]=before_sum_h_p;
                    st.insert({sum_h[x],x});
                    st.insert({sum_h[p],p});
                }
            }else if(r<=6){
                int z=mt()%(int)not_used[min(2,y)].size();
                int before_sum_h_x=sum_h[x];
                st.erase({sum_h[x],x});
                sum_h[x]-=tree[min(2,y)][ans[x][y]].h;
                sum_h[x]+=tree[min(2,y)][not_used[min(2,y)][z]].h;
                swap(ans[x][y],not_used[min(2,y)][z]);
                st.insert({sum_h[x],x});
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][ans[x][i+1]].w<=tree[min(2,i)][ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                int new_score=(*st.rbegin()).first-(*st.begin()).first;
                double temp=10.0+(0.1-10.0)*(double)ms/(double)limit_time;
                double prob=exp((double)(min_score-new_score)/temp);
                if(ok&&1.0*(double)mt()/(double)mt19937::max()<prob){
                    min_score=new_score;
                }else{
                    swap(ans[x][y],not_used[min(2,y)][z]);
                    st.erase({sum_h[x],x});
                    sum_h[x]=before_sum_h_x;
                    st.insert({sum_h[x],x});
                }
            }else if(r<=9){
                vector<int> v;
                rep(i,4){
                    if(i==y) continue;
                    v.push_back(i);
                }
                int z=v[mt()%(int)v.size()];
                int p=mt()%(int)not_used[min(2,y)].size(),q=mt()%(int)not_used[min(2,z)].size();
                if(min(2,y)==min(2,z)&&p==q) continue;
                int before_sum_h_x=sum_h[x];
                st.erase({sum_h[x],x});
                sum_h[x]-=tree[min(2,y)][ans[x][y]].h;
                sum_h[x]-=tree[min(2,z)][ans[x][z]].h;
                sum_h[x]+=tree[min(2,y)][not_used[min(2,y)][p]].h;
                sum_h[x]+=tree[min(2,z)][not_used[min(2,z)][q]].h;
                swap(ans[x][y],not_used[min(2,y)][p]);
                swap(ans[x][z],not_used[min(2,z)][q]);
                st.insert({sum_h[x],x});
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][ans[x][i+1]].w<=tree[min(2,i)][ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                int new_score=(*st.rbegin()).first-(*st.begin()).first;
                double temp=10.0+(0.1-10.0)*(double)ms/(double)limit_time;
                double prob=exp((double)(min_score-new_score)/temp);
                if(ok&&1.0*(double)mt()/(double)mt19937::max()<prob){
                    min_score=new_score;
                }else{
                    swap(ans[x][y],not_used[min(2,y)][p]);
                    swap(ans[x][z],not_used[min(2,z)][q]);
                    st.erase({sum_h[x],x});
                    sum_h[x]=before_sum_h_x;
                    st.insert({sum_h[x],x});
                }
            }else{
                vector<int> v;
                rep(i,4){
                    if(i==y) continue;
                    v.push_back(i);
                }
                int y2=v[mt()%(int)v.size()];
                int p=mt()%(int)not_used[min(2,y)].size(),p2=mt()%(int)not_used[min(2,y2)].size();
                v.clear();
                rep(i,4){
                    if(i==y||i==y2) continue;
                    v.push_back(i);
                }
                int y3=v[mt()%(int)v.size()],p3=mt()%(int)not_used[min(2,y3)].size();
                if(pii{min(2,y),p}==pii{min(2,y2),p2}||pii{min(2,y),p}==pii{min(2,y3),p3}||pii{min(2,y2),p2}==pii{min(2,y3),p3}) continue;
                int before_sum_h_x=sum_h[x];
                st.erase({sum_h[x],x});
                sum_h[x]-=tree[min(2,y)][ans[x][y]].h;
                sum_h[x]-=tree[min(2,y2)][ans[x][y2]].h;
                sum_h[x]-=tree[min(2,y3)][ans[x][y3]].h;
                sum_h[x]+=tree[min(2,y)][not_used[min(2,y)][p]].h;
                sum_h[x]+=tree[min(2,y2)][not_used[min(2,y2)][p2]].h;
                sum_h[x]+=tree[min(2,y3)][not_used[min(2,y3)][p3]].h;
                swap(ans[x][y],not_used[min(2,y)][p]);
                swap(ans[x][y2],not_used[min(2,y2)][p2]);
                swap(ans[x][y3],not_used[min(2,y3)][p3]);
                st.insert({sum_h[x],x});
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][ans[x][i+1]].w<=tree[min(2,i)][ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                int new_score=(*st.rbegin()).first-(*st.begin()).first;
                double temp=10.0+(0.1-10.0)*(double)ms/(double)limit_time;
                double prob=exp((double)(min_score-new_score)/temp);
                if(ok&&1.0*(double)mt()/(double)mt19937::max()<prob){
                    min_score=new_score;
                }else{
                    swap(ans[x][y],not_used[min(2,y)][p]);
                    swap(ans[x][y2],not_used[min(2,y2)][p2]);
                    swap(ans[x][y3],not_used[min(2,y3)][p3]);
                    st.erase({sum_h[x],x});
                    sum_h[x]=before_sum_h_x;
                    st.insert({sum_h[x],x});
                }
            }
        }
        rep(i,K){
            cout << ans[i][1]+1 << ' ' << ans[i][2]+1 << ' ' << ans[i][3]+1 << ' ' << ans[i][0]+1 << '\n';
        }
    }
}Solver;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N >> K;
    tree.resize(3);
    tree[1].resize(N);
    tree[2].resize(2*N);
    tree[0].resize(N);
    rep(i,N) cin >> tree[1][i].w;
    rep(i,N) cin >> tree[1][i].h;
    rep(i,2*N) cin >> tree[2][i].w;
    rep(i,2*N) cin >> tree[2][i].h;
    rep(i,N) cin >> tree[0][i].w;
    rep(i,N) cin >> tree[0][i].h;
    Solver.solve();
}
0