N堆石头排成一列,每堆有Ai个石子。有M个学生来将所有石头搬走。一开始所有学生都在原点, 每秒钟每个学生都可以在原地搬走一块石头,或者向前移动一格距离,求搬走所有石头的最短时间。
*解法:二分答案x(时间),即每位学生均有x秒的时间,模拟搬石头过程看能否搬完即可(一个人一个人计算,每个人均尽力完成任务即可)。
记得开longlong
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define SZ 100005
#define INF 1e15+10
long long a[SZ], b[SZ];
int n, m;
int check(long long x)
{
int flag = ;
long long p = , t;//时间x
for(int i = ; i <= n; i++) b[i] = a[i];
for(int i = ; i <= m; i++)
{
t = x - p;
while(t > && p <= n)
{
if(t > b[p]) {t -= b[p]; b[p] = ; p++; t--;}
//do{p++; t--;} while(b[p] == 0);
else if(t == b[p]) {b[p] = ; p++; t = ;}
else {b[p] -= t; t = ;}
}
}
for(int i = ; i <= n; i++)
if(b[i] > ) flag = ;
if(!flag) return ;
return ;
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++)
scanf("%lld", &a[i]);
long long l = , r = INF, mid;
while(r > l)
{
mid = (r + l) / ;
if(check(mid)) r = mid;
else l = mid + ;
}
printf("%lld\n", r);
return ;
}

二分+贪心 || CodeForces 551C GukiZ hates Boxes的更多相关文章

  1. Codeforces 551C GukiZ hates Boxes(二分)

    Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量 ...

  2. CodeForces 551C - GukiZ hates Boxes - [二分+贪心]

    题目链接:http://codeforces.com/problemset/problem/551/C time limit per test 2 seconds memory limit per t ...

  3. Codeforces 551C GukiZ hates Boxes 二分答案

    题目链接 题意:  一共同拥有n个空地(是一个数轴,从x=1 到 x=n),每一个空地上有a[i]块石头  有m个学生  目标是删除全部石头  一開始全部学生都站在 x=0的地方  每秒钟每一个学生都 ...

  4. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分

    C. GukiZ hates Boxes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/551/ ...

  5. Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 二分

    C. GukiZ hates Boxes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  6. CF GukiZ hates Boxes 【二分+贪心】

    Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...

  7. 【24.67%】【codeforces 551C】 GukiZ hates Boxes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  8. codeforces 551 C GukiZ hates Boxes

    --睡太晚了. ..脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最開始都站在第一个箱子的左边, 每个人在每一秒钟都必须做出两种选 ...

  9. Codeforces551 C. GukiZ hates Boxes

    二分答案 + 贪心 传送门:$>here<$ $Solution$ 二分时间+贪心验证.思维难度主要在验证上,然而坑人的点却在n的取值上.那么先来谈如何验证.在已知时间的条件下,能否用一种 ...

随机推荐

  1. Java 8中Collection转为Map的方法

    Java 8中java.util.stream.Collectors提供了几个方法可用于把Collection转为Map结构,本文记录了个人对其中三个的理解. Method Return Type g ...

  2. FTP相关内容

    FTP相关介绍 FTP 1)File Transfer Protocol ( FTP ) 是相当古老的网络协议之一,他最主要的功能就是进行 Server端与 Client 端之间的档案传送的功能.这个 ...

  3. J20170507-ts

    プロンプト n. 提示 オブジェクト n. 对象 アスタリスク *  アンパサンド      & スラッシュ       / イテレータ    n 迭代器 差し詰め 当前 スペル spell ...

  4. apcloud混合式开发app学习笔记

    修改图标新建项目检出到本地--------------------------1.api.ajax var loginName = $api.val($api.byId('uname')); var ...

  5. c#数据类型和类型转换

    C# 数据类型 在 C# 中,变量分为以下几种类型: 值类型(Value types) 引用类型(Reference types) 指针类型(Pointer types) 值类型(Value type ...

  6. pika消息中间件模块

    参考: http://www.rabbitmq.com/tutorials/tutorial-one-python.html http://www.rabbitmq.com/tutorials/tut ...

  7. Salazar Slytherin's Locket CodeForces - 855E

    Salazar Slytherin's Locket CodeForces - 855E http://www.cnblogs.com/ftae/p/7590187.html 数位dp: http:/ ...

  8. 题解报告:hdu 1969 Pie(二分)

    Problem Description My birthday is coming up and traditionally I'm serving pie. Not just one pie, no ...

  9. innobackupex的使用

    优点: 不暂停服务器创建Innodb热备份 为mysql做增量的备份 在mysql服务器之间做在线表迁移 使创建mysql replication更加容易 备份mysql但不增加服务器的负载 安装:x ...

  10. NIO客户端主要创建过程

    NIO客户端主要创建过程:   步骤一:打开SocketChannel,绑定客户端本地地址(可选,默认系统会随机分配一个可用的本地地址),示例代码如下:    SocketChannel client ...