数据结构 5.综教楼后的那个坑

问题描述

在 LIT 综教楼后有一个深坑,关于这个坑的来历,有很多种不同的说法。其中一种说法是,在很多年以前,这个坑就已经在那里了。这种说法也被大多数人认可,这是因为该坑有一种特别的结构,想要人工建造是有相当困难的。

从横截面图来看,坑底成阶梯状,由从左至右的 1…N 个的平面构成(其中 1 ≤ N ≤ 100,000),如图:

在这里插入图片描述

每个平面 i 可以用两个数字来描述,即它的宽度 Wi 和高度 Hi,其中 1 ≤ Wi ≤ 1,000、1 ≤ Hi ≤ 1,000,000,而这个坑最特别的地方在于坑底每个平面的高度都是不同的。每到夏天,雨水会把坑填满,而在其它的季节,则需要通过人工灌水的方式把坑填满。灌水点设在坑底位置最低的那个平面,每分钟灌水量为一个单位(即高度和宽度均为 1)。随着水位的增长,水自然会向其它平面扩散,当水将某平面覆盖且水高达到一个单位时,就认为该平面被水覆盖了。

请你计算每个平面被水覆盖的时间。
  在这里插入图片描述

输入

输入的第一行是一个整数 N,表示平面的数量。从第二行开始的 N 行上分别有两个整数,分别表示平面的宽度和高度。

输出

输出每个平面被水覆盖的时间。

样例

输入(1)

3
4 2
2 7
6 4

输出(1)

4
50
26

输入(2)

3
4 2
6 4
2 7

输出(2)

4
18
50

代码

#include<stdio.h>  
#include<stdlib.h>   
int main()  
{  
    struct node  
    {  
        int id;  
        long long int width;  
        long long int height;  
        struct node *front;  
        struct node *back;  
    };  
    int n;  
    scanf("%d",&n);  
    long long int time[100005];  
    struct node *left,*right;  
    left=(struct node*)malloc(sizeof(struct node));  
    left->width=0;  
    left->height=100005;  
    struct node *start;  
    start=left;  
    struct node *ppre;  
    ppre=left;  
    struct node *p;  
    for(int i=1;i<=n;i++)  
    {  
        p=(struct node*)malloc(sizeof(struct node));  
        scanf("%lld %lld",&p->width,&p->height);  
        p->id=i;  
        p->front=ppre;  
        ppre->back=p;  
        p->back=NULL;  
        struct node *c1;  
        c1=p;  
        ppre=c1;  
        if(start->height>p->height)  
        {  
            start=p;  
        }  
    }  
    right=(struct node*)malloc(sizeof(struct node));  
    right->width=0;  
    right->height=100005;  
    right->back=NULL;  
    right->front=p;  
    p->back=right;  
    long long int t=0;  
    for(int i=1;i<n;i++)  
    {  
        time[start->id]=t+start->width;  
        if(start->front->height>start->back->height)  
        {  
            struct node *c1,*c2;  
            long long int w;  
            w=start->width;  
            c1=start->front;  
            c2=start->back;    
            t=t+(c2->height-start->height)*w;    
            c2->width+=w;    
            c1->back=c2;  
            c2->front=c1;    
            start=c2;   
            if((start->height<c1->height)&&(start->height<c2->height))  
            {  
                continue;  
            }  
            else  
            {  
                while(start->height>start->back->height)  
                {  
                    start=start->back;  
                }  
            }  
        }    
        else    
        {  
            struct node *c1,*c2;  
            c1=start->front;  
            c2=start->back;  
            long long int w;  
            w=start->width;    
            t=t+(c1->height-start->height)*w;    
            c1->width+=w;    
            c1->back=c2;     
            c2->front=c1;    
            start=c1;   
            if((start->height<c1->height)&&(start->height<c2->height))  
            {  
                continue;  
            }  
            else  
            {  
                while(start->height>start->front->height)  
                {  
                    start=start->front;  
                }  
            }  
        }  
    }  
    time[start->id]=t+start->width;  
    int x;  
    x=1;  
    int i=1;  
    while(x>0)  
    {  
        if(i>n)  
        {  
            break;  
        }  
        else  
        {  
            printf("%lld\n",time[i]);  
            i++;  
        }  
    }  
    return 0;  
}   
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
描述   在 LIT 楼后有一个深,关于这个的来历,有很多种不同的说法。其中一种说法是,在很多年以前,这个就已经在那里了。这种说法也被大多数人认可,这是因为该有一种特别的结构,想要人工建造是有相当困难的。   从横截面图来看,底成阶梯状,由从左至右的 1..N 个的平面构成(其中 1 ≤ N ≤ 100,000),如图:    *            * :    *            * :    *            * 8    *    **      * 7    *    **      * 6    *    **      * 5    *    ********* 4 <- 高度    *    ********* 3    ************** 2    ************** 1 平面 |  1  |2|   3    | 每个平面 i 可以用两个数字来描述,即它的宽度 Wi 和高度 Hi,其中 1 ≤ Wi ≤ 1,000、1 ≤ Hi ≤ 1,000,000,而这个最特别的地方在于底每个平面的高度都是不同的。每到夏天,雨水会把填满,而在其它的季节,则需要通过人工灌水的方式把填满。灌水点设在底位置最低的那个平面,每分钟灌水量为一个单位(即高度和宽度均为 1)。随着水位的增长,水自然会向其它平面扩散,当水将某平面覆盖且水高达到一个单位时,就认为该平面被水覆盖了。   请你计算每个平面被水覆盖的时间。    灌水 水满后自动扩散 | | * | * * | * * * * V * * V * * * * * * .... * *~~~~~~~~~~~~* * ** * *~~~~** : * *~~~~**~~~~~~* * ** * *~~~~** : * *~~~~**~~~~~~* * ** * *~~~~**~~~~~~* *~~~~**~~~~~~* * ********* *~~~~********* *~~~~********* *~~~~********* *~~~~********* *~~~~********* ************** ************** ************** ************** ************** **************    4 分钟后    26 分钟后        50 分钟后    平面 1 被水覆盖     平面 3 被水覆盖    平面 2 被水覆盖输入   输入的第一行是一个整数 N,表示平面的数量。从第二行开始的 N 行上分别有两个整数,分别表示平面的宽度和高度。 输出   输出每个平面被水覆盖的时间。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值