Time Limit: 8000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 411 Accepted Submission(s): 135
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*
1<=k<=n
-<=<=
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 ,,…, ,. (means the pleasure value of book i.)n<=2*.
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.
【题意】
给n本书,k个人,每本书有个权值,可以丢掉一些后缀,前面的书分给k个人。使得区间权值和最大的最小。
【题解】
【代码】
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,k,tot;
ll a[N],s1[N],s[N];
int pre[N],mx[N*4];
int getid(ll x)
{
return lower_bound(s1+1,s1+1+tot,x)-s1;
}
void up(int id,int l,int r,int pos,int val)
{
if(l==r){
mx[id]=val;
return ;
}
int mid=l+r>>1;
if(pos<=mid) up(id<<1,l,mid,pos,val);
else up(id<<1|1,mid+1,r,pos,val);
mx[id]=max(mx[id<<1],mx[id<<1|1]);
}
int qu(int id,int l,int r,int ql,int qr)
{
if(ql<=l&&r<=qr) return mx[id];
int mid=l+r>>1;
int res=0;
if(ql<=mid) res=max(res,qu(id<<1,l,mid,ql,qr));
if(qr>mid) res=max(res,qu(id<<1|1,mid+1,r,ql,qr));
return res;
}
bool valid(ll mid)
{
for(int i=0;i<=4*tot;++i) mx[i]=0;
rep(i,1,n)
{
ll val=s[i]-mid;
int ql=getid(s[i]-mid);
int v=qu(1,1,tot,ql,tot);
if (v == k - 1) {
if (v || (!v && s[i] <= mid))
return 1;
}
if (v || (!v && s[i] <= mid))
up(1, 1, tot, pre[i], v + 1);
}
return 0;
}
int main()
{
int _;
cin>>_;
while(_--)
{
scanf("%d%d",&n,&k);
rep(i,0,n) s1[i]=s[i]=0;
ll mn=1e9;
rep(i,1,n)
{
scanf("%lld",&a[i]);
s1[i]=s1[i-1]+a[i];
s[i]=s1[i];
mn=min(mn,a[i]);
}
sort(s1+1,s1+1+n);
tot=unique(s1+1,s1+1+n)-s1-1;
rep(i,1,n) pre[i]=getid(s[i]);
ll l,r,ans;
ll M=abs(mn*n);
l=0,r=M+1e9;
ans=l;
while(l<=r)
{
ll mid=l+r>>1;
if(valid(mid-M))
{
ans=mid,r=mid-1;
}
else l=mid+1;
}
printf("%lld\n",ans-M);
}
}