結果

問題 No.2596 Christmas Eve (Heuristic ver.)
ユーザー FplusFplusFFplusFplusF
提出日時 2023-12-24 16:46:36
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,204 ms / 1,224 ms
コード長 5,853 bytes
コンパイル時間 4,246 ms
コンパイル使用メモリ 282,568 KB
実行使用メモリ 6,676 KB
スコア 4,978,502
最終ジャッジ日時 2023-12-24 16:49:22
合計ジャッジ時間 166,379 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,203 ms
6,676 KB
testcase_01 AC 1,204 ms
6,676 KB
testcase_02 AC 1,203 ms
6,676 KB
testcase_03 AC 1,204 ms
6,676 KB
testcase_04 AC 1,203 ms
6,676 KB
testcase_05 AC 1,204 ms
6,676 KB
testcase_06 AC 1,204 ms
6,676 KB
testcase_07 AC 1,204 ms
6,676 KB
testcase_08 AC 1,203 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,203 ms
6,676 KB
testcase_14 AC 1,204 ms
6,676 KB
testcase_15 AC 1,203 ms
6,676 KB
testcase_16 AC 1,203 ms
6,676 KB
testcase_17 AC 1,203 ms
6,676 KB
testcase_18 AC 1,203 ms
6,676 KB
testcase_19 AC 1,203 ms
6,676 KB
testcase_20 AC 1,204 ms
6,676 KB
testcase_21 AC 1,203 ms
6,676 KB
testcase_22 AC 1,204 ms
6,676 KB
testcase_23 AC 1,204 ms
6,676 KB
testcase_24 AC 1,203 ms
6,676 KB
testcase_25 AC 1,203 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,203 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,203 ms
6,676 KB
testcase_34 AC 1,203 ms
6,676 KB
testcase_35 AC 1,203 ms
6,676 KB
testcase_36 AC 1,203 ms
6,676 KB
testcase_37 AC 1,203 ms
6,676 KB
testcase_38 AC 1,203 ms
6,676 KB
testcase_39 AC 1,204 ms
6,676 KB
testcase_40 AC 1,204 ms
6,676 KB
testcase_41 AC 1,204 ms
6,676 KB
testcase_42 AC 1,204 ms
6,676 KB
testcase_43 AC 1,203 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,203 ms
6,676 KB
testcase_47 AC 1,203 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,204 ms
6,676 KB
testcase_53 AC 1,204 ms
6,676 KB
testcase_54 AC 1,203 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,204 ms
6,676 KB
testcase_58 AC 1,203 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,204 ms
6,676 KB
testcase_62 AC 1,203 ms
6,676 KB
testcase_63 AC 1,204 ms
6,676 KB
testcase_64 AC 1,204 ms
6,676 KB
testcase_65 AC 1,203 ms
6,676 KB
testcase_66 AC 1,203 ms
6,676 KB
testcase_67 AC 1,203 ms
6,676 KB
testcase_68 AC 1,204 ms
6,676 KB
testcase_69 AC 1,204 ms
6,676 KB
testcase_70 AC 1,203 ms
6,676 KB
testcase_71 AC 1,203 ms
6,676 KB
testcase_72 AC 1,204 ms
6,676 KB
testcase_73 AC 1,204 ms
6,676 KB
testcase_74 AC 1,203 ms
6,676 KB
testcase_75 AC 1,203 ms
6,676 KB
testcase_76 AC 1,204 ms
6,676 KB
testcase_77 AC 1,203 ms
6,676 KB
testcase_78 AC 1,204 ms
6,676 KB
testcase_79 AC 1,203 ms
6,676 KB
testcase_80 AC 1,204 ms
6,676 KB
testcase_81 AC 1,203 ms
6,676 KB
testcase_82 AC 1,203 ms
6,676 KB
testcase_83 AC 1,204 ms
6,676 KB
testcase_84 AC 1,203 ms
6,676 KB
testcase_85 AC 1,203 ms
6,676 KB
testcase_86 AC 1,204 ms
6,676 KB
testcase_87 AC 1,202 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,204 ms
6,676 KB
testcase_91 AC 1,203 ms
6,676 KB
testcase_92 AC 1,204 ms
6,676 KB
testcase_93 AC 1,204 ms
6,676 KB
testcase_94 AC 1,204 ms
6,676 KB
testcase_95 AC 1,203 ms
6,676 KB
testcase_96 AC 1,204 ms
6,676 KB
testcase_97 AC 1,203 ms
6,676 KB
testcase_98 AC 1,204 ms
6,676 KB
testcase_99 AC 1,203 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,203 ms
6,676 KB
testcase_103 AC 1,202 ms
6,676 KB
testcase_104 AC 1,204 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,203 ms
6,676 KB
testcase_108 AC 1,204 ms
6,676 KB
testcase_109 AC 1,203 ms
6,676 KB
testcase_110 AC 1,203 ms
6,676 KB
testcase_111 AC 1,204 ms
6,676 KB
testcase_112 AC 1,203 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,204 ms
6,676 KB
testcase_116 AC 1,203 ms
6,676 KB
testcase_117 AC 1,204 ms
6,676 KB
testcase_118 AC 1,204 ms
6,676 KB
testcase_119 AC 1,204 ms
6,676 KB
testcase_120 AC 1,203 ms
6,676 KB
testcase_121 AC 1,204 ms
6,676 KB
testcase_122 AC 1,204 ms
6,676 KB
testcase_123 AC 1,204 ms
6,676 KB
testcase_124 AC 1,203 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;
        double min_score=score(ans);
        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(1200<=ms) break;
            vector<vector<int>> new_ans=ans;
            pii mx={-INF,-INF},mn={INF,INF};
            rep(i,K){
                int sum=0;
                rep(j,4){
                    sum+=tree[min(2,j)][ans[i][j]].h;
                }
                chmax(mx,{sum,i});
                chmin(mn,{sum,i});
            }
            int x=mx.second,y=mt()%4;
            if(mt()%2==0) x=mn.second;
            if(mt()%3==0){
                int p=mt()%K,q=y;
                if(x==p) continue;
                if(2<=y) q=2+mt()%2;
                swap(new_ans[x][y],new_ans[p][q]);
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][new_ans[x][i+1]].w<=tree[min(2,i)][new_ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                rep(i,3){
                    if(tree[min(2,i+1)][new_ans[p][i+1]].w<=tree[min(2,i)][new_ans[p][i]].w){
                        ok=false;
                        break;
                    }
                }
                double new_score=score(new_ans);
                if(ok&&new_score<=min_score){
                    min_score=new_score;
                    ans=new_ans;
                }else{
                    swap(new_ans[x][y],new_ans[p][q]);
                }
            }else{
                int z=mt()%not_used[min(2,y)].size();
                swap(new_ans[x][y],not_used[min(2,y)][z]);
                bool ok=true;
                rep(i,3){
                    if(tree[min(2,i+1)][new_ans[x][i+1]].w<=tree[min(2,i)][new_ans[x][i]].w){
                        ok=false;
                        break;
                    }
                }
                double new_score=score(new_ans);
                if(ok&&new_score<=min_score){
                    min_score=new_score;
                    ans=new_ans;
                }else{
                    swap(new_ans[x][y],not_used[min(2,y)][z]);
                }
            }
        }
        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