跳到正文

个人记录

CF527C TJ

hongye123456约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟

版本

  1. v1当前

题意

切割一块 W×HW \times H 的玻璃,H 表示横着切,V 表示竖着切。问每次切割后最大的那块玻璃有多大。

解题思路

C++
multiset<long long> x,y;

用 xx 存所有碎片的宽,yy 存所有碎片的高。

C++
multiset<long long> lx,ly;

用 lxlx 存竖着(V)切的位置,lyly 存横着(H)切的位置。

每次循环时:

  • 如果输入的是 VV,pospos。

假如我们竖着切 pospos 这个位置。 那么我们直接让 lx.insert(pos)。然后找到 pospos 小和比 pospos 大的两个位置 l,rl,r,显然我们是切割 xx 中边界 l,rl,r 的玻璃,它们的宽为 r−lr-l。

  • 如果输入的是 HH,pospos。

同理。每次切割一次,都会删除一个 xx 或者 yy 中的一个宽度或者高度,然后产生两个新的宽度或者高度。

代码

C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int w,h,n;
multiset<ll> x,y;//存储所有碎片的边长
multiset<ll> lx,ly;//存储切割的位置
multiset<ll>::iterator it;
int main(){
    scanf("%d%d%d",&w,&h,&n);
    //最开始是一整块玻璃
    x.insert(w);
    y.insert(h);
    lx.insert(0);lx.insert(w);
    ly.insert(0);ly.insert(h);

    while(n--){
        char ch;
        ll pos;
        cin>>ch>>pos;
        if(ch=='H'){
            ll l,r;
            ly.insert(pos);
            it=ly.find(pos);
            it--;//找到pos前一个切割位置
            l=*it;
            it++;it++;//找到pos后一个切割位置
            r=*it;
            //删除一个高度r-l,增加两个新高度
            it=y.find(r-l);
            y.erase(it);
            y.insert(r-pos);
            y.insert(pos-l);
        }else{
            ll l,r;
            lx.insert(pos);
            it=lx.find(pos);
            it--;//找到pos前一个切割位置
            l=*it;
            it++;it++;//找到pos后一个切割位置
            r=*it;
            //删除一个宽度r-l,增加两个新宽度
            it=x.find(r-l);
            x.erase(it);
            x.insert(r-pos);
            x.insert(pos-l);
        }
        ll r,c;
        it=x.end();it--;
        r = *it;
        it=y.end();it--;
        c = *it;
        printf("%lld\n",r*c);
    }

    return 0;
}
原文