Submission #1183294

#TimeUsernameProblemLanguageResultExecution timeMemory
1183294steveonalexCutting a Rectangle (BOI24_rectangle)C++20
100 / 100
206 ms11400 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(abs(a), abs(b));}
ll lcm(ll a, ll b){return abs(a) / gcd(a, b) * abs(b);}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ull mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}

template <class T1, class T2>
    bool maximize(T1 &a, T2 b){
        if (a < b) {a = b; return true;}
        return false;
    }

template <class T1, class T2>
    bool minimize(T1 &a, T2 b){
        if (a > b) {a = b; return true;}
        return false;
    }

template <class T>
    void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

void solve(){
    int n; cin >> n;
    vector<pair<ll, ll>> a(n);
    for(int i = 0; i < n; ++i) cin >> a[i].first >> a[i].second;
    sort(ALL(a));

    ll area = 0;
    for(auto i: a) area += 1LL * i.first * i.second;

    vector<ll> candidate;
    ll ma = 0;
    for(pair<int, int> i: a) maximize(ma, i.second);

    if (area % ma == 0) candidate.push_back(ma);

    if (ma == 1){
        candidate.push_back(a.back().first);
    }
    else{
        for(pair<int, int> i: a){
            if (i.first <= ma) continue;
            if (area % i.first == 0) candidate.push_back(i.first);
        }
    }


    for(ll &i: candidate) {
        minimize(i, area / i);
    }
    remove_dup(candidate);
    vector<ll> ans;
    for(ll i: candidate){
        ll goy = i;
        ll j = area / i;
        multiset<pair<int, int>> S1, S2;
        for(pair<int, int> i: a){
            S1.insert(i);
            S2.insert(make_pair(i.second, i.first));
        }

        bool check = true;
        for(int it = 1; it <= n; ++it){
            if (i < j) swap(i, j);

            pair<int, int> ma = {-1, -1};
            for(int x = 0; x <= 1; ++x){
                for(int y = 0; y<= 1; ++y){
                    auto it = S1.lower_bound(make_pair(i+1, -1));
                    if (it != S1.begin()) {
                        it--;

                        if ((*it).first == i){
                            pair<int, int> cur = (*it);
                            maximize(ma, cur);
                        }   
                    }
                    swap(S1, S2);
                }
                swap(i, j);
            }

            if (ma.first == -1){
                check = false;
                break;
            }

            bool cur_found = false;
            for(int x = 0; x <= 1; ++x){
                for(int y = 0; y<= 1; ++y){
                    auto it = S1.lower_bound(make_pair(i+1, -1));
                    if (it != S1.begin()){
                        it--;
                        if ((*it).first == i){
                            pair<int, int> cur = (*it);
                            if (cur == ma){
                                j -= cur.second;
                                S1.erase(S1.find(cur));
                                swap(cur.first, cur.second);
                                S2.erase(S2.find(cur));
                                cur_found = true;
                                break;
                            }
                        }

                    }
                    swap(S1, S2);
                }
                if (cur_found) break;
                swap(i, j);
            }
        }

        if (check) ans.push_back(goy);
    }
    cout << ans.size() << "\n";
    for(int i: ans) cout << i << "\n";
}

int main(void){
    ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);

    clock_t start = clock();

    solve();

    cerr << "Time elapsed: " << clock() - start << "ms!\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...