Codeforces551C:GukiZ hates Boxes(二分+贪心)

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:

  1. If i?≠?n, move from pile i to
    pile i?+?1;
  2. 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.

Input

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.

Output

In a single line, print one number, minimum time needed to remove all the boxes in seconds.

Sample test(s)

input

2 1
1 1

output

4

input

3 2
1 0 2

output

5

input

4 100
3 4 5 4

output

5

Note

First sample: Student will first move to the first pile (1 second), then remove box from first pile (1 second),
then move to the second pile (1 second) and finally remove the box from second pile (1 second).

Second sample: One of optimal solutions is to send one student to remove a box from the first pile and a box from the third pile, and send another student to remove a box from the third pile. Overall, 5 seconds.

Third sample: With a lot of available students, send three of them to remove boxes from the first pile, four of them to remove boxes from the second pile, five of them to remove boxes from the third pile, and four of them to remove boxes from the fourth pile.
Process will be over in 5 seconds, when removing the boxes from the last pile is finished.

题意:

有n个地点,每个地点有a[i]个箱子,然后有m个人,每个人从第i个位置移动到第i+1的位置花费1S,每个人移动一个箱子花费1S,要求把所有箱子全部移除花费的最小时间是多少

思路:

先二分枚举答案,然后对每个答案判断是否能够满足即可

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 100005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;

int n,m,tot;
LL a[N];

int check(LL x)
{
    int cnt = m;
    int i,j,k;
    LL s = 0;
    for(i = 1; i<=tot; i++)
    {
        s+=a[i];
        while(s+i>=x)//移动箱子加移动路程的总时间超过了x
        {
            s-=x-i;//对于已走的路程i,那么一个人有x-i个时间来搬箱子,所以每增加一人,时间消耗可以减少x-i
            cnt--;
            if(cnt<0) return 0;
        }
    }
    if(cnt==0) return s<=0;
    return 1;
}

int main()
{
    int i,j;
    LL ans,sum,l,r;
    while(~scanf("%d%d",&n,&m))
    {
        sum = 0;
        for(i = 1; i<=n; i++)
        {
            scanf("%I64d",&a[i]);
            sum+=a[i];
            if(a[i])
                tot = i;
        }
        l = tot,r = tot+sum;
        while(l<=r)
        {
            LL mid = (l+r)>>1;
            if(check(mid))
            {
                ans = mid;
                r = mid-1;
            }
            else
                l = mid+1;
        }
        printf("%I64d\n",ans);
    }

    return 0;
}
时间: 2024-07-31 22:23:30

Codeforces551C:GukiZ hates Boxes(二分+贪心)的相关文章

Codeforces 551C GukiZ hates Boxes(二分)

Problem C. GukiZ hates Boxes Solution: 假设最后一个非零的位置为K,所有位置上的和为S 那么答案的范围在[K+1,K+S]. 二分这个答案ans,然后对每个人尽量在ans时间内搬最多的砖.可以得出至少需要多少个人才能搬完.这个可以在O(n)的时间内利用贪心得到 只要判断需要的人数是否小于等于实际的人数就好了 时间复杂度O(nlogS) #include <bits/stdc++.h> using namespace std; const int N = 1

Codeforces 551C - GukiZ hates Boxes(二分加贪心)

题意: 就是n个学生帮助教授搬箱子, 箱子分成m 堆, 每个学生每秒可以选择的两个操作 操作1,为从i堆迈向第i+1堆, 操作2,从i-1堆箱子中帮忙抱走一个箱子 问抱走所有箱子的最少时间为多少, 每个学生每秒可以同时行动 题解: 二分时间   然后再当前时间下,一个一个派出所有的学生,使其走到他能走的最远距离 代码: #include<stdio.h> int flag, value[100005], value2[100005], n, m; void find(long long int

Codeforces 551C GukiZ hates Boxes 二分答案

