Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
2015-06-13 03:33
417 查看
C. GukiZ hates Boxes
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/contest/551/problem/CDescription
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are blocking his way.In total there are n piles of boxes, arranged in a line, from left to right, i-th pile (1 ≤ i ≤ n) containing ai boxes. Luckily, m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time 0, all students are located left of the first pile. It takes one second for every student to move from this position to the first pile, and after that, every student must start performing sequence of two possible operations, each taking one second to complete. Possible operations are:
If i ≠ n, move from pile i to pile i + 1;
If pile located at the position of student is not empty, remove one box from it.
GukiZ's students aren't smart at all, so they need you to tell them how to remove boxes before professor comes (he is very impatient man, and doesn't want to wait). They ask you to calculate minumum time t in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner after t seconds, but all the boxes must be removed.
[b]Input[/b]
The first line contains two integers n and m (1 ≤ n, m ≤ 105), the number of piles of boxes and the number of GukiZ's students.
The second line contains n integers a1, a2, ... an (0 ≤ ai ≤ 109) where ai represents the number of boxes on i-th pile. It's guaranteed that at least one pile of is non-empty.
[b]Output[/b]
In a single line, print one number, minimum time needed to remove all the boxes in seconds.
[b]Sample Input[/b]
2 1
1 1
[b]Sample Output[/b]
4
HINT
[b]题意[/b]有一群小学生帮着老师搬东西,一开始小学生都在0点,然后每次每个小学生有两种操作,1是移动一格,2是搬走一堆东西
然后问你花费最少的时间
[b]题解:[/b]
想dp的都是傻逼(没错就是我!
直接二分答案之后,然后贪心就好了……
[b]代码:[/b]
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** ll a[maxn],b[maxn]; int n,m; bool check(ll x) { for(int i=1;i<=n;i++) b[i]=a[i]; ll tmp=0; int cur=n; for(int i=1;i<=m;i++) { while(cur>=1&&b[cur]==0) cur--; if(cur==0) return 1; tmp=cur; if(tmp>x) continue; while(cur>=1&&b[cur]+tmp<=x) { tmp+=b[cur]; b[cur]=0; cur--; } if(cur==0) return 1; b[cur]-=(x-tmp); } return 0; } int main() { //test; ll sum=0; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; } sum+=n; ll L=0,R=sum; while(L<R) { ll M=(L+R)>>1; if(check(M)) R=M; else L=M+1; } printf("%lld\n",R); return 0; }
相关文章推荐
- android 读取手机硬件信息,我这里只读出SMEI码
- Codeforces Round #307 (Div. 2) B. ZgukistringZ 暴力
- android 我用过的一些权限,网络、SD卡、手机硬件信息、自动横屏、键盘自动弹出
- Codeforces Round #307 (Div. 2) A. GukiZ and Contest 水题
- android 手机系统日期及时间的读取
- 初识Maven
- Android 调用系统发送短信界面,预设号码和短信内容
- android 自定标题栏的应用
- c 语言文本文件判断是否到达结尾的问题
- iOS 项目中用到的一些 iOS 开源库和第三方组件
- android 把1位数,补0变成2位数
- android 获取手机内存大小
- iOS 8 Tableview根据AutoLayout自动调整高度,iostableview高度
- Linux环境下MySQL设置gbk编码
- android 缓存读取与清除
- Java学习篇之---Maven开发跳过单元测试
- 执行shell脚本中declare: not found
- php操作mysqli(示例代码)
- ubuntu qq
- 图像局部不变性特征描述