热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

POJ2482星空中的星星:利用线段树与扫描线算法解决

在《POJ2482星空中的星星》问题中,通过运用线段树和扫描线算法,可以高效地解决星星在窗口内的计数问题。该方法不仅能够快速处理大规模数据,还能确保时间复杂度的最优性,适用于各种复杂的星空模拟场景。
Stars in Your Window
 

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

 

题意:

  给你一个w*h的矩形,和平面上n个点,每个点有权值

  问你用这个矩形能框住最大的点权和,要球必须在这个矩形内部

题解:

  将点的范围延展成矩形,用扫描线去扫

  线段树维护区间最值

#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int N = 2e6+20, M = 1e6+10, mod = 1e9+7, inf = 2e9+1000;
typedef long long ll;

int n,w,h;
ll y[N];
struct Line{
    ll top,x,down,f;
    Line() {}
    Line ( ll a,ll b,ll c,ll d )  { top=a,x=b,down=c,f=d;}
}line[N];
bool cmp(Line s1,Line s2) {
    if(s1.x==s2.x) return s1.f<s2.f;
    else return s1.x<s2.x;
}
mapint > H;
ll mx[N*4],l[N*4],r[N*4],lazy[N*4];
void pushdown(int k) {
    if(lazy[k]==0) return ;
    ll tmp = lazy[k];
    lazy[k] = 0;
    mx[k<<1]+=tmp;
    mx[k<<1|1]+=tmp;
    lazy[k<<1] += tmp;
    lazy[k<<1|1] += tmp;
}
void build(int k,int s,int t) {
    l[k] = s;
    r[k] = t;
    mx[k] = 0;
    lazy[k] = 0;
    if(s==t) return ;
    int mid = (s+t)>>1;
    build(k<<1,s,mid);
    build(k<<1|1,mid+1,t);
}
void update(int k,int x,int y,ll c) {
     pushdown(k);
    if(l[k]==x&&r[k]==y) {
        mx[k] += c;
        lazy[k] += c;
        return ;
    }

    int mid = (l[k]+r[k])>>1;
    if(y<=mid) update(k<<1,x,y,c);
    else if(x>mid) update(k<<1|1,x,y,c);
    else update(k<<1,x,mid,c),update(k<<1|1,mid+1,y,c);

    mx[k] = max(mx[k<<1],mx[k<<1|1]);
}
int main() {
    while(scanf("%d%d%d",&n,&w,&h)!=EOF) {
        int cnt = 0;
         H.clear();

        for(int i=1;i<=n;i++) {
            ll a,b,c;
            scanf("%I64d%I64d%I64d",&a,&b,&c);
            line[++cnt] = Line(b+h-1,a,b,c);
            y[cnt] = b;
            line[++cnt] = Line(b+h-1,a+w,b,-c);
            y[cnt] = b+h-1;
        }

        sort(y+1,y+cnt+1);
        sort(line+1,line+1+cnt,cmp);
        int c = unique(y+1,y+cnt+1) - y - 1;
        for(int i=1;i<=c;i++) H[y[i]] = i;

        build(1,1,c);
        ll ans = 0;
        for(int i=1;i<=cnt;i++) {
            update(1,H[line[i].down],H[line[i].top],line[i].f);
            ans = max(ans,mx[1]);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

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


推荐阅读
  • ListView简单使用
    先上效果:主要实现了Listview的绑定和点击事件。项目资源结构如下:先创建一个动物类,用来装载数据:Animal类如下:packagecom.example.simplelis ... [详细]
  • 探讨 HDU 1536 题目,即 S-Nim 游戏的博弈策略。通过 SG 函数分析游戏胜负的关键,并介绍如何编程实现解决方案。 ... [详细]
  • 深入解析动态代理模式:23种设计模式之三
    在设计模式中,动态代理模式是应用最为广泛的一种代理模式。它允许我们在运行时动态创建代理对象,并在调用方法时进行增强处理。本文将详细介绍动态代理的实现机制及其应用场景。 ... [详细]
  • Python自动化测试入门:Selenium环境搭建
    本文详细介绍如何在Python环境中安装和配置Selenium,包括开发工具PyCharm的安装、Python环境的设置以及Selenium包的安装方法。此外,还提供了编写和运行第一个自动化测试脚本的步骤。 ... [详细]
  • 本文详细介绍了一种高效的算法——线性筛法,用于快速筛选出一定范围内的所有素数。通过该方法,可以显著提高求解素数问题的效率。 ... [详细]
  • 本文详细介绍了get和set方法的作用及其在编程中的实现方式,同时探讨了点语法的使用场景。通过具体示例,解释了属性声明与合成存取方法的概念,并补充了相关操作的最佳实践。 ... [详细]
  • 本文介绍了如何通过Java代码计算一个整数的位数,并展示了多个基础编程示例,包括求和、平均分计算、条件判断等。 ... [详细]
  • 本题要求在一组数中反复取出两个数相加,并将结果放回数组中,最终求出最小的总加法代价。这是一个经典的哈夫曼编码问题,利用贪心算法可以有效地解决。 ... [详细]
  • 使用JS、HTML5和C3创建自定义弹出窗口
    本文介绍如何结合JavaScript、HTML5和C3.js来实现一个功能丰富的自定义弹出窗口。通过具体的代码示例,详细讲解了实现过程中的关键步骤和技术要点。 ... [详细]
  • 本篇文章介绍如何将两个分别表示整数的链表进行相加,并生成一个新的链表。每个链表节点包含0到9的数值,如9-3-7和6-3相加得到1-0-0-0。通过反向处理链表、逐位相加并处理进位,最终再将结果链表反向,即可完成计算。 ... [详细]
  • 深入剖析JVM垃圾回收机制
    本文详细探讨了Java虚拟机(JVM)中的垃圾回收机制,包括其意义、对象判定方法、引用类型、常见垃圾收集算法以及各种垃圾收集器的特点和工作原理。通过理解这些内容,开发人员可以更好地优化内存管理和程序性能。 ... [详细]
  • Java中的基本数据类型与包装类解析
    本文探讨了Java编程语言中的8种基本数据类型及其对应的包装类。通过分析这些数据类型的特性和使用场景,以及自动拆装箱机制的实现原理,帮助开发者更好地理解和应用这些概念。 ... [详细]
  • Linux中的yum安装软件
    yum俗称大黄狗作用:解决安装软件包的依赖关系当安装依赖关系的软件包时,会将依赖的软件包一起安装。本地yum:需要yum源,光驱挂载。yum源:(刚开始查看yum源中的内容就是上图 ... [详细]
  • Vue 开发与调试工具指南
    本文介绍了如何使用 Vue 调试工具,包括克隆仓库、安装依赖包、构建项目以及在 Chrome 浏览器中加载扩展的详细步骤。 ... [详细]
  • 本文详细探讨了Java中的包管理机制,包括默认包的使用和自定义包名的创建方法。通过实际操作,帮助开发者更好地理解和应用包管理。 ... [详细]
author-avatar
高正_飞翔之殇_826
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有