HDU 5192 Building Blocks Ⅱ(树状数组)

题意就略了。

先做完BC的B再做这个就比较好理解了,还是基本一样的想法,但是那个h就不确定了,要求是大于等于给定的H。

对于一个区间,消耗的价值等于max(大于h减h的和,h减小于h的和),所以可以发现当h取这个区间所有楼高度的平均值的时候,这个消耗是最小的。那么思路就来了,要维护一下区间高度小于等于x楼的个数,以及区间里高度小于等于x楼的总和,这样在算出平均高度h后,就可以顺利的算出max(大于h减h的和,h减小于h的和)了,当然因为四舍五入的关系,h可能是h或者h+1,还要跟H比较一下。维护的那两个东西就用树状数组维护就可以了。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
const int maxn = 50000+10;
ll a[maxn*3], t1[maxn], t2[maxn];//t1指<=h个数,t2指<=h的和

inline int lowbit(int x)
{
    return x&(-x);
}

void add(int x, ll k, int flag)
{
    while(x <= maxn)
    {
        if(flag) t1[x] += k;
        else t2[x] += k;
        x += lowbit(x);
    }
}

ll sum(int x, int flag)
{
    ll ret = 0;
    while(x)
    {
        if(flag) ret += t1[x];
        else ret += t2[x];
        x -= lowbit(x);
    }
    return ret;
}

inline void update(ll *a, ll b, ll h)
{
    if(a[0] < 0 || a[0] > b || (a[0] == b && a[1] < h))
    {
        a[0] = b;
        a[1] = h;
    }
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int n,h;
    ll w;
    while(~scanf("%d%I64d%d",&n,&w,&h))
    {
//        cout<<n<<" "<<w<<" "<<h<<endl;
        memset(t1,0,sizeof(t1));
        memset(t2,0,sizeof(t2));
        ll all = 0;
        for(int i = 1; i <= w; i++)
            a[i] = a[i+w+n] = 0;
        for(int i = w+1; i <= w+n; i++)
        {
            scanf("%I64d",&a[i]);
            all += a[i];
        }
        for(int i = 1; i < w; i++)
            add(1,1,0);
        ll suma = 0,ans[2];
        ans[0] = -1;
        for(int i = w; i <= n+w+w; i++)
        {
            int p = (a[i] == 0) ? 1 : a[i];
            add(p,1,0);
            if(i > w)
            {
                if(i <= n+w) add(p,p,1);
                p = (a[i-w] == 0) ? 1 : a[i-w];
                add(p,-1,0);
                if(i-w > w) add(p,-p,1);
            }
            suma += a[i]-a[i-w];
            ll th = suma/w;
            ll tmp[2];
            tmp[0] = -1;
            if(th >= h)
            {
                if(th*w <= all)
                {
                    ll ge = sum(th,0),he = sum(th,1);
                    update(tmp, max(ge*th-he,suma-he-(w-ge)*th),th);
                }
            }
            if(th+1 >= h)
            {
                if((th+1)*w <= all)
                {
                    ll ge = sum(th+1,0),he = sum(th+1,1);
                    update(tmp, max(ge*(th+1)-he,suma-he-(w-ge)*(th+1)),th+1);
                }
            }
            if(tmp[0] == -1)
            {
                if(h*w <= all)
                {
                    ll ge = sum(h,0),he = sum(h,1);
                    update(tmp, max(ge*h-he,suma-he-(w-ge)*h),h);
                }
            }
            update(ans,tmp[0],tmp[1]);
        }
        if(ans[0] == -1) printf("-1\n");
        else printf("%I64d %I64d\n",ans[1],ans[0]);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值