Skip to content

Commit 827d725

Browse files
committed
add chokudai search
1 parent 5b324ba commit 827d725

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

docs/marathon/chokudai_search.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- `add_next_states` で状態遷移を定義する
2+
- 状態およびそのターン数が与えられるので、次の状態およびそのターン数を決定し、 `register_state` を使って状態を登録
3+
- メモリ制約に引っかからないように、各ターンで持つ状態数に上限を設けることが可能
4+
- `set_max_num_states` で設定する
5+
- Interval Heap を使って、あふれた状態を消している
6+
7+
AHC032 で利用実績あり。
8+
[https://atcoder.jp/contests/ahc032/submissions/61302590](https://atcoder.jp/contests/ahc032/submissions/61302590)

marathon/chokudai_search.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* @brief chokudai サーチ
3+
* @docs docs/marathon/chokudai_search.md
4+
*/
5+
6+
#include "interval_heap.cpp"
7+
8+
template <class State, class Timer>
9+
class ChokudaiSearch {
10+
public:
11+
ChokudaiSearch() : num_iter_(-1) {}
12+
void init(int max_turns, double search_time, int time_check_iter) {
13+
states_.resize(max_turns + 1);
14+
search_time_ = search_time;
15+
num_iter_ = 0;
16+
time_check_iter_ = time_check_iter;
17+
max_num_states_.resize(max_turns, 1 << 30);
18+
}
19+
void set_max_num_states(int max_num_state) {
20+
fill(max_num_states_.begin(), max_num_states_.end(), max_num_state);
21+
}
22+
void set_max_num_states(const vector<int> &max_num_states) {
23+
assert(max_num_states.size() == max_num_states_.size());
24+
max_num_states_ = max_num_states;
25+
}
26+
void register_state(int turns, const State &state) {
27+
states_[turns].emplace(state);
28+
while(states_[turns].size() > max_num_states_[turns]) {
29+
states_[turns].pop_max();
30+
}
31+
}
32+
void search(Timer &timer, const auto &add_next_states) {
33+
assert(num_iter_ >= 0);
34+
const double start_time = timer.getTime();
35+
for(num_iter_ = 0;; num_iter_++) {
36+
for(int turns = 0; turns + 1 < (int)states_.size(); turns++) {
37+
if(timer.getTime() - start_time > search_time_) {
38+
goto TIME_OVER;
39+
}
40+
if(states_[turns].size() == 0) continue;
41+
State state = states_[turns].top_min();
42+
states_[turns].pop_min();
43+
add_next_states(turns, state);
44+
}
45+
}
46+
TIME_OVER:;
47+
fprintf(stderr, "chokudai search: num_iter = %d\n", num_iter_);
48+
}
49+
const State &get_best_state() const { return states_.back().top_min(); }
50+
51+
private:
52+
double search_time_;
53+
int num_iter_;
54+
int time_check_iter_;
55+
vector<IntervalHeap<State>> states_;
56+
vector<int> max_num_states_;
57+
};

0 commit comments

Comments
 (0)