题目来源: Poj
基准时间限制:2 秒 空间限制:131072 KB 分值: 160 难度:6级算法题
 收藏
 取消关注
整点上有N颗星星,每颗星星有一个亮度。用一个平行于x轴和y轴,宽为W高为H的方框去套星星。套住的所有星星的亮度之和为S(包括边框上的星星),求S的最大值。

Input
第1行:共3个数N, W, H,中间用空格分割,N为星星的数量,W为方框的宽度,H为方框的高度。(2 <= N <= 50000, 1 <= W, H <= 10^9)
第2 - N + 1行:每行3个数,X, Y, L,中间用空格分隔,分别表示星星的横坐标X,纵坐标Y,以及星星的亮度L。(1 <= X, Y <= 10^9,1 <= L <= 10000)
Output
输出方框能够套住的最大亮度和S。
Input示例
6 3 3
1 1 2
2 2 3
3 3 4
4 4 3
5 5 2
6 6 1
Output示例
12

最近学习线段树,觉得线段树的代码真的是好长啊~,另外线段树的应用范围是真广啊~

能A掉这道题特别开心,这个题目要直接套星星的话不知道怎么套,所以把星星转化成矩形,把矩形转化成小空间或者是线段。然后求哪一个空间,或者那一条线段上的权值最大

把点转换成矩形就是说把每一个星星的x+xx/2,y+yy/2,y-yy/2。然后就是扫描线,看哪一部分重合的权值最大。

与之前做过的扫描线不同的就是,因为要算上边缘上的星星,所以线段树上的节点不能是某一段空间了,而是具体的一个一个直线了,所以建立线段树的时候就是dycount-1,而不是dycouut-1-1。

然后就是求动态插入某一段线段树,求总的最大值。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#pragma warning(disable:4996)
using namespace std; #define maxn 90002 struct no
{
int le,re;
long long max_value,sing_value;
}tree[maxn*5]; struct li
{
double x,y1,y2;
int value;
bool end;
}line[maxn*2]; struct sta
{
int x,y,value;
}star[maxn]; int lcount,dycount;
double dy[maxn*2];
map<double,int>map_m;
long long n,w,h; bool cmp_double(double a,double b)
{
return a<b;
} bool cmp_x(const li a,const li b)
{
if(a.x==b.x)
return !a.end;
else
return a.x<b.x;
} void buildtree(int root,int L,int R)
{
tree[root].le=L;
tree[root].re=R;
tree[root].max_value=0;
tree[root].sing_value=0; if(L!=R)
{
int mid = (L+R)/2;
buildtree(root*2+1,L,mid);
buildtree(root*2+2,mid+1,R);
}
} void add_tree(int root,int L,int R,int c)
{
if(tree[root].le==L&&tree[root].re==R)
{
tree[root].sing_value+=c;
tree[root].max_value+=c;
return;
} int mid = (tree[root].le+tree[root].re)/2;
if(R<=mid)
{
add_tree(root*2+1,L,R,c);
}
else if(L>=mid+1)
{
add_tree(root*2+2,L,R,c);
}
else
{
add_tree(root*2+1,L,mid,c);
add_tree(root*2+2,mid+1,R,c);
}
tree[root].max_value = max(tree[2*root+1].max_value,tree[2*root+2].max_value)+tree[root].sing_value;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,L1,R1;
long long ans;
double xx,yy;
while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF)
{
xx=w/2.0;
yy=h/2.0;
map_m.clear();
lcount=0;
dycount=0;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&star[i].x,&star[i].y,&star[i].value); line[lcount].x= star[i].x-xx;
line[lcount].y1=star[i].y-yy;
line[lcount].y2=star[i].y+yy;
line[lcount].value=star[i].value;
line[lcount].end=false;
lcount++; line[lcount].x=star[i].x+xx;
line[lcount].y1=star[i].y-yy;
line[lcount].y2=star[i].y+yy;
line[lcount].value=star[i].value;
line[lcount].end=true;
lcount++; dy[dycount++]=star[i].y-yy;
dy[dycount++]=star[i].y+yy;
}
sort(line,line+lcount,cmp_x);
sort(dy,dy+dycount,cmp_double);
dycount=unique(dy,dy+dycount)-dy; buildtree(0,0,dycount-1); for(i=0;i<dycount;i++)
{
map_m[dy[i]]=i;
} ans=0;
for(i=0;i<lcount;i++)
{
L1=map_m[line[i].y1];
R1=map_m[line[i].y2]; if(!line[i].end)
{
add_tree(0,L1,R1,line[i].value);
ans=max(tree[0].max_value,ans);
}
else
{
add_tree(0,L1,R1,-1*line[i].value);
}
}
cout<<ans<<endl;
}
//system("pause");
return 0;
}
Stars in Your Window
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11073   Accepted: 3033

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. 

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. 

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

