結果

問題 No.3252 Constrained Moving
ユーザー Anh Nguyễn Ngọc Tuấn
提出日時 2025-09-05 21:37:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 51 ms / 2,000 ms
コード長 1,132 bytes
コンパイル時間 2,529 ms
コンパイル使用メモリ 206,832 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-05 21:38:00
合計ジャッジ時間 4,926 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:19:17: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   19 |         freopen (TASKNAME".inp", "r", stdin);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:20:17: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |         freopen (TASKNAME".out", "w", stdout);
      |         ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>

using namespace std;

const int N = 5 + 1e5;
const int inf = 7 + 1e9;

int main() {
#define TASKNAME ""
    ios_base :: sync_with_stdio (0);
    cin.tie (0);
    if ( fopen( TASKNAME".inp", "r" ) ) {
        freopen (TASKNAME".inp", "r", stdin);
        freopen (TASKNAME".out", "w", stdout);
    }
    int n, s, t, k;
    cin >> n >> s >> t >> k;
    vector <int> a(n + 1, 0);
    vector <II> b;
    for (int i = 1; i <= n; i ++) {
        cin >> a[i];
        if (i != s)
            b.push_back({a[i], i});
    }
    sort(b.begin(), b.end(), greater <> ());
    vector <int> ans(n + 1, inf);
    queue <int> q;
    q.push(s);
    ans[s] = 0;
    while (!q.empty()) {
        int u = q.front(); q.pop();
        int val = k - a[u];
        while (!b.empty() && b.back().st <= val) {
            int v = b.back().nd;
            b.pop_back();
            q.push(v);
            ans[v] = ans[u] + 1;
        }
    }
    cout << (ans[t] < inf ? ans[t] : -1);
    return 0;
}
0