#53615: 找不到原因 score 85%


yeice2022@gmail.com (磺冰)

學校 : 不指定學校
編號 : 305635
來源 : [123.194.240.65]
最後登入時間 :
2025-09-22 21:40:40

為何沒加while跳過空房間會導致錯誤:

 

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define Pll pair<ll, ll>
#define FF first
#define SS second

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    ll n, t;
    cin >> n >> t;
    vector<ll> sand(n + 1, 0);
    for (ll i=0; i<n; i++) {
        cin >> sand[i];
    }

    vector<ll> prev(n+1), next(n+1);
    iota(prev.begin()+1, prev.end(), 0);
    iota(next.begin(), next.end()-1, 1);
    prev[0]=n;

    priority_queue<Pll, vector<Pll>, greater<Pll>> pq;

    for (ll i = 0; i < n; ++i) {
        if (sand[i] > 0) {
            pq.emplace(sand[i], i);
        }    
    }

    ll total = 0;

    while (!pq.empty()) {
        auto p = pq.top();
        pq.pop();
        ll idx=p.SS, cnt=p.FF;
        if (sand[idx] == 0 || sand[idx] != cnt) continue;
        if (cnt > t) break;

        ll next_idx = next[idx];

        // 這裡沒有寫就只有85%
        while(next_idx < n && sand[next_idx] == 0) {
          next_idx = next[next_idx];
        }

        sand[idx] = 0;
        next[prev[idx]] = next_idx;
        prev[next_idx] = prev[idx];
        total += cnt;

        if (next_idx < n) {
            sand[next_idx] += cnt;
            pq.emplace(sand[next_idx], next_idx);
        }
    }

    cout << total;
}