(数据结构) 综教楼后的那个坑

(数据结构) 综教楼后的那个坑:

描述:

在 LIT 综教楼后有一个深坑,关于这个坑的来历,有很多种不同的说法。其中一种说法是,在很多年以前,这个坑就已经在那里了。这种说法也被大多数人认可,这是因为该坑有一种特别的结构,想要人工建造是有相当困难的。
  从横截面图来看,坑底成阶梯状,由从左至右的 1…N 个的平面构成(其中 1 ≤ N ≤ 100,000),如图:
   在这里插入图片描述
每个平面 i 可以用两个数字来描述,即它的宽度 Wi 和高度 Hi,其中 1 ≤ Wi ≤ 1,000、1 ≤ Hi ≤ 1,000,000,而这个坑最特别的地方在于坑底每个平面的高度都是不同的。每到夏天,雨水会把坑填满,而在其它的季节,则需要通过人工灌水的方式把坑填满。灌水点设在坑底位置最低的那个平面,每分钟灌水量为一个单位(即高度和宽度均为 1)。随着水位的增长,水自然会向其它平面扩散,当水将某平面覆盖且水高达到一个单位时,就认为该平面被水覆盖了。

请你计算每个平面被水覆盖的时间。

在这里插入图片描述

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

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

测试输入期待输出
3↵ 4 2↵ 2 7↵ 6 4↵4↵ 50↵ 26↵
3↵ 4 2↵ 6 4↵ 2 7↵4↵ 18↵ 50↵

代码如下:

#include<stdio.h>   
#include<stdlib.h>    
typedef struct hole   
{   
    long long number,width,height;   
    struct hole *pre,*next;   
}thehole;   
    
long long tot[100005];   
    
int main()   
{   
    thehole *left,*right,*prehole,*cur,*deep;   
    long long n,a,b;   
       
   left = (thehole *)malloc(sizeof(thehole));   
   left->width = 0;   
    left->height = 1000005;   
    left->pre=left->next = NULL;   
       
   prehole = left;   
    deep = left;   
       
  scanf("%lld",&n);   
    
   for(long long i = 0; i < n; i++)   
  {   
        scanf("%lld %lld", &a, &b);   
        cur = (thehole *)malloc(sizeof(thehole));   
        cur->width = a;   
        cur->height = b;   
        cur->pre = prehole;   
        cur->next = NULL;   
        cur->number = i;   
           
       prehole->next = cur;   
        prehole = cur;   
    
       if(deep->height > b) deep = cur;   
    }   
    
    right = (thehole *)malloc(sizeof(thehole));   
    right->width = 0;   
    right->height = 1000005;   
    right->pre = cur;   
       
    cur->next = right;   
    right->next = NULL;   
    
    long long time = 0;   
   	while(deep->pre->height != deep->next->height)   
  	{   
        tot[deep->number] = time + deep->width;   
        
        if(deep->pre->height > deep->next->height)   
        {   
            time = time + (deep->next->height - deep->height) * deep->width;   
            deep->next->width += deep->width;   
            deep->pre->next = deep->next;   
            deep->next->pre = deep->pre;   
            deep = deep->next;   
        }   
           
        else   
	    {   
            time = time + (deep->pre->height - deep->height) * deep->width;   
            deep->pre->width += deep->width;   
            deep->pre->next = deep->next;   
            deep->next->pre = deep->pre;   
            deep = deep->pre;   
        }   
    
        if(deep->height < deep->pre->height && deep->height < deep->next->height)   
         continue;   
           
      	else if(deep->pre->height > deep->next->height)   
       	{   
            while(deep->height > deep->next->height)   
            deep = deep->next;   
        }   
           
        else   
       	{   
            while(deep->height > deep->pre->height)   
            deep = deep->pre;   
        }   
    }   
       
    tot[deep->number] = time + deep->width;   
    
    for (long long i = 0; i < n; i++) printf("%lld\n", tot[i]);   
       
}  
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值