2019 Multi-University Training Contest 3 Distribution of books (离散化+线段树优化dp)

22 篇文章 0 订阅

Distribution of books

Time Limit: 8000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1324    Accepted Submission(s): 514


 

Problem Description

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

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.

 

 

Output

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

 

 

Sample Input

 

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

 

 

Sample Output

 

2 -1

Hint

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.

 

 

Source

2019 Multi-University Training Contest 3

 

 

Recommend

chendu   |   We have carefully selected several similar problems for you:  6730 6729 6728 6727 6726 

 题目大意:给了n本书,要求将前若干本书分成k堆,使得每一对的和的最大值最小。

解题思路:最大值最小,考虑二分。 

如何check ,将前缀和离散化。用dp[i]表示前i本书,每一堆的和不超过mid的最大值。

如何转移  dp[i]=max(dp[j])+1;  j<=i 并且 sum[i]-sum[j]<=mid.

一些细节。我们需要将0一起离散化。并且将rak[0]的位置初始化为0,其他初始化为-inf。

#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)

typedef long long ll;
const int N = 2e5+5;
const int inf = -11111111;
ll a[N],pre[N],b[N];
int rnk[N],cnt[N];

int n,k;

int t[N*4];

void build(int rt,int l,int r)
{
    if(l==r)
    {
        t[rt]=inf ;
        return ;
    }
    int m=(l+r)>>1;
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
    t[rt]=max(t[rt<<1],t[rt<<1|1]);
}

int query(int rt,int l,int r,int ql,int qr)
{
    if(l>=ql && r<=qr)
    {
        return t[rt];
    }
    int m=(l+r)>>1;
    int q1=inf   ,q2=inf ;
    if(ql<=m) q1=query(rt<<1,l,m,ql,qr);
    if(qr>m) q2=query(rt<<1|1,m+1,r,ql,qr);
    return max(q1,q2);
}

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

bool check(ll mid)
{
    build(1,0,n+1);
    upd(1,0,n+1,rnk[0],0);
    for(int i=1;i<=n;i++)
    {
        int l=lower_bound(b,b+1+n,pre[i]-mid)-b;
        upd(1,0,n+1,rnk[i],query(1,0,n+1,l,n+1)+1);
    }
    return query(1,0,n+1,0,n)>=k;
}


int main()
{
    int _;
    cin>>_;
    while(_--)
    {
        scanf("%d%d",&n,&k);
        memset(cnt,0,sizeof(cnt));
        for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
        for(int i=1;i<=n;i++)pre[i]=pre[i-1]+a[i];
        for(int i=0;i<=n;i++)b[i]=pre[i];
        sort(b,b+1+n);

        for(int i=0;i<=n;i++)
        {
            int p=lower_bound(b,b+1+n,pre[i])-b;
            rnk[i]=p+cnt[p];    //注意这里,相同的数多次出现赋值不同的rnk。
            cnt[p]++;
        }

        ll l=-1e15,r=1e15;
        ll ans=0;
        while(l<=r)
        {
            ll mid=(l+r)>>1;
            if(check(mid))r=mid-1,ans=mid;
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值