a183簡單流程模擬、時間與狀態更新

TOI 2021/11 新手組:帶動商機

用舊人數計算一天所有增加量,再一起更新。

前往題目練習 →回報教學錯誤店數 K、天數 N:時間 O(NK),空間 O(K)。

這題在學什麼

第一列以 0 結束,0 不屬於商店;其後的整數是天數。

每家店當作影響來源:端點向唯一較少的鄰店增加自身人數的 10%;內部向每個較少的鄰店各增加 5%。

next 先複製當天人數,所有比較與百分比都讀舊陣列,完成一天後替換;單店無鄰居便不增加。

Step-by-step animation

帶動商機・同步更新

回報動畫錯誤
步驟 1 / 4

今日人數

兩端影響比例 10%,內部影響比例 5%。

40
12
80
15
29
天數
0

Reference solution

回報程式錯誤

完整程式

#include <iostream>
#include <vector>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<long long> a;
    long long x;
    while (cin >> x && x)
        a.push_back(x);
    int days;
    cin >> days;
    int k = (int)a.size();
    while (days--) {
        auto b = a;
        for (int i = 0; i < k; i++) {
            long long inc = a[i] / ((i == 0 || i == k - 1) ? 10 : 20);
            for (int j : {i - 1, i + 1})
                if (j >= 0 && j < k && a[i] > a[j])
                    b[j] += inc;
        }
        a = b;
    }
    for (int i = 0; i < k; i++)
        cout << (i ? " " : "") << a[i];
    cout << '\n';
}

先試著自己完成,再切換語言比對解法。參考程式可複製到題目編輯器。

更多