| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 14291 | Accepted: 3867 |
Description
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
Input
Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
Output
Sample Input
4 2 5 1 9 10 4 6 8 2 4 6 3
Sample Output
16
建筑的两条边对应建筑的进堆和出堆。 (堆必须支持删除)
当入边的高度比 对顶建筑还要高,说明轮廓中前面的矩形到头了。
当出边对应的建筑就是当前最高的建筑(堆顶的建筑),同样说明轮廓中当前的矩形到头了。
当然这个题目还可以用 线段树来做。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define N 40400
struct _building{
long long l,r,h;
}B[N];
struct _wall{
int b;
bool left;
void set(int b,bool left)
{
this->b = b;
this->left = left;
}
bool operator < (const _wall& other)const
{
long long x = left ? B[b].l:B[b].r;
long long y = other.left ? B[other.b].l:B[other.b].r;
if( x==y ) return left < other.left; // right first
else return x < y;
}
}wall[N*2];
struct node{
int b;
bool operator < (const node& other)const
{ return B[b].h < B[other.b].h; }
bool operator > (const node& other)const
{ return B[b].h > B[other.b].h; }
};
struct Heap{
void swap(int x,int y)
{
Hash[ array[x].b ] = y;
Hash[ array[y].b ] = x;
node t = array[ x ] ;
array[ x ] = array[ y ];
array[ y ] = t;
}
void up_adjust(int idx)
{
int father = idx/2;
while(father && array[father] < array[idx]){
swap( father,idx );
father /= 2; idx /= 2;
}
}
void insert(int b)
{
array[++size].b = b;
Hash[b] = size;
up_adjust(size);
}
void down_adjust(int idx)
{
while(idx<=size){
int mx = idx;
if( idx*2 <= size && array[idx*2] > array[mx] ) mx = idx*2;
if( idx*2+1 <= size && array[idx*2+1] > array[mx] ) mx = idx*2+1;
if( mx == idx ) break;
swap( idx, mx );
idx = mx;
}
}
void remove(int b)
{
int idx = Hash[b];
if( idx==size ){
size -- ;
}else if( array[size] < array[idx] ){
array[idx] = array[size--];
Hash[ array[idx].b ] = idx;
down_adjust(idx);
}else{
array[idx] = array[size--];
Hash[ array[idx].b ] = idx;
up_adjust(idx);
}
Hash[b] = 0;
}
bool empty(){return size==0;}
int top(){ return array[1].b; }
void show()
{
for(int i=1;i<=size;i++){
cout<<"<"<<array[i].b<<","<<B[array[i].b].h<<"> ";
if( (i &(i+1)) == 0) cout<<endl;
}cout<<endl<<endl;
}
int Hash[N];
int size;
node array[N];
}hp;
int main()
{
int n; scanf("%d",&n);
for(int i=0;i<n;i++){
//cout<<"Building "<<i<<":";
scanf("%lld%lld%lld",&B[i].l,&B[i].r,&B[i].h);
wall[i*2].set(i,0);
wall[i*2+1].set(i,1);
}
sort(wall,wall+n*2);
long long area = 0;
long long pre = 0; // pre wall
int b; // current building
for(int i=0;i<n*2;i++){
b = wall[i].b;
//if( wall[i].left ) cout<<"Buiding "<<b<<" left: "<<B[b].l<<" Height:"<<B[b].h<<endl;
//else cout<<"Buiding "<<b<<" right :"<<B[b].r<<" Height:"<<B[b].h<<endl;
if( wall[i].left ){ // ¿ªÊ¼±ß
if( hp.empty() ) pre = B[b].l;
else if( B[hp.top()].h < B[b].h ){
area += B[hp.top()].h * (B[b].l - pre) ;
pre = B[b].l;
}
hp.insert( b );
}else{ //½áÊø±ß
if( b == hp.top() ){
area += B[b].h * (B[b].r - pre);
pre = B[b].r;
}
hp.remove( b );
}
//cout<<"area :"<<area<<endl;
//cout<<"pre :"<<pre<<endl<<endl;
}
cout<<area<<endl;
//system("pause");
return 0;
}
本文探讨了如何计算城市天际线上由多个建筑物形成的总面积,通过将建筑物离散化并使用堆数据结构来解决该问题。利用算法处理每条边,计算轮廓面积,实现高效解决方案。
319

被折叠的 条评论
为什么被折叠?



