結果

問題 No.929 よくあるボールを移動するやつ
ユーザー snrnsidy
提出日時 2025-09-21 01:27:13
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,116 bytes
コンパイル時間 1,843 ms
コンパイル使用メモリ 200,228 KB
実行使用メモリ 7,716 KB
最終ジャッジ日時 2025-09-21 01:27:17
合計ジャッジ時間 2,958 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 5 WA * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

int n;
int a[100001];
long long int res = 0;
set <int> S;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];

    for(int i=1;i<=n;i++)
    {
        if(a[i] > 1) S.insert(i);
    }

    for(int i=1;i<=n;i++)
    {
        if(a[i]==0)
        {
            auto it = S.lower_bound(i);
            int Min = 1e9;
            int idx = -1;
            if(it!=S.begin())
            {
                it--;
                int d = abs(i - *it);
                if(Min > d)
                {
                    Min = d;
                    idx = *it;
                }
                it++;
            }
            if(it!=S.end())
            {
                int d = abs(i - *it);
                if(Min > d)
                {
                    Min = d;
                    idx = *it;
                }                
            }
            a[idx]-=1;
            a[i] = 1;
            res += Min;
            if(a[idx]==1) S.erase(idx);
        }
    }

    cout << res << '\n';

    return 0;
}
0