BTW wants to buy a gift for her BF and plans to buy an integer array. Generally Integer arrays are costly and hence bought the cheapest one from the market. Her BF is very judgemental and assess the quality of the array by just looking at the smallest element in the array. Hence, she decided to improve the quality of the array. Increment operation on array elements are very costly and would take ONE FULL DAY to complete. Also, Increment operations can be done in parallel on at most M consecutive array elements. She only has K days left. Help BTW calculate the maximum possible “quality” of the array she can attain.

(BTW BTW is the name of the character :P )

Input

Very first line contains T – (number of test cases)

First line in each test case contains N – (size of the array BTW has bought from the market), M, K (days left)

Second line in each test case contains N integers (values of the initial array BTW bought from the market)

Output

Print the maximum possible “quality” she can attain.

Constraints

1<=T<=100

1<=N<=10^5

0<=M<=N

0<=K<=10^9

0<=Values of array<=10^9

Example

Sample test case 1

Input

3 2 1

2 2 3

Output

3

Sample test case 2

Input

3 2 1

2 3 2

Output

2

题意:有N个物品,排成一排,给个物品有一个价值,给定M,K,表示有K次机会,每次将一段连续的不超过M长度的区间的物品价值都加1。求将最小价值最大化是。

思路:开始一看100组数据,二分复杂度是(T*N*log(Min+K))。。。以为过不了,想其他方法又想不出,二分试一试居然过了。。。
           给长度为M的区间都加一个值的操作是:delta数据记录累加前缀和,如果位置i加值val,那么位置i+M减值val,然后就可以二分乱搞了。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. const int maxn=;
  5. const int inf=;
  6. ll sum[maxn],tsum[maxn],delta[maxn],N,M,K;
  7. bool check(ll Mid){
  8. ll tmp=K;
  9. for(int i=;i<=N;i++) tsum[i]=sum[i],delta[i]=;
  10. for(int i=;i<=N;i++){
  11. delta[i]+=delta[i-];
  12. if(tsum[i]+delta[i]<Mid&&tmp){
  13. ll t=min(tmp,Mid-tsum[i]-delta[i]);
  14. delta[i]+=t;
  15. if(M+i<=N) delta[M+i]-=t;
  16. tmp-=t;
  17. }
  18. tsum[i]+=delta[i];
  19. if(tsum[i]<Mid) return false;
  20. }
  21. return true;
  22. }
  23. int main()
  24. {
  25. int T,i,j;
  26. ll Min;
  27. scanf("%d",&T);
  28. while(T--){
  29. scanf("%lld%lld%lld",&N,&M,&K); Min=inf;
  30. for(i=;i<=N;i++){
  31. scanf("%lld",&sum[i]);
  32. if(sum[i]<Min) Min=sum[i];
  33. }
  34. ll L=Min,R=Min+K,Mid,ans=Min;
  35. while(L<=R){
  36. Mid=(L+R)>>1LL;
  37. if(check(Mid)) ans=Mid,L=Mid+;
  38. else R=Mid-;
  39. }
  40. printf("%lld\n",ans);
  41. }
  42. return ;
  43. }

SPOJ:Help BTW(二分)的更多相关文章

  1. SPOJ PHT【二分】+SPOJ INUM【最小/大值重复】

    BC 两道其实都是水 没有完整地想好直接就码出事情.wa了一次以后要找bug,找完要把思路理的非常清楚 SPOJ PHT[二分] #include<bits/stdc++.h> using ...

  2. BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )

    全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...

  3. SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)

    [题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...

  4. SPOJ COMPANYS Two Famous Companies 最小生成树,二分,思路 难度:2

    http://www.spoj.com/problems/COMPANYS/en/ 题目要求恰好有k条0类边的最小生成树 每次给0类边的权值加或减某个值delta,直到最小生成树上恰好有k条边为0,此 ...

  5. spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】

    在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...

  6. SPOJ MINSUB - Largest Submatrix(二分+单调栈)

    http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...

  7. SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂

    题意:给一个递推式S(n) = a1*S(n-1)+...+aR*S(n-R),要求S(k)+S(2k)+...+S(nk)的值. 分析:看到n的大小和递推式,容易想到矩阵快速幂.但是如何转化呢? 首 ...

  8. SPOJ AMR12A The Black Riders --二分+二分图最大匹配

    题意:有n个人,m个洞.每个洞能容纳一个人,每个人到每个洞需要花费一些时间.每个人到达一个洞后可以花C的时间来挖一个洞,并且最多挖一个洞,这样又能多容纳一人.求能使至少K个人进洞的最短时间. 解法:看 ...

  9. SPOJ TEMPLEQ - Temple Queues(二分查找+树状数组)

    题意: 有N个队伍(1 <= N <= 100,000),每个队伍开始有ai个人[0 <= ai<= 100,000,000],有Q个操作[0<=Q<= 500,0 ...

随机推荐

  1. T3139 栈练习3 codevs

    http://codevs.cn/problem/3139/ 题目描述 Description 比起第一题,本题加了另外一个操作,访问栈顶元素(编号3,保证访问栈顶元素时或出栈时栈不为空),现在给出这 ...

  2. 感受lambda之美,推荐收藏,需要时查阅

    一.引言二.java重要的函数式接口1.什么是函数式接口1.1 java8自带的常用函数式接口.1.2 惰性求值与及早求值2.常用的流2.1 collect(Collectors.toList())2 ...

  3. 边看chromium的代码,边想骂人...

    这一年一直在看chromium for android的代码,边看边想骂,谷歌这帮人..一开始搞了个牛逼的架构,在安卓4.4上把以前android webkit团队的简单版替换掉了,结果发现性能大不如 ...

  4. 【Todo】Java类型转换总结

    参考 http://www.cnblogs.com/lwbqqyumidi/p/3700164.html 这篇文章也可以对照着看:http://www.360doc.com/content/10/09 ...

  5. Application具体解释(一)

     1:Application是什么? Application和Activity,Service一样,是android框架的一个系统组件.当android程序启动时系统会创建一个 applicati ...

  6. 嵌入式程序员应知道的0x10个C语言Tips

    [1].[代码] [C/C++]代码 跳至 [1] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  7. JNI——访问数组

    JNI在处理基本类型数组和对象数组上面是不同的.对象数组里面是一些指向对象实例或者其它数组的引用. 因为速度的原因,先通过GetXXXArrayElements函数把简单类型的数组转化成本地类型的数组 ...

  8. Ubuntu下编译Android JNI实例全过程

    第一步:保证make和gcc可用 在shell中输入make-v.不报错就是对的.(可參考http://wenku.baidu.com/view/d87586c24028915f804dc24a.ht ...

  9. Serial attached SCSI

    http://en.wikipedia.org/wiki/Serial_attached_SCSI Serial attached SCSI From Wikipedia, the free ency ...

  10. hihoCoder 1234 fractal

    #1234 : Fractal 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 This is the logo of PKUACM 2016. More specific ...