记录一下一开始没想通的地方

关键在于分清几种情况:

n == 2且两数相等	输出"0 0"//特判
mx >= tot - mx	最后剩下的只能是最大值
mx < tot - mx	tot为偶数且a[i] == 1时不能是最后剩下的那个//关键
#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1e5 + 10;

void solve()
{
    int n; cin >> n;
    vector<int> a(n);
    
    LL tot = 0;
    int mx = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        tot += a[i];
        mx = max(mx, a[i]);
    }
    
    if (n == 2 && a[0] == a[1]) { //特判只有两个数相等情况
        cout << "0 0" << endl;
        return ;
    }
    if (mx >= tot - mx)
    {
        for (int i = 0; i < n; i++)
        {
            if (a[i] == mx) cout << "1 ";
            else cout << "0 ";
        }
        cout << endl;
        return ;
    }
    else {
        for (int i = 0; i < n; i++)
        {
            if (a[i] == 1 && tot % 2 == 0) cout << "0 ";
            else cout << "1 ";
        }
        cout << endl;
        return ;
    }
}

int main()
{
    int T; cin >> T;
    
    while (T--) solve();
    
    return 0;
}