結果

問題 No.2596 Christmas Eve (Heuristic ver.)
ユーザー FplusFplusFFplusFplusF
提出日時 2023-12-24 17:34:48
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,209 ms / 1,224 ms
コード長 8,132 bytes
コンパイル時間 4,261 ms
コンパイル使用メモリ 292,044 KB
実行使用メモリ 6,676 KB
スコア 4,996,873
最終ジャッジ日時 2023-12-24 17:38:45
合計ジャッジ時間 143,239 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,209 ms
6,676 KB
testcase_01 AC 910 ms
6,676 KB
testcase_02 AC 909 ms
6,676 KB
testcase_03 AC 1,208 ms
6,676 KB
testcase_04 AC 1,208 ms
6,676 KB
testcase_05 AC 1,208 ms
6,676 KB
testcase_06 AC 1,208 ms
6,676 KB
testcase_07 AC 909 ms
6,676 KB
testcase_08 AC 910 ms
6,676 KB
testcase_09 AC 1,208 ms
6,676 KB
testcase_10 AC 908 ms
6,676 KB
testcase_11 AC 1,208 ms
6,676 KB
testcase_12 AC 911 ms
6,676 KB
testcase_13 AC 911 ms
6,676 KB
testcase_14 AC 1,208 ms
6,676 KB
testcase_15 AC 1,208 ms
6,676 KB
testcase_16 AC 911 ms
6,676 KB
testcase_17 AC 910 ms
6,676 KB
testcase_18 AC 1,209 ms
6,676 KB
testcase_19 AC 1,208 ms
6,676 KB
testcase_20 AC 1,207 ms
6,676 KB
testcase_21 AC 913 ms
6,676 KB
testcase_22 AC 1,208 ms
6,676 KB
testcase_23 AC 909 ms
6,676 KB
testcase_24 AC 1,207 ms
6,676 KB
testcase_25 AC 911 ms
6,676 KB
testcase_26 AC 909 ms
6,676 KB
testcase_27 AC 1,209 ms
6,676 KB
testcase_28 AC 910 ms
6,676 KB
testcase_29 AC 1,208 ms
6,676 KB
testcase_30 AC 1,209 ms
6,676 KB
testcase_31 AC 1,208 ms
6,676 KB
testcase_32 AC 910 ms
6,676 KB
testcase_33 AC 1,208 ms
6,676 KB
testcase_34 AC 910 ms
6,676 KB
testcase_35 AC 1,208 ms
6,676 KB
testcase_36 AC 1,208 ms
6,676 KB
testcase_37 AC 910 ms
6,676 KB
testcase_38 AC 1,208 ms
6,676 KB
testcase_39 AC 910 ms
6,676 KB
testcase_40 AC 910 ms
6,676 KB
testcase_41 AC 910 ms
6,676 KB
testcase_42 AC 910 ms
6,676 KB
testcase_43 AC 1,208 ms
6,676 KB
testcase_44 AC 910 ms
6,676 KB
testcase_45 AC 1,208 ms
6,676 KB
testcase_46 AC 910 ms
6,676 KB
testcase_47 AC 1,207 ms
6,676 KB
testcase_48 AC 910 ms
6,676 KB
testcase_49 AC 911 ms
6,676 KB
testcase_50 AC 1,208 ms
6,676 KB
testcase_51 AC 910 ms
6,676 KB
testcase_52 AC 909 ms
6,676 KB
testcase_53 AC 910 ms
6,676 KB
testcase_54 AC 911 ms
6,676 KB
testcase_55 AC 910 ms
6,676 KB
testcase_56 AC 911 ms
6,676 KB
testcase_57 AC 910 ms
6,676 KB
testcase_58 AC 910 ms
6,676 KB
testcase_59 AC 1,208 ms
6,676 KB
testcase_60 AC 911 ms
6,676 KB
testcase_61 AC 910 ms
6,676 KB
testcase_62 AC 1,207 ms
6,676 KB
testcase_63 AC 910 ms
6,676 KB
testcase_64 AC 910 ms
6,676 KB
testcase_65 AC 1,209 ms
6,676 KB
testcase_66 AC 910 ms
6,676 KB
testcase_67 AC 1,208 ms
6,676 KB
testcase_68 AC 909 ms
6,676 KB
testcase_69 AC 910 ms
6,676 KB
testcase_70 AC 1,208 ms
6,676 KB
testcase_71 AC 1,208 ms
6,676 KB
testcase_72 AC 910 ms
6,676 KB
testcase_73 AC 910 ms
6,676 KB
testcase_74 AC 1,207 ms
6,676 KB
testcase_75 AC 1,208 ms
6,676 KB
testcase_76 AC 910 ms
6,676 KB
testcase_77 AC 910 ms
6,676 KB
testcase_78 AC 1,209 ms
6,676 KB
testcase_79 AC 911 ms
6,676 KB
testcase_80 AC 910 ms
6,676 KB
testcase_81 AC 1,207 ms
6,676 KB
testcase_82 AC 911 ms
6,676 KB
testcase_83 AC 910 ms
6,676 KB
testcase_84 AC 909 ms
6,676 KB
testcase_85 AC 1,208 ms
6,676 KB
testcase_86 AC 1,208 ms
6,676 KB
testcase_87 AC 909 ms
6,676 KB
testcase_88 AC 910 ms
6,676 KB
testcase_89 AC 910 ms
6,676 KB
testcase_90 AC 1,208 ms
6,676 KB
testcase_91 AC 909 ms
6,676 KB
testcase_92 AC 911 ms
6,676 KB
testcase_93 AC 910 ms
6,676 KB
testcase_94 AC 910 ms
6,676 KB
testcase_95 AC 910 ms
6,676 KB
testcase_96 AC 911 ms
6,676 KB
testcase_97 AC 910 ms
6,676 KB
testcase_98 AC 910 ms
6,676 KB
testcase_99 AC 910 ms
6,676 KB
testcase_100 AC 911 ms
6,676 KB
testcase_101 AC 909 ms
6,676 KB
testcase_102 AC 910 ms
6,676 KB
testcase_103 AC 1,208 ms
6,676 KB
testcase_104 AC 910 ms
6,676 KB
testcase_105 AC 1,209 ms
6,676 KB
testcase_106 AC 911 ms
6,676 KB
testcase_107 AC 910 ms
6,676 KB
testcase_108 AC 911 ms
6,676 KB
testcase_109 AC 1,208 ms
6,676 KB
testcase_110 AC 1,208 ms
6,676 KB
testcase_111 AC 1,207 ms
6,676 KB
testcase_112 AC 911 ms
6,676 KB
testcase_113 AC 1,208 ms
6,676 KB
testcase_114 AC 909 ms
6,676 KB
testcase_115 AC 910 ms
6,676 KB
testcase_116 AC 910 ms
6,676 KB
testcase_117 AC 911 ms
6,676 KB
testcase_118 AC 910 ms
6,676 KB
testcase_119 AC 911 ms
6,676 KB
testcase_120 AC 910 ms
6,676 KB
testcase_121 AC 909 ms
6,676 KB
testcase_122 AC 911 ms
6,676 KB
testcase_123 AC 1,208 ms
6,676 KB
testcase_124 AC 910 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(){
        int all_min_score=INF;
        vector<vector<int>> all_min_score_ans;
        rep(t,4){
            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;
            if(t&1) sort(all(front_ord),[&](int i,int j){return tree[1][i].w<tree[1][j].w;});
            else sort(all(front_ord),[&](int i,int j){return tree[1][i].w>tree[1][j].w;});
            if(t&2) sort(all(center_ord),[&](int i,int j){return tree[2][i].w<tree[2][j].w;});
            else 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;
            }
            if(cnt_tree!=K) continue;
            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;
            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(300<=ms) break;
                int x=(*st.rbegin()).second,y=mt()%4;
                if(mt()%2==0) x=(*st.begin()).second;
                if(mt()%3==0){
                    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=3.0+(0.1-3.0)*(double)ms/300.0;
                    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{
                    int z=mt()%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=3.0+(0.1-3.0)*(double)ms/300.0;
                    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});
                    }
                }
            }
            if(min_score<all_min_score){
                all_min_score=min_score;
                all_min_score_ans=ans;
            }
        }
        rep(i,K){
            cout << all_min_score_ans[i][1]+1 << ' ' << all_min_score_ans[i][2]+1 << ' ' << all_min_score_ans[i][3]+1 << ' ' << all_min_score_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