結果

問題 No.2465 Dilated Water Simulation
ユーザー 👑 NachiaNachia
提出日時 2023-08-18 05:27:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 3,130 bytes
コンパイル時間 896 ms
コンパイル使用メモリ 84,300 KB
実行使用メモリ 15,064 KB
最終ジャッジ日時 2024-05-05 11:41:29
合計ジャッジ時間 5,245 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 56 ms
11,008 KB
testcase_06 AC 51 ms
10,880 KB
testcase_07 AC 57 ms
11,008 KB
testcase_08 AC 52 ms
11,008 KB
testcase_09 AC 58 ms
10,880 KB
testcase_10 AC 53 ms
11,008 KB
testcase_11 AC 54 ms
11,008 KB
testcase_12 AC 51 ms
11,008 KB
testcase_13 AC 56 ms
11,008 KB
testcase_14 AC 53 ms
10,880 KB
testcase_15 AC 49 ms
11,008 KB
testcase_16 AC 54 ms
10,880 KB
testcase_17 AC 49 ms
10,880 KB
testcase_18 AC 52 ms
11,008 KB
testcase_19 AC 51 ms
11,008 KB
testcase_20 AC 53 ms
14,168 KB
testcase_21 AC 54 ms
14,176 KB
testcase_22 AC 55 ms
14,428 KB
testcase_23 AC 54 ms
14,168 KB
testcase_24 AC 57 ms
14,176 KB
testcase_25 AC 56 ms
15,064 KB
testcase_26 AC 58 ms
14,936 KB
testcase_27 AC 57 ms
14,044 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using i64 = long long;
    
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    i64 l; cin >> l;
    vector<i64> v(l);
    for(int i=0; i<l; i++) cin >> v[i];
    i64 n; cin >> n;
    i64 ncycles = n / l;

    vector<i64> gv(l, v[0]);
    for(int i=1; i<l; i++) gv[i] = min(gv[i-1], v[i]);
    i64 minv = gv[l-1];
    for(int i=0; i<l; i++) v[i] -= minv;
    for(int i=0; i<l; i++) gv[i] -= minv;

    vector<pair<i64, i64>> height_length;
    vector<i64> imos1(l+1);
    vector<i64> imos0(l);
    i64 mask = ((i64)1 << 30) - 1;

    auto contribute = [&](i64 pos, i64 t, i64 weight) {
        if(t + pos <= ncycles){
            imos0[pos] += weight * (ncycles - t);
            imos0[pos] &= mask;
            imos1[pos] -= weight;
            imos1[pos] &= mask;
        }
        else if(t <= ncycles){
            i64 decrease_len = ncycles - t;
            imos0[pos] += weight * (ncycles - t);
            imos0[pos] &= mask;
            imos1[pos] -= weight;
            imos1[pos] &= mask;
            imos1[pos - decrease_len] += weight;
            imos1[pos - decrease_len] &= mask;
        }
    };

    for(int i=l-1; i>=0; i--){
        if(gv[i] == 0) continue;

        i64 rem = v[i];
        i64 turn = 1;

        while(!height_length.empty() && rem >= gv[i]){
            auto [h, len] = height_length.back();
            height_length.pop_back();
            contribute(i, turn, -h);
            contribute(i, turn + len, h);

            if(len == 0) continue;
            if(h == gv[i]){
                turn += len;
                continue;
            }

            i64 hturn = (rem - gv[i]) / (gv[i] - h) + 1;
            if(hturn >= len){
                turn += len;
                rem -= len * (gv[i] - h);
            }
            else{
                rem -= hturn * (gv[i] - h);
                contribute(i, turn + hturn, h);
                contribute(i, turn + len, -h);
                turn += hturn;
                height_length.push_back({ h, len - hturn });
                break;
            }
        }

        turn += rem / gv[i];
        rem %= gv[i];
        if(turn > ncycles + 100) turn = ncycles + 100;

        contribute(i, turn - 1, rem);
        contribute(i, turn, -rem);
        height_length.push_back({ rem, 1 });
        turn -= 1;
        
        contribute(i, 0, gv[i]);
        contribute(i, turn, -gv[i]);
        height_length.push_back({ gv[i], turn });
    }

    for(int i=l-1; i>=0; i--) imos1[i] = (imos1[i] + imos1[i+1]) & mask;
    for(int i=0; i<l; i++) imos0[i] = (imos0[i] + imos1[i+1]) & mask;
    imos0[0] = v[0];

    vector<i64> ans(l);
    i64 rem = v[0];
    for(int i=l-1; i>=0; i--){
        i64 x = min(imos0[i], rem);
        rem -= x;
        ans[i] = x;
    }
    ans[0] += minv;

    for(i64 i=0; i<(n%l); i++){
        i64 f = min(ans[i], v[i+1] + minv - ans[i+1]);
        ans[i] -= f;
        ans[i+1] += f;
    }

    for(int i=0; i<l; i++){
        if(i) cout << ' ';
        cout << ans[i];
    }
    cout << '\n';

    return 0;
}
0