Posted on

Description

Submission

  • The size of each container should be unchanged
  • The balls move until for each container, there is only one kind of ball inside
#include <bits/stdc++.h>

using namespace std;

// Complete the organizingContainers function below.
string organizingContainers(vector<vector<int>> container) {
    int n = container.size();
    // how many balls in each container
    vector<int> c(n);
    vector<int> t(n);
    for(int i = 0; i < n; ++i) {
        c[i] = 0;
        t[i] = 0;
        // how many balls of each kind
        for(int j = 0; j < n; ++j) {
            t[i] += container[j][i];
            c[i] += container[i][j];
        }
    }
    sort(c.begin(), c.end());
    sort(t.begin(), t.end());
    
    // In the end, one kind of ball should be in one container 
    if(c == t) return "Possible";
    return "Impossible"; 
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    int q;
    cin >> q;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    for (int q_itr = 0; q_itr < q; q_itr++) {
        int n;
        cin >> n;
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        vector<vector<int>> container(n);
        for (int i = 0; i < n; i++) {
            container[i].resize(n);

            for (int j = 0; j < n; j++) {
                cin >> container[i][j];
            }

            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        string result = organizingContainers(container);

        fout << result << "\n";
    }

    fout.close();

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *