hdu1050 Moving Tables BIT树状数组

The floor has 200 rooms each on the north side and south side along the corridor. Recently the Company made a plan to reform its system. The reform includes moving a lot of tables between rooms. Because the corridor is narrow and all the tables are big, only one table can pass through the corridor. Some plan is needed to make the moving efficient. The manager figured out the following plan: Moving a table from a room to another room can be done within 10 minutes. When moving a table from room i to room j, the part of the corridor between the front of room i and the front of room j is used. So, during each 10 minutes, several moving between two rooms not sharing the same part of the corridor will be done simultaneously. To make it clear the manager illustrated the possible cases and impossible cases of simultaneous moving.


题意:图上看到的是房间,中间隔着的是走廊,北边是奇数号房间,南边是偶数号房间,现在要搬桌子,每次搬一次桌子要10分钟,假设从1号搬到2号,从2号搬到3号,那么需要20分钟(因为2是公共的,1->2占用了走廊)。而从1号搬到3号,4号搬到5号就只要10分钟。要求统计最少需要的时间。

题解:利用BIT树状数组求出最大的重叠次数。需要注意的是,如果给的是从4号搬到5号,那么BIT的区间范围应该是[3,6]。而不是[4,5]。

#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define LL long long
#define maxn 6005
#define INF 2147483646
using namespace std;
int a[100005],b[100005];
int n,m,k,l,r;
void BIT(int i,int d)
{
    while(i<=400)
    {
        b[i]+=d;
        i+=i&-i;
    }
}
int  Sum(int i)
{
    int sum=0;
    while(i)
    {
        sum+=b[i];
        i-=i&-i;
    }
    return sum;
}
int main()
{
    int tmp,count=1;
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        tmp=0;
        memset(b,0,sizeof(b));
        for(int i=1;i<=n;i++)
        {
            int x,y;
            cin>>x>>y;
            if(x>y)
                swap(x,y);
            if(x%2==0)
                x--;
            if(y%2==1)
                y++;
            BIT(x,1);
            BIT(y+1,-1);
        }
        for(int i=1;i<=400;i++)
        {
            if(Sum(i)>tmp)
                tmp=Sum(i);
        }
        cout<<tmp*10<<endl;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值