CF1819B

· · 题解

Link

题意

一个长方形经过多次裁剪后得到一堆小长方形,求原长方形的长和宽。

思路

一开始的长方形会被砍一刀,切成两个小长方形 a 和 b。a 之后就放着不动了,b 继续递归裁剪。

注意到第一次得到的长方形 a,它的长或者宽一定是原长方形的长或宽的其中一个。

再进一步,它的长或者宽一定是最大的,这样我们就可以得到两种情况。设 a 长 x,宽 y:

  1. 长为 x,宽为 \dfrac{S}{x}

  2. 长为 \dfrac{S}{y},宽为 y

我们会发现情况只有这两种。然后还要判断一下总面积 S 是不是整除。

接下来就可以模拟了。拿第一种情况来讨论,初始长为 x,宽为 \dfrac{S}{x},我们就可以找有哪些小长方形的长为 x,并且之前未被使用,这样我们就可以减去这个小长方形的宽。

宽无处可减之后又找有没有宽匹配的,继续减长。以此类推,直到最后长或者宽等于零。

可以用 map 或者 set 实现。

代码



#include <bits/stdc++.h>
using namespace std;

#define int long long


int read() {
    char s = getchar();
    while (s < '0' ||s > '9') {
        s = getchar();
    }int sum = 0;
    while (s >= '0' && s <= '9') {
        sum = sum * 10 + s - '0';
        s = getchar();
    }return sum;
}

pair <int, int> a[200005];

map <pair <int, int>, int, greater<> > mp1, t1;//greater使长或宽从大到小排序,mp1存储的是{长,宽}
map <pair <int, int>, int, greater<> > mp2, t2;//mp2存储的是宽,长}

signed main() {

    int t;
    cin >> t;
    while (t--) {
        int n = read();
        mp1.clear();//CF特性,多测清空
        mp2.clear();
        int S = 0, mxa = 0, mxb = 0;
        for (int i = 1; i <= n; i++) {
            a[i].first = read();
            a[i].second = read();
            mp1[{a[i].first, a[i].second}]++;
            mp2[{a[i].second, a[i].first}]++;
            S += a[i].first * a[i].second;//计算总面积
            mxa = max(mxa, a[i].first);//如上所述,找最大值
            mxb = max(mxb, a[i].second);
        }

        t1 = mp1, t2 = mp2;//做备份
        vector <pair <int, int > > ans;
        if (S % mxa == 0) {
            int nowa = mxa, nowb = S / mxa, flag = 1;
            while (1) {

                if (mp1.begin()->first.first != nowa) {//没有与之匹配的
                    flag = 0;
                    break;
                }
                while (mp1.begin()->first.first == nowa) {//都删完
                    nowb -= mp1.begin()->first.second;
                    mp2[{mp1.begin()->first.second, nowa}]--;
                    if (!mp2[{mp1.begin()->first.second, nowa}]) {//mp2与mp1同步删除
                        mp2.erase({mp1.begin()->first.second, nowa});
                    }
                    mp1[{nowa, mp1.begin()->first.second}]--;
                    if (!mp1[{nowa, mp1.begin()->first.second}]) {
                        mp1.erase({nowa, mp1.begin()->first.second});
                    }
                }
                if (!nowa || !nowb)break;//出现0了就跳出


                if (mp2.begin()->first.first != nowb) {//宽的删除与长相同
                    flag = 0;
                    break;
                }
                while (mp2.begin()->first.first == nowb) {
                    nowa -= mp2.begin()->first.second;
                    if (!--mp1[{mp2.begin()->first.second, nowb}]) {
                        mp1.erase({mp2.begin()->first.second, nowb});
                    }
                    if (!--mp2[{nowb, mp2.begin()->first.second}]) {
                        mp2.erase({nowb, mp2.begin()->first.second});
                    }
                }
                if (!nowa || !nowb)break;
            }
            if (flag) {
                ans.push_back({mxa, S / mxa});
            }
        }
        mp1 = t1;
        mp2 = t2;
        if (S % mxb == 0) {//另一种情况,大致相同
            int nowa = S / mxb, nowb = mxb, flag = 1;
            while (1) {
                if (mp2.begin()->first.first != nowb) {//注意这里是先删宽相同的,可以想想为什么
                    flag = 0;
                    break;
                }
                while (mp2.begin()->first.first == nowb) {
                    nowa -= mp2.begin()->first.second;
                    if (!--mp1[{mp2.begin()->first.second, nowb}]) {
                        mp1.erase({mp2.begin()->first.second, nowb});
                    }
                    if (!--mp2[{nowb, mp2.begin()->first.second}]) {
                        mp2.erase({nowb, mp2.begin()->first.second});
                    }
                }
                if (!nowa || !nowb)break;


                if (mp1.begin()->first.first != nowa) {
                    flag = 0;
                    break;
                }
                while (mp1.begin()->first.first == nowa) {
                    nowb -= mp1.begin()->first.second;
                    if (!--mp2[{mp1.begin()->first.second, nowa}]) {
                        mp2.erase({mp1.begin()->first.second, nowa});
                    }
                    if (!--mp1[{nowa, mp1.begin()->first.second}]) {
                        mp1.erase({nowa, mp1.begin()->first.second});
                    }
                }
                if (!nowa || !nowb)break;

            }
            if (flag) {
                ans.push_back({S / mxb, mxb});
            }
        }
        if (ans.size() == 2 && ans[0] == ans[1]) {//如果两种情况相同要特判一下
            printf("1\n%lld %lld\n", ans[0].first, ans[0].second);
            continue;
        }
        printf("%d\n", ans.size());
        for (auto i : ans) {
            printf("%lld %lld\n", i.first, i.second);
        }
    }




    return 0;
}

upd:笔者代码都挤在主函数里了……可以分函数来写。