結果
| 問題 | No.3 ビットすごろく |
| コンテスト | |
| ユーザー |
commy
|
| 提出日時 | 2019-03-29 19:45:43 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 3 ms / 5,000 ms |
| コード長 | 1,499 bytes |
| コンパイル時間 | 801 ms |
| コンパイル使用メモリ | 82,784 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-01 09:16:32 |
| 合計ジャッジ時間 | 1,826 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 33 |
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <utility>
#include <tuple>
#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#ifdef _DEBUG_
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl
#else
#define dump(val)
#endif
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
template<typename T>
vector<T> make_v(size_t a, T b) {
return vector<T>(a, b);
}
template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}
int popcount(int n) {
int cnt = 0;
while (n) {
cnt += (n % 2);
n /= 2;
}
return cnt;
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
const int inf = 1 << 30;
vector<int> cost(n + 1, inf);
cost[1] = 1;
cost[0] = -1;
queue<P> q;
q.emplace(1, 1);
int d[2] = {-1, 1};
while (q.size()) {
int c, pos;
tie(c, pos) = q.front();
q.pop();
if (cost[pos] < c) {
continue;
}
int len = popcount(pos);
REP(k, 0, 2) {
int a = pos + d[k] * len;
if (a <= 0 || a > n) continue;
if (cost[a] > c + 1) {
cost[a] = c + 1;
q.emplace(cost[a], a);
}
}
}
if (cost[n] == inf) {
cost[n] = -1;
}
cout << cost[n] << endl;
return 0;
}
commy