結果

問題 No.5020 Averaging
ユーザー arimattiarimatti
提出日時 2024-02-25 23:48:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,521 bytes
コンパイル時間 3,169 ms
コンパイル使用メモリ 218,972 KB
実行使用メモリ 6,548 KB
スコア 0
最終ジャッジ日時 2024-02-25 23:49:35
合計ジャッジ時間 53,351 ms
ジャッジサーバーID
(参考情報)
judge10 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 WA -
testcase_46 WA -
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
template<typename T> using vc = vector<T>;
template<typename T> using vv = vc<vc<T>>;
template<typename T> using PQ = priority_queue<T,vc<T>,greater<T> >;
template<typename T>void uni(T& a){sort(rng(a));a.erase(unique(rng(a)),a.end());}
using ll = long long;
using P = pair<int,int>;
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
constexpr int INF = 1001001001;
constexpr ll INFL = 1001002003004005006ll;


inline unsigned long long xor128() {
	static unsigned long long rx = 123456789, ry = 986960440, rz = 738905609, rw = 23140692;
	unsigned long long rt = (rx ^ (rx << 11));
	rx = ry;ry = rz;rz = rw;
	return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}
inline float rand128(float min = 0, float max = 1){
	return (float)(xor128() % 0xFFFF) / 0xFFFF * (max - min) + min;
}
inline unsigned long long randint(int min, int max){
	return xor128()%(max - min + 1) + min;
}

// 時間をDouble型で管理し、経過時間も取り出せるクラス
class TimeKeeper {
 private:
  std::chrono::high_resolution_clock::time_point start_time_;
  double time_threshold_;

  double now_time_ = 0;

 public:
  TimeKeeper(const double time_threshold)
    : start_time_(std::chrono::high_resolution_clock::now()),
      time_threshold_(time_threshold) {}
  void setNowTime() {
    auto diff = std::chrono::high_resolution_clock::now() - this->start_time_;
    this->now_time_ =
      std::chrono::duration_cast<std::chrono::microseconds>(diff).count() *
      1e-3;  // ms
  }
  double getNowTime() const { return this->now_time_; }
  bool isTimeOver() const { return now_time_ >= time_threshold_; }
};

int used[55][55];

// グローバル変数
int n; // 頂点数
vector<ll> ga;
vector<ll> gb;
void read_input() {
  cin >> n;
  ga.resize(n);
  gb.resize(n);
  rep(i,n) cin >> ga[i] >> gb[i];
}

struct Output {
  int u,v;
  Output (int u=0, int v=0) : u(u), v(v) {}
};

struct State {
  vector<ll> a;
  vector<ll> b;  
  vector<Output> ans;

  State() {
    a.resize(n);
    b.resize(n);
    a = ga;
    b = gb;
  }
  
  void init() {
  }

  void make_init_ans() {
    rep(i,n) {
      if (i == 0) continue;
      int idx1 = 0;
      int idx2 = i;
      ans.push_back(Output(idx1,idx2));
    }
  }

  void transition() {
    int m = ans.size();
    int idx1 = randint(0,m-2);
    int idx2 = randint(0,m-1);
    if (idx1 == idx2) idx1++;
    swap(ans[idx1],ans[idx2]);
  }

  void print_ans() {
    cout << ans.size() << endl;
    for (Output o : ans) {
      cout << o.u << " " << o.v << endl;
    }
  }


  ll calc_score() {
    auto aa = a;
    auto bb = b;
    rep(i,ans.size()) {
      int idx1 = ans[i].u;
      int idx2 = ans[i].v;
      ll va = (aa[idx1]+aa[idx2])/2;
      ll vb = (bb[idx1]+bb[idx2])/2;
      aa[idx1] = aa[idx2] = va;
      bb[idx1] = bb[idx2] = vb;
    }
    ll V1 = abs(aa[0]-5e17);
    ll V2 = abs(bb[0]-5e17);
    return 2000000 - 100000*log10(max(V1,V2)+1);
  }

};

void simulatedAnnealing() {
  TimeKeeper timer(950);
  State state;
  state.make_init_ans();
  state.calc_score();
  ll score = state.calc_score();
  ll best_score = score;
  
  double time_limit = 950;
  double start_temp = 1000.0;
  double end_temp = 10.0;
  auto best_state = state;
  int iter_count = 0;  

  while (true) {
    timer.setNowTime();
    if (timer.isTimeOver()) break;

    iter_count++;
    State next_state = state;
    next_state.transition();
    ll next_score = next_state.calc_score();

    // score maximumize
    double temp = start_temp + (end_temp - start_temp) * timer.getNowTime() / time_limit;
    double probability = exp((next_score - score) / temp);
    bool is_force_next = probability > rand128();      

    if (next_score > score || is_force_next) {
      score = next_score;
      state = next_state;
    }

    if (next_score > best_score) {
      best_score = next_score;
      best_state = next_state;
      cerr << "best score: " << best_score << endl;
    }
    
  }
  best_state.print_ans();
  cerr << "best_score:" << "\t" << best_score << endl;
  cerr << "iter_count:" << "\t" << iter_count << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  read_input();
  simulatedAnnealing();
  return 0;
}
0