SPOJ:Help BTW(二分)
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。求将最小价值最大化是。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=;
const int inf=;
ll sum[maxn],tsum[maxn],delta[maxn],N,M,K;
bool check(ll Mid){
ll tmp=K;
for(int i=;i<=N;i++) tsum[i]=sum[i],delta[i]=;
for(int i=;i<=N;i++){
delta[i]+=delta[i-];
if(tsum[i]+delta[i]<Mid&&tmp){
ll t=min(tmp,Mid-tsum[i]-delta[i]);
delta[i]+=t;
if(M+i<=N) delta[M+i]-=t;
tmp-=t;
}
tsum[i]+=delta[i];
if(tsum[i]<Mid) return false;
}
return true;
}
int main()
{
int T,i,j;
ll Min;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld",&N,&M,&K); Min=inf;
for(i=;i<=N;i++){
scanf("%lld",&sum[i]);
if(sum[i]<Min) Min=sum[i];
}
ll L=Min,R=Min+K,Mid,ans=Min;
while(L<=R){
Mid=(L+R)>>1LL;
if(check(Mid)) ans=Mid,L=Mid+;
else R=Mid-;
}
printf("%lld\n",ans);
}
return ;
}
SPOJ:Help BTW(二分)的更多相关文章
- SPOJ PHT【二分】+SPOJ INUM【最小/大值重复】
BC 两道其实都是水 没有完整地想好直接就码出事情.wa了一次以后要找bug,找完要把思路理的非常清楚 SPOJ PHT[二分] #include<bits/stdc++.h> using ...
- BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster( 后缀数组 + 二分 + RMQ + 树状数组 )
全部串起来做SA, 在按字典序排序的后缀中, 包含每个询问串必定是1段连续的区间, 对每个询问串s二分+RMQ求出包含s的区间. 然后就是求区间的不同的数的个数(经典问题), sort queries ...
- SPOJ 220 Relevant Phrases of Annihilation(后缀数组+二分答案)
[题目链接] http://www.spoj.pl/problems/PHRASES/ [题目大意] 求在每个字符串中出现至少两次的最长的子串 [题解] 注意到这么几个关键点:最长,至少两次,每个字符 ...
- SPOJ COMPANYS Two Famous Companies 最小生成树,二分,思路 难度:2
http://www.spoj.com/problems/COMPANYS/en/ 题目要求恰好有k条0类边的最小生成树 每次给0类边的权值加或减某个值delta,直到最小生成树上恰好有k条边为0,此 ...
- spoj 287 NETADMIN - Smart Network Administrator【二分+最大流】
在spoj上用题号找题就已经是手动二分了吧 把1作为汇点,k个要入网的向t连流量为1的边,因为最小颜色数等于最大边流量,所以对于题目所给出的边(u,v),连接(u,v,c),二分一个流量c,根据最大流 ...
- SPOJ MINSUB - Largest Submatrix(二分+单调栈)
http://www.spoj.com/problems/MINSUB/en/ 题意:给出一个n*m的矩阵M,和一个面积k,要使得M的子矩阵M'的最小元素最大并且面积大于等于k,问子矩阵M'的最小元素 ...
- SPOJ AMR10E Stocks Prediction --二分求和+矩阵快速幂
题意:给一个递推式S(n) = a1*S(n-1)+...+aR*S(n-R),要求S(k)+S(2k)+...+S(nk)的值. 分析:看到n的大小和递推式,容易想到矩阵快速幂.但是如何转化呢? 首 ...
- SPOJ AMR12A The Black Riders --二分+二分图最大匹配
题意:有n个人,m个洞.每个洞能容纳一个人,每个人到每个洞需要花费一些时间.每个人到达一个洞后可以花C的时间来挖一个洞,并且最多挖一个洞,这样又能多容纳一人.求能使至少K个人进洞的最短时间. 解法:看 ...
- SPOJ TEMPLEQ - Temple Queues(二分查找+树状数组)
题意: 有N个队伍(1 <= N <= 100,000),每个队伍开始有ai个人[0 <= ai<= 100,000,000],有Q个操作[0<=Q<= 500,0 ...
随机推荐
- BZOJ 4810 [Ynoi2017]由乃的玉米田 (莫队 + bitset)
题目链接 BZOJ 4810 首先对询问离线, 莫队算法处理. 首先我们可以用bitset维护处当前区间中是否存在某个数. 对于询问1, 我们可以用 ((f >> q[i].x) &am ...
- mysql索引底层的数据结构和算法
1. 为什么要用索引 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要. 索 ...
- Annotation基本概念,作用以及举例说明。
Annotation即注解,是Jav5新特征,Annotatio提供一些本来不属于程序的数据,用来将任何的信息或元数据(metadata)与程序元素(类.方法.成员变量等)进行关联.为程序的元素(类. ...
- 使用ftrace学习linux内核函数调用
http://www.cnblogs.com/pengdonglin137/articles/4752082.html 转载: http://blog.csdn.net/ronliu/article/ ...
- Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1”
(2)服务器收到http请求报文,返回http响应报文 Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1” Tomcat服务器解析“GET /JavaWebDe ...
- php-cpp用来开发php的拓展
官网: http://www.php-cpp.com/documentation/install, 找时间要学一下.
- python爬虫遇到10060
python爬虫遇到10060 学习了:https://blog.csdn.net/wetest_tencent/article/details/51272981 可以设置代理,可以手动进行图片获取:
- [AngularJS + Unit Testing] Testing a component with requiring ngModel
The component test: describe('The component test', () => { let component, $componentController, $ ...
- [Testing] JavaScript Mocking Fundamentals
Ensure Functions are Called Correctly with JavaScript Mocks Often when writing JavaScript tests and ...
- vim调用python格式化json数据
vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...