题目链接 题意: 一共有n个空地(是一个数轴,从x=1 到 x=n),每个空地上有a[i]块石头 有m个学生 目标是删除所有石头 一开始所有学生都站在 x=0的地方 每秒钟每个学生都可以在原地删除一块石头,或者向 → 移动一格距离 问:删除所有石头的最短时间 案例解析: 3 2 1 0 2 第一个学生第一秒向→走,第二秒删a[1]的一块石头 第二个学生一直走到头,删掉a[3] ,所以第二个学生花费是 1+1+1+2 = 5 两个学生可以同时运动. 思路: 二分答案,设答案为x,即需要x秒来搬完石

CodeForces 551C GukiZ hates Boxes

题目链接:CodeForces 551C GukiZ hates Boxes 解题思路: 题目要求最短时间,因此我们可以先考虑最长时间,最长时间一定是一个学生从开始走到结束,每走到一处就把该处箱子搬空,所以最长时间等于走到最后一个有箱子格子的步数ed加箱子总数sum. 接下来二分所需时间,直接搜索最短时间,每次都对假设时间进行判断.每次判断都从第一格开始,因为要搬空一个格子的箱子一定需要学生走到该处,同时所有学生可以同时移动,所以从头到尾判断要搬空该处需要多少学生,再与有的学生数量进行比较. 代

codeforces 551 C GukiZ hates Boxes

--睡太晚了...脑子就傻了-- 这个题想的时候并没有想到该这样-- 题意大概是有n堆箱子从左往右依次排列,每堆ai个箱子,有m个人,最开始都站在第一个箱子的左边, 每一个人在每一秒钟都必须做出两种选择中的一种:1若他的位置有箱子则搬走一个箱子,2往右走一步. 问把所有箱子都搞掉的最少时间-- 很显然二分一下答案,若为x秒,则每个人都有x秒,一个一个排出去搬,看是否能够搬完-- 我竟然没想到-- #include<map> #include<string> #include<

二分+贪心

上海邀请赛热身时候,C题是一个二分+贪心的题目.起初并不会,问了旁边的复旦大神.这几天无意发现VJ上一个专题.擦原来是一个经典类型. 二分+贪心 这类题目注意数据范围,1e8,1e9一般都是这样. 注意事项 二分法有很多写法,推荐用lf+1 < rf的写法.这个也符合计算机中数据存取的原则.对于浮点数,直接就循环100次,精度绝对够. 一般有两种类型,一种是询问最优,即数列中无重复.一种是多个即lower_bound ,upper_bound这类函数问题. 贪心使用,就是这个问题枚举答案可被验证

nyoj586||poj2456 二分+贪心

完全看不懂题意....百度搜搜才看懂题意  然后就参考代码了 和yougth的最大化()nyoj914差不多的方法 二分+贪心 #include <stdio.h> #include <algorithm> using namespace std; int c,a[100005],n; bool judge(int k) { int p=a[0],cnt=1;//也就这里注意点 从1开始 自己想想为啥 for(int i=1;i<n;i++) { if(a[i]-p>=

HDU 4004 The Frog&#39;s Games 二分 贪心

戳这里:HDU 4004 //思路:二分经典入门题...贪心判方案是否可行 1 #include "bits/stdc++.h" 2 using namespace std; 3 int L, n, m; 4 int pos[500010], dis[500010]; 5 6 bool Cant(int Dis_Jump) 7 { 8 int i, Dis_Sum = 0, Count = 0; 9 for(i = 1; i <= n + 1; ++i) { 10 if(dis[

贪心(bnuoj49103+二分+贪心)

贪心 小明喜欢养小鸡,小鸡喜欢吃小米.小明很贪心,希望养s只不同种类的小鸡,小鸡也很贪心,每天除了吃固定的ai粒小米外,还想多吃bi*s粒小米. 小明每天有M(0<=M<=10^9)粒小米可以喂小鸡,小鸡共有N(0<=N<=1000)种.问小明最多可以养多少只小鸡? Input 多组数据,请读到文件尾 第一行,整数N,M,以空格分隔,之后两行,第一行为N个整数ai,第二行为N个整数bi. ai.bi都在int范围内 Output 一行一个整数,s. Sample Input 2 4