置頂

我的 VPython 教學文件 (HackMD 版本)

VPython 教學文件目錄 安裝及測試 基本語法 等速度直線運動 自由落下 終端速度 水平抛射 使用For迴圈計算水平抛射資料 斜向抛射 圓周運動 簡諧運動 單擺 木塊彈簧系統分離 重力及簡諧 行星運動 相疊木塊 雙重簡諧運動 一維彈性碰撞 ...

熱門文章

2026年11月30日 星期一

ZeroJudge 解題筆記:t232.瘋狂水世界,啟動!

作者:王一哲
日期:2026年11月30日


ZeroJudge 題目連結:t232.瘋狂水世界,啟動!

解題想法


這題看起來很簡單,開一個長度為 $5$ 的陣列 $values$ 儲存 $4$ 個象限各自的物資價值加總,接下來只要計算每一列測資給的兩條直線 $y = a x + b, y = c x + d$ 交點 $(x, y)$,依照 $x, y$ 的正負號將物資價值 $e$ 加到 $values$ 之中對應的索引值即可。如果 $a = c$ 代表兩條直線平行或重合,跳過這筆測資。對兩條方程式解聯立可得交點坐標 $$ a x + b = c x + d ~\Rightarrow~ (a - c) x = d - b ~\Rightarrow~ x = \frac{d - b}{a - c} $$ 再將 $x$ 代入其中一條直線方程式可得 $$ y = a \times \frac{d - b}{a - c} + b = \frac{ad - ab + ab - bc}{a - c} = \frac{ad - bc}{a - c} $$

但其實這題藏了一些陷阱,有些測資的交點坐標很接近 $x$ 軸或 $y$ 軸,如果用浮點數或倍精數計算 $x, y$ 會有誤差,無法正確地判斷交點所在的象限。因此這題最好用整數格式,將分子、分母分開處理,由上式可知 $x$ 的分子 $num_x = d - b$,$y$ 的分子 $num_y = a d - b c$,兩者的分母都是 $den = a - c$。如果 $num_x = 0$ 或 $num_y = 0$,代表交點在軸上,跳過這筆測資。接下來用 $num_x, num_y, den$ 是否大於 $0$ 來判斷交點所在的象限,這裡會有另一個陷阱,如果用 C++ 解題,即使用 long long 格式儲存 $num_x, num_y, den$,計算 $num_x \times den, num_y \times den$ 仍然會溢位,不能用兩者的乘積判斷正負號。我是改用 bool 值及 XOR 判斷乘積的正負號,如果 $num_x > 0, den > 0$ 或 $num_x < 0, den < 0$,兩者相乘才會是正值;如果 $num_x, den$ 一正、一負,兩者相乘為負值。因此 $x, y$ 是否大於 $0$ 可以這樣寫
px = not((num_x > 0) ^ (den > 0))
py = not((num_y > 0) ^ (den > 0))
加上 not 是因為我比較習慣大於 0 的值為 True。最後用 $px, py$ 為 True 或 False 的組合判斷 $x, y$ 所在的象限。

Python 程式碼


使用時間約為 0.4 s,記憶體約為 82.9 MB,通過測試。
def solve():
    import sys

    result = []
    data = sys.stdin.read().split()
    ptr = 0
    while ptr < len(data):
        n = int(data[ptr])  # n 個訊號
        ptr += 1
        values = [0] * 5  # 各象限的物資價值,上限為 2E5 * 1E9
        for _ in range(n):
            a, b, c, d, e = map(int, data[ptr : ptr + 5])
            ptr += 5
            if a == c: continue  # 平行或重合,跳過
            num_x = d - b  # a*x + b = c*x + d,x 的分子
            den = a - c  # x, y 的分母
            num_y = a * d - b * c  # y 的分子
            
            if num_x == 0 or num_y == 0: continue  # 交點在 x 軸或 y 軸上

            px, py = not((num_x > 0) ^ (den > 0)), not((num_y > 0) ^ (den > 0))  # 只要取正負號
            
            if px and py: values[1] += e    # 第一象限
            elif not px and py: values[2] += e  # 第二象限
            elif not px and not py: values[3] += e  # 第三象限
            elif px and not py: values[4] += e  # 第四象限

        ans, imax = 1, 0  # 物資價值最高的象限預設為 1,最高物資價值
        for i in range(1, 5):
            if values[i] > imax:
                ans = i
                imax = values[i]
        
        result.append(f"{ans:d} {imax:d}\n")
    sys.stdout.write("".join(result))

