题目大意
在一个天空中有很多星星(看作平面直角坐标系),已知每颗星星的坐标和亮度(都是整数)。求用宽为w,高为h(h,w为整数)能圈住星星的亮度总和最大是多少(边界上的星星不算)
解题思路
由于矩形大小固定,所以我们只需要考虑矩形右上角顶点在什么位置能圈住的星星亮度最大。
对于一个星星
(
x
,
y
,
c
)
(x, y, c)
(x,y,c),
x
,
y
x,y
x,y为坐标,
c
c
c 为亮度,我们发现,能圈住这颗星星的矩形右上角坐标构成的区域也是一个矩形,由于边界上的星星不算,所以我们可以把星星整体往左下角移动一小段距离,所以能圈住星星的区域矩形的坐标为
(
x
,
y
)
(x, y)
(x,y) 右上角坐标为
(
x
+
w
−
1
,
y
+
h
−
1
)
(x+w-1,y+h-1)
(x+w−1,y+h−1) 这时候边界也算在内。
这样我们的问题转化为了:平面上有若干个区域,每个区域带有一个权值,求在哪个坐标上重叠的区域权值最大。每个区域的权值大小由星星的亮度决定。
在转化后的问题中,我们可以使用扫描线算法,取出每个区域的左右边界构成四元组
(
x
,
y
,
y
+
h
−
1
,
c
)
(x, y, y+h-1, c)
(x,y,y+h−1,c)和
(
x
+
w
,
y
+
h
−
1
,
−
c
)
(x+w, y+h-1, -c)
(x+w,y+h−1,−c)把这些四元组按照横坐标排序,这里有个小细节,当横坐标相同时,我们要按照第四维坐标从小到大排序。
我们建立一棵线段树来维护区间最大值,同时我们支持区间加的操作,可以用lazy标记来维护。
代码
#include <bits/stdc++.h>
#define ll long long
#define qc ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define fi first
#define se second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define pb push_back
using namespace std;
const int MAXN = 2e4 + 7;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll mod = 1e9 + 7;
inline int read()
{
int x=0,f=1;char ch=getchar();
while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
struct node{
ll x, y1, y2, k;
bool operator<(node b) const{
if(x == b.x)
return k < b.k;
return x < b.x;
}
};
int n, h, w;
struct Tree{
int l, r;
ll add;
ll sum;
}tree[MAXN << 2];
void pushup(int rt){
tree[rt].sum = max(tree[rt << 1].sum, tree[rt << 1 | 1].sum);
}
void build(int l, int r, int rt){
tree[rt].l = l;
tree[rt].r = r;
tree[rt].add = 0;
if(l == r){
tree[rt].sum = 0;
return ;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m+1, r, rt << 1 | 1);
pushup(rt);
}
void pushdown(int rt, int len){
if(tree[rt].add){
tree[rt << 1].add += tree[rt].add;
tree[rt << 1 | 1].add += tree[rt].add;
tree[rt << 1].sum += tree[rt].add;
tree[rt << 1 | 1].sum += tree[rt].add;
tree[rt].add = 0;
}
}
void update(int L, int R, ll x, int rt){
if(L <= tree[rt].l && tree[rt].r <= R){
tree[rt].add += x;
tree[rt].sum += x;
return ;
}
pushdown(rt, tree[rt].r - tree[rt].l + 1);
int m = (tree[rt].l + tree[rt].r) >> 1;
if(L <= m)
update(L, R, x, rt << 1);
if(m < R)
update(L, R, x, rt << 1 | 1);
pushup(rt);
}
ll ls[MAXN];
int lscnt;
ll als[MAXN << 1];
int ask(ll x){
return lower_bound(ls+1, ls+1+lscnt, x) - ls;
}
void solve(){
vector<node> v;
set<ll> st;
int cnt = 0;
for(int i = 1; i <= n; i++){
ll x, y, k;
cin >> x >> y >> k;
v.pb(node{x, y, y+h-1, k});
v.pb(node{x+w, y, y+h-1, -k});
als[++cnt] = y;
als[++cnt] = y+h-1;
}
sort(als+1, als+1+cnt);
sort(v.begin(), v.end());
ls[0] = -inf;
lscnt = 0;
for(int i = 1; i <= cnt; i++)
if(als[i] != als[i-1])
ls[++lscnt] = als[i];
build(1, lscnt, 1);
ll ans = 0;
for(auto& it : v){
update(ask(it.y1), ask(it.y2), it.k, 1);
ans = max(ans, tree[1].sum);
}
cout << ans << endl;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
qc;
int T;
// cin >> T;
T = 1;
while(T--){
while(cin >> n >> w >> h)
solve();
}
return 0;
}
利用扫描线算法解决最大矩形覆盖星星问题

该博客介绍了如何解决一个几何优化问题,即在给定宽度和高度的矩形内,找到能包含星星的最大亮度总和。通过将问题转化为寻找重叠区域权值最大的问题,并应用扫描线算法和线段树,实现了动态维护区间最大值。代码中展示了详细的解题思路和实现过程。

被折叠的 条评论
为什么被折叠?