POJ2482前面和题意无关,是一封非常优美的情书,觉得写得真是棒。

然后这个题目是不算边缘,一种做法就是建立线段树的时候像是之前的做法,叶子节点代表某一段空间了,另一种做法就是让窗户 减少一点点,在不影响圈星星的条件下,只减少一点点,这样就排除了边缘星星。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#pragma warning(disable:4996)
using namespace std; #define maxn 90002 struct no
{
int le,re;
long long max_value,sing_value;
}tree[maxn*5]; struct li
{
double x,y1,y2;
int value;
bool end;
}line[maxn*2]; struct sta
{
int x,y,value;
}star[maxn]; int lcount,dycount;
double dy[maxn*2];
map<double,int>map_m;
long long n,w,h; bool cmp_double(double a,double b)
{
return a<b;
} bool cmp_x(const li a,const li b)
{
if(a.x==b.x)
return !a.end;
else
return a.x<b.x;
} void buildtree(int root,int L,int R)
{
tree[root].le=L;
tree[root].re=R;
tree[root].max_value=0;
tree[root].sing_value=0; if(L!=R)
{
int mid = (L+R)/2;
buildtree(root*2+1,L,mid);
buildtree(root*2+2,mid+1,R);
}
} void add_tree(int root,int L,int R,int c)
{
if(tree[root].le==L&&tree[root].re==R)
{
tree[root].sing_value+=c;
tree[root].max_value+=c;
return;
} int mid = (tree[root].le+tree[root].re)/2;
if(R<=mid)
{
add_tree(root*2+1,L,R,c);
}
else if(L>=mid+1)
{
add_tree(root*2+2,L,R,c);
}
else
{
add_tree(root*2+1,L,mid,c);
add_tree(root*2+2,mid+1,R,c);
}
tree[root].max_value = max(tree[2*root+1].max_value,tree[2*root+2].max_value)+tree[root].sing_value;
} int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); int i,L1,R1;
long long ans;
double xx,yy;
while(scanf("%lld%lld%lld",&n,&w,&h)!=EOF)
{
xx=w/2.0-0.1;//!!!!!!!!!!!!!!!!!!!!这里有变化了
yy=h/2.0-0.1;//!!!!!!!!!!!!!!!!!!!!这里有变化了 map_m.clear();
lcount=0;
dycount=0;
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&star[i].x,&star[i].y,&star[i].value); line[lcount].x= star[i].x-xx;
line[lcount].y1=star[i].y-yy;
line[lcount].y2=star[i].y+yy;
line[lcount].value=star[i].value;
line[lcount].end=false;
lcount++; line[lcount].x=star[i].x+xx;
line[lcount].y1=star[i].y-yy;
line[lcount].y2=star[i].y+yy;
line[lcount].value=star[i].value;
line[lcount].end=true;
lcount++; dy[dycount++]=star[i].y-yy;
dy[dycount++]=star[i].y+yy;
}
sort(line,line+lcount,cmp_x);
sort(dy,dy+dycount,cmp_double);
dycount=unique(dy,dy+dycount)-dy; buildtree(0,0,dycount-1); for(i=0;i<dycount;i++)
{
map_m[dy[i]]=i;
} ans=0;
for(i=0;i<lcount;i++)
{
L1=map_m[line[i].y1];
R1=map_m[line[i].y2]; if(!line[i].end)
{
add_tree(0,L1,R1,line[i].value);
ans=max(tree[0].max_value,ans);
}
else
{
add_tree(0,L1,R1,-1*line[i].value);
}
}
cout<<ans<<endl;
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

