CodeForces 551C - GukiZ hates Boxes - [二分+贪心]
题目链接:http://codeforces.com/problemset/problem/551/C
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.
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.
In a single line, print one number, minimum time needed to remove all the boxes in seconds.
2 1
1 1
4
3 2
1 0 2
5
4 100
3 4 5 4
5
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.
题目大意:
有m个学生帮着老师搬箱子,箱子有n堆排成一排,每堆箱子有a[i]个箱子;
一开始学生都在第1堆箱子左边,然后每一秒钟每个学生可以有两种操作:1是往后移动一格( i -> i+1 ),2是搬走一个箱子;
问需要花费最少时间是多少。
题解:
首先OB一下数据范围,n和m达到1e5,那么复杂度最多O(nlogn);a[i]达到1e9,那么为了安全就用long long;
对于任意一排的成堆箱子,最慢的情况无非是只有一个学生,一个个搬走,所花时间为ed+sum;
ed为最后一个非零a[i]的i,sum为Σa[i];
所以可以在1~ed+sum进行二分搜索答案time;
那么对于如何测试time是否可以满足m个学生把所有箱子搬完?
可以通过O(n)的遍历a[1]~a[ed]的箱子,计算出需要多少学生,在与m进行比较即可。
AC代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e5+5; ll n,m,a[maxn],ed; bool test_ok(ll time) { ll acc=0; ll p=0; for(ll i=1;i<=ed;i++) { acc+=a[i]; while(i+acc>=time) { if(i>=time) return 0; acc-=(time-i); p++; } } if(p<m) return 1; else if(p==m && acc==0) return 1; else return 0; } int main() { scanf("%I64d%I64d",&n,&m); ll sum=0; for(ll i=1;i<=n;i++) { scanf("%I64d",&a[i]); sum+=a[i]; if(a[i]!=0) ed=i; } ll left=1,right=ed+sum,mid; while(right-left>1) { mid=(left+right)/2; if(test_ok(mid)) right=mid; else left=mid; } printf("%I64d\n",right); }