POJ 2482 Stars in Your Window【扫描线+离散化】
题目:
View Code
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.
题意: 作者的女友有一个窗户,从这个窗户里可以看到天上的星星,每个星星都有固定的坐标和亮度,知道了窗户的长和宽,问如何放置窗户的位置可以使得从窗户中看到的
星星的总亮度最大。
分析: 离散化坐标,把每个星星(x,y)亮度为C ,分成两个点 A(x,y,c),B(x,y,-c)
将坐标映射到 y 轴,扫描的时候如果遇到 A 点,说明A 进入扫描的范围内,遇到B
则说明这颗星星从窗户中移出,找到覆盖值最大的线段即为答案。
#include<stdio.h> #include<string.h> #include<stdlib.h> #define clr(x)memset(x,0,sizeof(x)) #define maxn 100005 long long max(long long a,long long b) { return a>b?a:b; } struct node { long long l,r,mid; long long val,add; }tree[8*maxn]; struct seg { long long x,y,val; }s[2*maxn]; long long Y[2*maxn]; int cmp1(const void*p1,const void*p2) { return *(long long*)p1-*(long long*)p2; } int cmp2(const void*p1,const void*p2) { struct seg *c=(seg*)p1; struct seg *d=(seg*)p2; if(c->x!=d->x) return c->x-d->x; return c->val-d->val; } void creat(long long l,long long r,long long rt) { tree[rt].l=l; tree[rt].r=r; tree[rt].mid=(l+r)/2; if(l==r) return; long long m=(l+r)/2; creat(l,m,rt*2); creat(m+1,r,rt*2+1); } void update(long long L,long long R,long long ad,long long rt) { if(L<=tree[rt].l&&tree[rt].r<=R) { tree[rt].add+=ad; tree[rt].val+=ad; return; } if(tree[rt].add) { tree[rt*2].add+=tree[rt].add; tree[rt*2].val+=tree[rt].add; tree[rt*2+1].add+=tree[rt].add; tree[rt*2+1].val+=tree[rt].add; tree[rt].add=0; } long long m=(tree[rt].l+tree[rt].r)/2; if(L<=m) update(L,R,ad,rt*2); if(R>m) update(L,R,ad,rt*2+1); tree[rt].val=max(tree[rt*2].val,tree[rt*2+1].val); } long long b_search(long long key,long long n) { long long m,l=1,r=n; while(l<=r) { m=(l+r)/2; if(Y[m]==key) return m; else if(Y[m]>key) r=m-1; else l=m+1; } return -1; } int main() { long long i,m,l,r,res,n,w,h; while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF) { for(i=1;i<=n;i++) { scanf("%lld%lld%lld",&s[i].x,&s[i].y,&s[i].val); s[i+n]=s[i]; s[i+n].x=s[i].x+w; s[i+n].val=-s[i+n].val; Y[i]=s[i].y; Y[i+n]=s[i].y+h; } qsort(Y+1,2*n,sizeof(Y[0]),cmp1); qsort(s+1,2*n,sizeof(s[0]),cmp2); m=1; for(i=1;i<=2*n;i++) if(Y[i]!=Y[m]) Y[++m]=Y[i]; creat(1,m,1); res=0; for(i=1;i<=2*n;i++) { l=b_search(s[i].y,m); r=b_search(s[i].y+h,m); update(l,r-1,s[i].val,1); res=max(res,tree[1].val); } printf("%lld\n",res); } return 0; }



浙公网安备 33010602011771号