POJ2482 Stars in Your Window
Posted autoint
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ2482 Stars in Your Window相关的知识,希望对你有一定的参考价值。
题意
Language:
Stars in Your Window
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15036 | Accepted: 4061 |
Description
Fleeting time does not blur my memory of you. Can it really be 4 years since I first saw you? I still remember, vividly, on the beautiful Zhuhai Campus, 4 years ago, from the moment I saw you smile, as you were walking out of the classroom and turned your head back, with the soft sunset glow shining on your rosy cheek, I knew, I knew that I was already drunk on you. Then, after several months’ observation and prying, your grace and your wisdom, your attitude to life and your aspiration for future were all strongly impressed on my memory. You were the glamorous and sunny girl whom I always dream of to share the rest of my life with. Alas, actually you were far beyond my wildest dreams and I had no idea about how to bridge that gulf between you and me. So I schemed nothing but to wait, to wait for an appropriate opportunity. Till now — the arrival of graduation, I realize I am such an idiot that one should create the opportunity and seize it instead of just waiting.
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.
Farewell, my princess!
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.
Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.
These days, having parted with friends, roommates and classmates one after another, I still cannot believe the fact that after waving hands, these familiar faces will soon vanish from our life and become no more than a memory. I will move out from school tomorrow. And you are planning to fly far far away, to pursue your future and fulfill your dreams. Perhaps we will not meet each other any more if without fate and luck. So tonight, I was wandering around your dormitory building hoping to meet you there by chance. But contradictorily, your appearance must quicken my heartbeat and my clumsy tongue might be not able to belch out a word. I cannot remember how many times I have passed your dormitory building both in Zhuhai and Guangzhou, and each time aspired to see you appear in the balcony or your silhouette that cast on the window. I cannot remember how many times this idea comes to my mind: call her out to have dinner or at least a conversation. But each time, thinking of your excellence and my commonness, the predominance of timidity over courage drove me leave silently.
Graduation, means the end of life in university, the end of these glorious, romantic years. Your lovely smile which is my original incentive to work hard and this unrequited love will be both sealed as a memory in the deep of my heart and my mind. Graduation, also means a start of new life, a footprint on the way to bright prospect. I truly hope you will be happy everyday abroad and everything goes well. Meanwhile, I will try to get out from puerility and become more sophisticated. To pursue my own love and happiness here in reality will be my ideal I never desert.
Farewell, my princess!
If someday, somewhere, we have a chance to gather, even as gray-haired man and woman, at that time, I hope we can be good friends to share this memory proudly to relight the youthful and joyful emotions. If this chance never comes, I wish I were the stars in the sky and twinkling in your window, to bless you far away, as friends, to accompany you every night, sharing the sweet dreams or going through the nightmares together.
Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a location (x, y). for each star, there is a grade ranging from 1 to 100, representing its brightness, where 100 is the brightest and 1 is the weakest. The window is a rectangle whose edges are parallel to the x-axis or y-axis. Your task is to tell where I should put the window in order to maximize the sum of the brightness of the stars within the window. Note, the stars which are right on the edge of the window does not count. The window can be translated but rotation is not allowed.
Input
There are several test cases in the input. The first line of each case contains 3 integers: n, W, H, indicating the number of stars, the horizontal length and the vertical height of the rectangle-shaped window. Then n lines follow, with 3 integers each: x, y, c, telling the location (x, y) and the brightness of each star. No two stars are on the same point.
There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
There are at least 1 and at most 10000 stars in the sky. 1<=W,H<=1000000, 0<=x,y<2^31.
Output
For each test case, output the maximum brightness in a single line.
Sample Input
3 5 4 1 2 3 2 3 2 6 3 1 3 5 4 1 2 3 2 3 2 5 3 1
Sample Output
5 6
Source
POJ Contest,Author:[email protected]
分析
不想吐槽出题人了。
考虑该矩形右上角的坐标,每个星星相当于给一个矩形的权值加了c,那么就是求最大的区域的点权和。
由于边界上的星星不算,所以把每个星星像左边和下面各移动0.5,那么覆盖这个星星的矩形坐标范围为(x,y)到(x+w-1,y+h-1)这个矩形。
时间复杂度\(O(n \log n)\)
代码
#include<iostream>
#include<algorithm>
#define rg register
#define il inline
#define co const
template<class T>il T read(){
rg T data=0,w=1;rg char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') w=-1;ch=getchar();}
while(isdigit(ch)) data=data*10+ch-'0',ch=getchar();
return data*w;
}
template<class T>il T read(rg T&x) {return x=read<T>();}
typedef long long ll;
using namespace std;
co int N=1e4+1;
int n;
struct P{
unsigned int x,y,z;
int c;
bool operator<(co P&w)co{
return x<w.x||(x==w.x&&c<w.c);
}
}a[N*2];
unsigned w,h,b[N*2];
struct T{
int l,r,ans,add;
}t[N*8];
void build(int p,int l,int r){
t[p].l=l,t[p].r=r;
t[p].ans=t[p].add=0;
if(l==r) return;
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
}
void spread(int p){
t[p<<1].add+=t[p].add,t[p<<1].ans+=t[p].add;
t[p<<1|1].add+=t[p].add,t[p<<1|1].ans+=t[p].add;
t[p].add=0;
}
void change(int p,int l,int r,int c){
if(l<=t[p].l&&t[p].r<=r) return t[p].add+=c,t[p].ans+=c,void();
if(t[p].add) spread(p);
int mid=t[p].l+t[p].r>>1;
if(l<=mid) change(p<<1,l,r,c);
if(r>mid) change(p<<1|1,l,r,c);
t[p].ans=max(t[p<<1].ans,t[p<<1|1].ans);
}
void Stars_in_Your_Window(){
for(int i=1;i<=n;++i){
int k=i<<1;
read(a[k-1].x),read(a[k-1].y),read(a[k-1].c);
a[k].x=a[k-1].x+w;
b[k-1]=a[k].y=a[k-1].y;
b[k]=a[k].z=a[k-1].z=a[k].y+h-1;
a[k].c=-a[k-1].c;
}
n<<=1;
sort(b+1,b+n+1);
int m=unique(b+1,b+n+1)-(b+1);
for(int i=1;i<=n;++i){
a[i].y=lower_bound(b+1,b+m+1,a[i].y)-b;
a[i].z=lower_bound(b+1,b+m+1,a[i].z)-b;
}
sort(a+1,a+n+1);
build(1,1,m);
int ans=0;
for(int i=1;i<=n;++i){
change(1,a[i].y,a[i].z,a[i].c);
ans=max(ans,t[1].ans);
}
printf("%d\n",ans);
}
int main(){
// freopen(".in","r",stdin),freopen(".out","w",stdout);
while(~scanf("%d%d%d",&n,&w,&h)) Stars_in_Your_Window();
return 0;
}
以上是关于POJ2482 Stars in Your Window的主要内容,如果未能解决你的问题,请参考以下文章
POJ2482 Stars in Your Window 题解
POJ 2482 Stars in Your Window(线段树+离散化+扫描线)
Stars in Your Window POJ - 2482