結果

問題 No.5020 Averaging
ユーザー 👑 NachiaNachia
提出日時 2024-02-25 16:40:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 7,367 bytes
コンパイル時間 1,541 ms
コンパイル使用メモリ 129,972 KB
実行使用メモリ 6,676 KB
スコア 41,672,252
最終ジャッジ日時 2024-02-25 16:42:04
合計ジャッジ時間 40,149 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 727 ms
6,548 KB
testcase_01 WA -
testcase_02 AC 695 ms
6,548 KB
testcase_03 AC 747 ms
6,548 KB
testcase_04 AC 695 ms
6,548 KB
testcase_05 AC 725 ms
6,548 KB
testcase_06 AC 703 ms
6,548 KB
testcase_07 AC 726 ms
6,548 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 696 ms
6,548 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 701 ms
6,548 KB
testcase_15 AC 697 ms
6,548 KB
testcase_16 AC 699 ms
6,548 KB
testcase_17 AC 698 ms
6,548 KB
testcase_18 AC 723 ms
6,548 KB
testcase_19 AC 699 ms
6,548 KB
testcase_20 AC 693 ms
6,548 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 698 ms
6,548 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 700 ms
6,548 KB
testcase_28 AC 697 ms
6,548 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 724 ms
6,548 KB
testcase_32 WA -
testcase_33 AC 700 ms
6,548 KB
testcase_34 WA -
testcase_35 AC 706 ms
6,548 KB
testcase_36 WA -
testcase_37 AC 702 ms
6,548 KB
testcase_38 AC 726 ms
6,548 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 696 ms
6,548 KB
testcase_43 AC 696 ms
6,548 KB
testcase_44 AC 701 ms
6,548 KB
testcase_45 WA -
testcase_46 AC 693 ms
6,548 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#define NDEBUG
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <queue>
#include <array>
#include <cmath>
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(i64 i=0; i<(i64)(n); i++)
#define repr(i,n) for(i64 i=(i64)(n)-1; i>=0; i--)
const i64 INF = 5001001001001001001;
const char* yn(bool x){ return x ? "Yes" : "No"; }
template<typename A> void chmin(A& l, const A& r){ if(r < l) l = r; }
template<typename A> void chmax(A& l, const A& r){ if(l < r) l = r; }
template<typename A> using nega_queue = std::priority_queue<A,std::vector<A>,std::greater<A>>;
using namespace std;

const i64 TARGET_INT = 500'000'000'000'000'000;
const int OFFSET_SIZE = 6;

#include <unordered_map>
#include <cassert>
#include <cstdint>


namespace nachia{

class Xoshiro256pp{
public:

    using i32 = int32_t;
    using u32 = uint32_t;
    using i64 = int64_t;
    using u64 = uint64_t;


private:
    uint64_t s[4];

    // https://prng.di.unimi.it/xoshiro256plusplus.c
    static inline uint64_t rotl(const uint64_t x, int k) noexcept {
        return (x << k) | (x >> (64 - k));
    }
    inline uint64_t gen(void) noexcept {
        const uint64_t result = rotl(s[0] + s[3], 23) + s[0];
        const uint64_t t = s[1] << 17;
        s[2] ^= s[0];
        s[3] ^= s[1];
        s[1] ^= s[2];
        s[0] ^= s[3];
        s[2] ^= t;
        s[3] = rotl(s[3], 45);
        return result;
    }

    // https://xoshiro.di.unimi.it/splitmix64.c
    u64 splitmix64(u64& x) {
        u64 z = (x += 0x9e3779b97f4a7c15);
        z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
        z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
        return z ^ (z >> 31);
    }

public:

    void seed(u64 x = 7001){
        assert(x != 0);
        s[0] = x;
        for(int i=1; i<4; i++) s[i] = splitmix64(x);
    }

    Xoshiro256pp(){ seed(); }
    
    u64 rng64() { return gen(); }
    u64 operator()(){ return gen(); }

    // generate x : l <= x <= r
    u64 random_unsigned(u64 l,u64 r){
        assert(l<=r);
        r-=l;
        auto res = rng64();
        if(res<=r) return res+l;
        u64 d = r+1;
        u64 max_valid = 0xffffffffffffffff/d*d;
        while(true){
            auto res = rng64();
            if(res<=max_valid) break;
        }
        return res%d+l;
    }

    // generate x : l <= x <= r
    i64 random_signed(i64 l,i64 r){
        assert(l<=r);
        u64 unsigned_l = (u64)l ^ (1ull<<63);
        u64 unsigned_r = (u64)r ^ (1ull<<63);
        u64 unsigned_res = random_unsigned(unsigned_l,unsigned_r) ^ (1ull<<63);
        return (i64)unsigned_res;
    }


    // permute x : n_left <= x <= n_right
    // output r from the front
    template<class Int>
    std::vector<Int> random_nPr(Int n_left, Int n_right, Int r){
        Int n = n_right-n_left;

        assert(n>=0);
        assert(r<=(1ll<<27));
        if(r==0) return {};  
        assert(n>=r-1);

        std::vector<Int> V;
        std::unordered_map<Int,Int> G;
        for(int i=0; i<r; i++){
            Int p = random_signed(i,n);
            Int x = p - G[p];
            V.push_back(x);
            G[p] = p - (i - G[i]);
        }

        for(Int& v : V) v+=n_left;
        return V;
    }



