POJ 1765 November Rain
题目大意:
有一些屋顶,相当于一些线段(不想交)。
问每一条线段能够接到多少水,相对较低的屋顶能够接到高屋顶留下的水(如题图所看到的)。因为y1!=y2,所以保证屋顶是斜的。
解题思路:
扫描线,由于对于同一个x最多有25条线段。所以不须要线段树更新。
在扫描线的过程中构造出线段与线段之间的关系。好在最后计算每一个屋顶能够接多少水。
以下是代码:
#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> #define eps 1e-8
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define iabs(x) ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y ) ( ((x) > (y)) ? (x) : (y) )
#define min( x, y ) ( ((x) < (y)) ? (x) : (y) ) using namespace std; struct node
{
int x1,x2,y1,y2;
int num,next,w,deg;
double k;
} segment[400005]; int n,point[400000<<1],ans[400005],q[400005],a[100]; bool cmp(node a,node b)
{
return a.x1<b.x1;
} int main()
{
int last=0,now,tail=0,k,cnt,u,v,pointcnt,levelcnt;
while(scanf("%d",&n)!=EOF)
{
pointcnt=0;
for(int i=0; i<n; i++)
{
scanf("%d%d%d%d",&segment[i].x1,&segment[i].y1,&segment[i].x2,&segment[i].y2);
segment[i].num=i;
segment[i].next=-1;
segment[i].w=0;
segment[i].deg=0;
segment[i].k=(1.0*segment[i].y2-segment[i].y1)/(segment[i].x2-segment[i].x1);
point[pointcnt++]=segment[i].x1;
point[pointcnt++]=segment[i].x2;
} sort(point,point+pointcnt);
pointcnt=unique(point,point+pointcnt)-point; sort(segment,segment+n,cmp); last=0;
tail=0;
levelcnt=0; for(int i = 0; i < pointcnt ; last=now,i++)
{
now=point[i];
if(levelcnt)
{
segment[a[levelcnt-1]].w+=now-last;
}
while(segment[tail].x1==now&&tail<n)
{
k=-1;
for(int j=0; j<levelcnt; j++)
{
if( (segment[a[j]].y1 + segment[a[j]].k * (now - segment[a[j]].x1))<segment[tail].y1)k=j;
}
for(int j=levelcnt-1; j>k; j--)
{
a[j+1]=a[j];
}
a[k+1]=tail;
levelcnt++;
tail++;
}
for(int j=1; j<levelcnt; j++)
{
if((segment[a[j]].x1==now&&segment[a[j]].y1<segment[a[j]].y2)||(segment[a[j]].x2==now&&segment[a[j]].y1>segment[a[j]].y2))
{
segment[a[j]].next=a[j-1];
segment[a[j-1]].deg++;
}
}
cnt=0;
for(int j=0; j<levelcnt; j++)
{
if(segment[a[j]].x2!=now)a[cnt++]=a[j];
}
levelcnt=cnt;
}
queue <int >q;
for(int i=0; i<n; i++)
{
if(!segment[i].deg)q.push(i);
}
while(!q.empty())
{
u=q.front();
q.pop();
v=segment[u].next;
if(v==-1)continue;
segment[v].deg--;
segment[v].w+=segment[u].w;
if(!segment[v].deg)q.push(v);
}
for(int i=0; i<n; i++)
{
ans[segment[i].num]=segment[i].w;
} for(int i=0; i<n; i++)
{
printf("%d\n",ans[i]);
} }
return 0;
}
POJ 1765 November Rain的更多相关文章
- [POJ1765]November Rain
[POJ1765]November Rain 试题描述 Contemporary buildings can have very complicated roofs. If we take a ver ...
- poj很好很有层次感(转)
OJ上的一些水题(可用来练手和增加自信) (POJ 3299,POJ 2159,POJ 2739,POJ 1083,POJ 2262,POJ 1503,POJ 3006,POJ 2255,POJ 30 ...
- POJ题目分类推荐 (很好很有层次感)
著名题单,最初来源不详.直接来源:http://blog.csdn.net/a1dark/article/details/11714009 OJ上的一些水题(可用来练手和增加自信) (POJ 3299 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 【Go入门教程6】interface(interface类型、interface值、空interface{}、嵌入interface、反射)
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- Go语言interface详解
interface Go语言里面设计最精妙的应该算interface,它让面向对象,内容组织实现非常的方便,当你看完这一章,你就会被interface的巧妙设计所折服. 什么是interface 简单 ...
- GoLang之方法与接口
GoLang之方法与接口 Go语言没有沿袭传统面向对象编程中的诸多概念,比如继承.虚函数.构造函数和析构函数.隐藏的this指针等. 方法 Go 语言中同时有函数和方法.方法就是一个包含了接受者的函数 ...
- Go 接口(interface)
文章转载地址:https://www.flysnow.org/2017/04/03/go-in-action-go-interface.html 1.什么是 interface? 简单的说,i ...
- Ext JS 4 的类系统
前言 我们知道,JavaScript中没有真正的类,它是一种面向原型的语言 .这种语言一个强大的特性就是灵活,实现一个功能可以有很多不同的方式,用不同的编码风格和技巧.但随之也带来了代码的不可预测和难 ...
随机推荐
- hdu 3177 Crixalis's Equipment
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- 【解决】run-as: Package '' is unknown
问题: [2014-07-30 20:20:25 - nativeSensorStl] gdbserver output: [2014-07-30 20:20:25 - nativeSensorStl ...
- Android编程获取手机型号,本机电话号码,sdk版本号及firmware版本号号(即系统版本号号)
Android开发平台中,可通过TelephonyManager 获取本机号码. TelephonyManager phoneMgr=(TelephonyManager)this.getSystemS ...
- nyoj-647-奋斗小蜗牛在请客(进制转换)
奋斗小蜗牛在请客 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描写叙述 一路艰辛一路收获.成功爬过金字塔的小蜗牛别提多高兴了.这不为了向以前帮助他的哥们们表达谢意,蜗牛宴请 ...
- hdu 2151
就是一个dp,数组内存的步数, 数组没清空,wa了一次 #include<cstdio> #include<algorithm> #include<cstring> ...
- struts2在action中获取request、session、application,并传递数据
假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...
- UVA 10127- Ones 数学
Given any integer 0 ≤ n ≤ 10000 not divisibleby 2 or 5, some multiple of n is a number whichin decim ...
- jquery 弹窗插件 layer
jquery 弹窗插件 layer 官网:http://sentsin.com/jquery/layer/ 1 <!DOCTYPE html PUBLIC "-//W3C//DTD H ...
- iOS~判断应用是否有定位权限
在特定场景下我们需要判断用户是否允许应用获取定位权限 1.导入类库: #import <CoreLocation/CLLocationManager.h> 2.判断用户手机是否开启了定位服 ...
- When Cyber Security Meets Machine Learning 机器学习 安全分析 对于安全领域的总结很有用 看未来演进方向
链接:http://ucys.ugr.es/jnic2016/docs/MachineLearning_LiorRokachJNIC2016.pdf https://people.eecs.berke ...