if __name__ == "__main__":
    solve()


C++ 程式碼


使用時間約為 87 ms,記憶體約為 3.6 MB,通過測試。
#include <iostream>
#include <vector>
typedef long long LL;
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n;  // n 個訊號
    vector<LL> values;  // 各象限的物資價值,上限為 2E5 * 1E9
    while(cin >> n) {
        values.assign(5, 0);
        for(int i = 0; i < n; i++) {
            LL a, b, c, d, e;  // y = a*x + b, y = c*x + d, 價值 e
            cin >> a >> b >> c >> d >> e;
            
            if (a == c) continue;  // 平行或重合,跳過
            
            LL num_x = d - b, den = a - c;  // a*x + b = c*x + d,x 的分子,x, y 的分母
            LL num_y = a * d - b * c;  // y 的分子
            
            if (num_x == 0 || num_y == 0) continue;  // 交點在 x 軸或 y 軸上
            
            // 用 bool 取交點 x, y 的正負號,分子、分母同號為 true,異號為 false
            // 不能用分子、分母相乘取正負號,這樣會超出 long long 的上限
            bool px = !((num_x > 0) ^ (den > 0)), py = !((num_y > 0) ^ (den > 0));
            
            if (px && py) values[1] += e;  // 第一象限
            else if (!px && py) values[2] += e;  // 第二象限
            else if (!px && !py) values[3] += e;  // 第三象限
            else if (px && !py) values[4] += e;  // 第四象限
        }

        int ans = 1;  // 物資價值最高的象限,預設值 1
        LL imax = 0;  // 最高物資價值,要設定成 long long 的最小值
        for(int i = 1; i <= 4; i++) {
            if (values[i] > imax) {
                ans = i;
                imax = values[i];
            }
        }
        cout << ans << " " << imax << "\n";
    }
    return 0;
}


C 語言程式碼


使用時間約為 96 ms,記憶體約為 1.6 MB,通過測試。
#include <stdio.h>
#include <string.h>
typedef long long LL;

int main() {
    int n;  // n 個訊號
    LL values[5];  // 各象限的物資價值,上限為 2E5 * 1E9
    while(scanf("%d", &n) != EOF) {
        memset(values, 0, sizeof(values));
        for(int i = 0; i < n; i++) {
            LL a, b, c, d, e;  // y = a*x + b, y = c*x + d, 價值 e
            scanf("%lld %lld %lld %lld %lld", &a, &b, &c, &d, &e);
            
            if (a == c) continue;  // 平行或重合,跳過
            
            LL num_x = d - b, den = a - c;  // a*x + b = c*x + d,x 的分子,x, y 的分母
            LL num_y = a * d - b * c;  // y 的分子
            
            if (num_x == 0 || num_y == 0) continue;  // 交點在 x 軸或 y 軸上
            
            // 用 bool 取交點 x, y 的正負號,分子、分母同號為 true,異號為 false
            // 不能用分子、分母相乘取正負號,這樣會超出 long long 的上限
            int px = !((num_x > 0) ^ (den > 0)), py = !((num_y > 0) ^ (den > 0));
            
            if (px && py) values[1] += e;  // 第一象限
            else if (!px && py) values[2] += e;  // 第二象限
            else if (!px && !py) values[3] += e;  // 第三象限
            else if (px && !py) values[4] += e;  // 第四象限
        }

        int ans = 1;  // 物資價值最高的象限,預設值 1
        LL imax = 0;  // 最高物資價值,要設定成 long long 的最小值
        for(int i = 1; i <= 4; i++) {
            if (values[i] > imax) {
                ans = i;
                imax = values[i];
            }
        }
        printf("%d %lld\n", ans, imax);
    }
    return 0;
}


沒有留言:

張貼留言