    // V[i] := V[perm[i]]
    // using swap
    template<class E,class PermInt_t>
    void permute_inplace(std::vector<E>& V,std::vector<PermInt_t> perm){
        assert(V.size() == perm.size());
        int N=V.size();
        for(int i=0; i<N; i++){
            int p=i;
            while(perm[p]!=i){
                assert(0 <= perm[p] && perm[p] < N);
                assert(perm[p] != perm[perm[p]]);
                std::swap(V[p],V[perm[p]]);
                int pbuf = perm[p]; perm[p] = p; p = pbuf;
            }
            perm[p] = p;
        }
    }

    template<class E>
    std::vector<E> shuffle(const std::vector<E>& V){
        int N=V.size();
        auto P = random_nPr(0,N-1,N);
        std::vector<E> res;
        res.reserve(N);
        for(int i=0; i<N; i++) res.push_back(V[P[i]]);
        return res;
    }

    // shuffle using swap
    template<class E>
    void shuffle_inplace(std::vector<E>& V){
        int N=V.size();
        permute_inplace(V,random_nPr(0,N-1,N));
    }


};

} // namespace nachia
#include <random>
#include <chrono>
nachia::Xoshiro256pp rng;
namespace RngInitInstance { struct RngInit { RngInit(){
    unsigned long long seed1 = std::random_device()();
    unsigned long long seed2 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
    rng.seed(seed1 ^ seed2);
} } a; }

int main(){
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<i64> A(N), B(N);
    rep(i,N) cin >> A[i] >> B[i];
    i64 minWeight = INF;
    vector<int> minWeightPerm(N);
    vector<pair<int,int>> offset;
    auto WeightFromAB = [](i64 a, i64 b){
        i64 ad = abs(a - TARGET_INT);
        i64 bd = abs(b - TARGET_INT);
        return max(ad, bd) * 2 + ad + bd;
    };
    auto MakeMove = [&](int a, int b){
        A[a] = A[b] = (A[a]+A[b])/2;
        B[a] = B[b] = (B[a]+B[b])/2;
        offset.push_back({a,b});
    };
    while(offset.size() < OFFSET_SIZE){
        {
            int argmin = 1;
            i64 minw = INF;
            for(int i=1; i<N; i++){
                i64 w = WeightFromAB((A[0]+A[i])/2, (B[0]+B[i])/2);
                if(minw > w){ argmin = i; minw = w; }
            }
            if(A[argmin] != A[0] || B[argmin] != B[0]){
                MakeMove(0, argmin);
                continue;
            }
        }
        if((int)offset.size()+2 > OFFSET_SIZE) break;
        {
            i64 minw = INF;
            int ma = 1, mb = 2;
            for(int a=1; a<N; a++) for(int b=a+1; b<N; b++){
                i64 qa = (A[a]+A[b])/2;
                i64 qb = (B[a]+B[b])/2;
                i64 w = WeightFromAB((A[0]+qa)/2, (B[0]+qb)/2);
                if(minw > w){ ma = a; mb = b; minw = w; }
            }
            MakeMove(ma, mb);
            MakeMove(0, mb);
        }
    }
    cerr << "A[0] = " << A[0] << endl;
    cerr << "B[0] = " << B[0] << endl;
    rep(i,N) minWeightPerm[i] = N-1-i;
    vector<int> Q = minWeightPerm;
    vector<int> Qp = minWeightPerm;
    auto evalQ = [&]() -> i64 {
        i64 a = A[Q[0]];
        i64 b = B[Q[0]];
        for(int i=1; i<N; i++){
            a = (a + A[Q[i]]) / 2;
            b = (b + B[Q[i]]) / 2;
        }
        i64 w = WeightFromAB(a, b);
        if(w < minWeight){
            copy(Q.begin(), Q.end(), minWeightPerm.begin());
            minWeight = w;
        }
        return w;
    };
    evalQ();
    int q = 50 - OFFSET_SIZE;
    rep(t,500){
        for(int i=1; i<N-1; i++){
            int j = ((rng.rng64() >> 32) * i) >> 32;
            swap(Q[j], Q[i]);
        }
        i64 w = evalQ();
        rep(ss,20) for(int i=0; i<N-1; i++){
            for(int j=i+1; j<N-1; j++){
                swap(Q[i], Q[j]);
                i64 nw = evalQ();
                if(w < nw) swap(Q[i], Q[j]); else w = nw;
            }
        }
    }
    Q = minWeightPerm;
    cerr << minWeight << endl;
    vector<pair<int,int>> ans;
    rep(i,OFFSET_SIZE) ans.push_back(offset[i]);
    rep(i,N-1) ans.push_back({ Q[i], Q[i+1] });
    cout << ans.size() << '\n';
    for(auto [u,v] : ans) cout << (u+1) << ' ' << (v+1) << '\n';
    return 0;
}
0