b749簡單

兩人退選後的票流

忠實重算所有兩人退選的獨立選舉,處理零票與同票。

解題思路

先按第一順位計票,按票多、編號小選原勝者。m≤9,因此排除原勝者後最多只有 C(8,2)=28 個退選對;逐對清空票數,對每張票取前三順位中第一個未退選者。三個順位互異且只刪兩人,必定能找到。

每次在所有未退選候選人中重新比較,包含零票者。如此每個退選對的模擬恰好實現定義,沒有跨情境的累積污染。依 x 由小到大、再 y 由小到大枚舉,第一次改變勝者的對就是所求字典序最小者;若從未改變則保留 0 0。

時間 O(m²(n+m)),空間 O(n+m);最多 28 次重算、每次至多 6000 張票。Python split 的 O(n) token 及票單也計入空間。

Step-by-step animation

逐步推演(例子條件見步驟說明)

回報動畫錯誤
步驟 1 / 3

原始票數

自訂三票 (1,2,3)、(2,3,1)、(3,2,1)。

1
1
1
原勝者
1

Reference solution

回報程式錯誤

完整程式

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
    int m, n;
    cin >> m >> n;
    vector<array<int, 3>> a(n);
    vector<int> cnt(m + 1);
    for (auto &v : a) {
        for (int &x : v)
            cin >> x;
        cnt[v[0]]++;
    }
    int win = 1;
    for (int i = 2; i <= m; i++)
        if (cnt[i] > cnt[win])
            win = i;
    int ans = 0, bx = 0, by = 0;
    for (int x = 1; x <= m; x++)
        for (int y = x + 1; y <= m; y++)
            if (x != win && y != win) {
                fill(cnt.begin(), cnt.end(), 0);
                for (auto v : a)
                    for (int z : v)
                        if (z != x && z != y) {
                            cnt[z]++;
                            break;
                        }
                int w = 0;
                for (int z = 1; z <= m; z++)
                    if (z != x && z != y && (!w || cnt[z] > cnt[w]))
                        w = z;
                if (w != win) {
                    if (!ans)
                        bx = x, by = y;
                    ans++;
                }
            }
    cout << win << ' ' << ans << ' ' << bx << ' ' << by << '\n';
}

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

更多