CF527C TJ
hongye123456 · · 个人记录
题意
切割一块 H 表示横着切,V 表示竖着切。问每次切割后最大的那块玻璃有多大。
解题思路
multiset<long long> x,y;
用
multiset<long long> lx,ly;
用 V)切的位置,H)切的位置。
每次循环时:
- 如果输入的是
V ,pos 。
假如我们竖着切
pos 这个位置。 那么我们直接让lx.insert(pos)。然后找到pos 小和比pos 大的两个位置l,r ,显然我们是切割x 中边界l,r 的玻璃,它们的宽为r-l 。
- 如果输入的是
H ,pos 。
同理。每次切割一次,都会删除一个
x 或者y 中的一个宽度或者高度,然后产生两个新的宽度或者高度。
代码
#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;
}