Distribution of books

题目描述

zz6d likes reading very much, so he bought a lot of books. One day, zz6d brought n books to a classroom in school. The books of zz6d is so popular that K students in the classroom want to borrow his books to read. Every book of zz6d has a number i (1<=i<=n). Every student in the classroom wants to get a continuous number books. Every book has a pleasure value, which can be 0 or even negative (causing discomfort). Now zz6d needs to distribute these books to K students. The pleasure value of each student is defined as the sum of the pleasure values of all the books he obtains.Zz6d didn't want his classmates to be too happy, so he wanted to minimize the maximum pleasure of the K classmates. zz6d can hide some last numbered books and not distribute them,which means he can just split the first x books into k parts and ignore the rest books, every part is consecutive and no two parts intersect with each other.However,every classmate must get at least one book.Now he wonders how small can the maximum pleasure of the K classmates be.

1<=T<=10 
1<=n<=2*105 
1<=k<=n 
-109<=ai<=109

 

输入

Input contains multiple test cases. 
The first line of the input is a single integer T which is the number of test cases. T test cases follow. 
For each test case, the first line contains two integer n,k: the number of books and the number of his classmates. The second line contains n integers a1 ,a2,…, an−1,an. (aimeans the pleasure value of book i.)∑n<=2*105.

 

输出

For each case, print the smallest maximum pleasure of the K classmates, and one line one case.

 

样例输入

复制样例数据

2
4 2
3 -2  4 -2
5 4
-1 -1 -1 -1 6

样例输出

2
-1

 

提示

In the first example,classmate 1 get book 1,2, classmate 2 get book 3,4.the maximum pleasure is max((3-2),(4-2))=2;

In the second example,he can ignore book 5 and spilt the first 4 books into 4 parts,give them to his classmates.

参考:https://www.cnblogs.com/pkgunboat/p/11267824.html

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn=200050;
ll sum[maxn],a[maxn],b[maxn];
int dp[maxn],mx[maxn<<2];
int n,m,tot;

void build(int rt,int l,int r){
    if(l==r){
        mx[rt]=-1;
        return;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}

void update(int rt,int l,int r,int pos,int val){
    if(l==r){
        mx[rt]=max(mx[rt],val);
        return;
    }
    int mid=(l+r)>>1;
    if(pos<=mid){
        update(rt<<1,l,mid,pos,val);
    }
    else{
        update(rt<<1|1,mid+1,r,pos,val);
    }
    mx[rt]=max(mx[rt<<1],mx[rt<<1|1]);
}

int ask(int rt,int l,int r,int ql,int qr){
    if(ql>qr){
        return -1;
    }
    if(l>=ql&&r<=qr){
        return mx[rt];
    }
    int ans=-1,mid=(l+r)>>1;
    if(ql<=mid){
        ans=max(ask(rt<<1,l,mid,ql,qr),ans);
    }
    if(qr>mid){
        ans=max(ask(rt<<1|1,mid+1,r,ql,qr),ans);
    }
    return ans;
}

bool check(ll mid){
    build(1,1,tot);
    update(1,1,tot,lower_bound(b+1,b+1+tot,0)-b,0);
    for(int i=1;i<=n;++i){
        int pos=lower_bound(b+1,b+1+tot,sum[i]-mid)-b;
        int pos1=lower_bound(b+1,b+1+tot,sum[i])-b;
        int tmp=ask(1,1,tot,pos,tot);
        if(tmp==-1){
            dp[i]=-1;
        }
        else{
            dp[i]=tmp+1;
        }
        if(dp[i]>=m){
            return false;
        }
        update(1,1,tot,pos1,dp[i]);
    }
    return true;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;++i){
            scanf("%lld",&a[i]);
            sum[i]=sum[i-1]+a[i];
            b[i]=sum[i];
        }
        b[n+1]=0;
        sort(b+1,b+2+n);
        tot=unique(b+1,b+2+n)-(b+1);
        ll l=(ll)-2e14,r=(ll)2e14;
        while(l<r){
            ll mid=(l+r)>>1;
            if(check(mid)){
                l=mid+1;
            }
            else{
                r=mid;
            }
        }
        printf("%lld\n",l);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值