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 ...
随机推荐
- intellij idea 和 myeclipse 转换
原文出处:http://chinaxxren.iteye.com/blog/893970 当只用 intellij idea 建立 工程 1.首先是new project--->create p ...
- Codeforces 833B The Bakery(主席树 + 决策单调性优化DP)
题目链接 The Bakery 题目大意:目标是把$n$个数分成$k$组,每个组的值为这个组内不同的数的个数,求$k$个组的值的和的最大值. 题目分析: 这道题我的解法可能和大众解法不太一样……我用主 ...
- ubuntu下U盘变为只读
原文地址:http://www.cnblogs.com/coding-way/p/4243331.html 首先执行命令: tail -f /var/log/syslog 然后插入有问题的U盘,tai ...
- 利用BURPSUITE检测CSRF漏洞
CSRF漏洞的手动判定:修改referer头或直接删除referer头,看在提交表单时,网站是否还是正常响应. 下面演示用Burpsuite对CSRF进行鉴定. 抓包. 成功修改密码完成漏洞的利用.
- Spring实战Day7面向切面编程术语介绍
#### 面向切面编程 为什么需要切面? 有些功能需要在应用中的多个地方使用到,但是我们又不想在着每个地方都调用他们 切面术语 通知(advice):切面需要完成的工作 通知的类型(什么时间完成工作) ...
- 33.Search in sorted Array
/* * 33.Search in sorted Array * 2016-4-19 by Mingyang * 我自己写的代码,开始没有考虑[3,1]取1得情况,所以现在需要额外的加一个部分来 * ...
- bash 文件头尾插入字符
头部插入:sed -i '1i\Insert this line' file.txt 尾部插入:echo "hehe" >> tmp.txt
- 杭电1863 畅通project
畅通project Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- php编译安装后,加扩展模块
1.进入php源码包中,找到需要安装的扩展模块目录. cd /root/php-5.6.26/ext/mbstring 2.在扩展模块目录,运行phpize程序. /usr/local/bin/php ...
- HDU 2845 Beans (两次线性dp)
Beans Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...