51nod 1208 && POJ 2482:Stars in Your Window的更多相关文章

  1. POJ - 2482:Stars in Your Window (扫描线 )

    题意:二维平面上给你N颗星,给出星星的坐标,亮度: 然后给你一个W*H的窗口,问你最大的亮度和. 思路:扫描线,假设有一个inf*H的窗口,按照y排序,那么就把H范围内的星星放入了这个窗口(单调队列实 ...

  2. 【POJ 2482】 Stars in Your Window(线段树+离散化+扫描线)

    [POJ 2482] Stars in Your Window(线段树+离散化+扫描线) Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  3. 【POJ 2482】Stars in Your Window

    http://poj.org/problem?id=2482 线段树扫描线 #include<cstdio> #include<cstring> #include<alg ...

  4. 【POJ 2482】 Stars in Your Windows

    [题目链接] http://poj.org/problem?id=2482 [算法] 线段树 + 扫描线 [代码] #include <algorithm> #include <bi ...

  5. poj 2482 Stars in Your Window(扫描线)

    id=2482" target="_blank" style="">题目链接:poj 2482 Stars in Your Window 题目大 ...

  6. POJ 2482 Stars in Your Window(线段树)

    POJ 2482 Stars in Your Window 题目链接 题意:给定一些星星,每一个星星都有一个亮度.如今要用w * h的矩形去框星星,问最大能框的亮度是多少 思路:转化为扫描线的问题,每 ...

  7. poj 2482 Stars in Your Window + 51Nod1208(扫描线+离散化+线段树)

    Stars in Your Window Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13196   Accepted:  ...

  8. POJ 2482 Stars in Your Window 线段树扫描线

    Stars in Your Window   Description Fleeting time does not blur my memory of you. Can it really be 4 ...

  9. (中等) POJ 2482 Stars in Your Window,静态二叉树。

    Description Here comes the problem: Assume the sky is a flat plane. All the stars lie on it with a l ...

随机推荐

  1. 深浅copy浅析

    Python代码在开始执行的时候,代码会被系统从硬盘调入内存,等候CPU执行,至于怎么个调入逻辑,还不清楚. 在高级语言中,变量是对内存及其地址的抽象.也就是说变量就是内存地址. 那么我们先来介绍两种 ...

  2. 标准盒模型、怪异盒模型(box-sizing)

    CSS中Box model是分为两种: W3C标准(标准盒模型) 和 IE标准盒子模型(怪异盒模型).大多数浏览器采用W3C标准模型,而IE中则采用Microsoft自己的标准. 重要的一个属性是bo ...

  3. 为kubernetes-dashboard页面增加过期时间,减少登录次数.

    方法很多,最简单的就是登录后,找到Deployments 服务, 右侧界面会出现kubernetes-dashboard的项目,如果没出现,那么在namespace那里选择全部名称空间. ports: ...

  4. html常用整理

    视频链接:https://www.bilibili.com/video/av5862916?from=search&seid=12139895566389560177 我的第一个html &l ...

  5. 「BJOI2018」求和

    「BJOI2018」求和 传送门 观察到 \(k\) 很小而且模数不会变,所以我们直接预处理 \(k\) 取所有值时树上前缀答案,查询的时候差分一下即可. 参考代码: #include <alg ...

  6. 设计模式课程 设计模式精讲 2-2 UML类图讲解

    1 类图讲解 1 类图讲解 + 公共方法 - private权限 # protected权限 包内和包外继承的子类都能引用 ~ default权限(包权限)只有包内能引用 下划线   静态 Stati ...

  7. Intend之属性extra

    我们这次想要实现的功能是从A活动跳到B活动时,A活动中有一个输入框和一个按钮,当点击按钮是时会跳到B活动,然后把A活动中的输入框的内容传到B活动中,且在B活动中的TextView中显示 A活动中先添加 ...

  8. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  9. java并发:初探消费者和生产者模式

    消费者和生产者模式 用继承Thread方式,用wait和notifyAll方法实现. 消费者和生产者模式的特点 1. 什么时候生产:仓库没有满的时候,生产者这可以生产,消费者也可以消费,仓库满的时候停 ...

  10. day16-Python运维开发基础(os / os.path / shutil模块)

    1. OS模块与shutil模块 os :新建/删除shutil: 复制/移动 # ### os模块 与 shutil模块 """ os 新建/删除 shutil 复制/ ...