poj3277 City Horizon

City Horizon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 15636 Accepted: 4222

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

Line 1: A single integer:  N 
Lines 2.. N+1: Input line  i+1 describes building  i with three space-separated integers:  AiBi, and  Hi

Output

Line 1: The total area, in square units, of the silhouettes formed by all  N buildings

Sample Input

4
2 5 1
9 10 4
6 8 2
4 6 3

Sample Output

16

Hint

The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 3*1 + 1*4 + 2*2 + 2*3 - 1 = 16.

Source


从CTrip编程大赛回来之后,发现是该象征性的准备下接下来的各种商业赛了……


老题了,给n个建筑,每个建筑的起点终点和高度,求所有建筑的面积和。

离散化,然后单调队列搞搞就可以了。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;

struct Build
{
    int l,r;
    long long val;
};

struct Hash
{
    int val,num;
};

Build build[40006];
Hash hash[100000];
int up;
int inout[40005];

struct cmp
{
    bool operator()(const Hash &x,const Hash &y)
    {
        if (build[x.num].val!=build[y.num].val) return build[x.num].val<build[y.num].val;
        return x.num<y.num;
    }
};

priority_queue<Hash,vector<Hash>,cmp> q;

bool Cmp(Hash x,Hash y)
{
    if (x.val!=y.val) return x.val<y.val;
    return x.num<y.num;
}

int main()
{
    int i,j,n;
    while(scanf("%d",&n)!=EOF)
    {
        up=0;
        for (i=0;i<n;i++)
        {
            scanf("%d%d%lld",&build[i].l,&build[i].r,&build[i].val);
            hash[up].val=build[i].l;
            hash[up++].num=i;
            hash[up].num=i;
            hash[up++].val=build[i].r;
        }
        sort(hash,hash+up,Cmp);
        memset(inout,0,sizeof(inout));
        long long ans=0;
        int last=0;
        for (i=0;i<up;i++)
        {
         //   printf("For No.%d: \n",i);
            if (!q.empty())
            {
                ans+=(hash[i].val-last)*build[q.top().num].val;
            //    printf("%d %d %d\n",hash[i].val,last,build[q.top().num].val);
            }

            last=hash[i].val;
            if (inout[hash[i].num]==0)
            {
                q.push(hash[i]);
                inout[hash[i].num]=1;
            }
            else
            {
                inout[hash[i].num]=0;
            }
            while(!q.empty())
            {
                if (inout[q.top().num]==0) q.pop();
                else break;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值