Click Here to go back to the homepage.

Gerrymandering Solution:


#include <bits/stdc++.h>

using namespace std;

int main() {

    int precincts, districts;
    cin >> precincts >> districts;
    int votes_per_district[1000][2] = {};
    double total_votes {};
    while (precincts--) {
        int district_num;
        cin >> district_num;
        int partyA, partyB;
        cin >> partyA >> partyB;
        total_votes += partyA + partyB;
        votes_per_district[district_num-1][0] += partyA;
        votes_per_district[district_num-1][1] += partyB;
    }
    //postive for A, negative for B
    int wasted {};

    for_each(&votes_per_district[0], &votes_per_district[districts], [&wasted](int district[2]){
        int aWasted{}, bWasted{};
        if(district[0] > (district[0] + district[1]) * 0.5){
            aWasted = district[0] - ((int) std::ceil(((district[0] + district[1]) * 0.5) + 0.2));
            bWasted = district[1];
            cout << "A " << aWasted << " " << bWasted << endl;

        } else {
            aWasted = district[0];
            bWasted = district[1]  - ((int) std::ceil(((district[0] + district[1]) * 0.5) + 0.2));
            cout << "B " << aWasted << " " << bWasted << endl;
        }
        wasted += aWasted - bWasted;
    });
    cout << setprecision(7) << fixed << abs(wasted) / total_votes;

    return 0;
}