codeforce 551C GukiZ hates Boxes(二分+贪心)
2017-12-02 19:26
417 查看
C. GukiZ hates Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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.
Examples
input
output
input
output
input
output
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 (1second) 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个空地(1~n),空地上有a[i]块石头,m个学生,每个学生在0这个位置,有两个操作均需要消耗1秒
操作1:将当前所在位置的石头删除
操作2:往右走一个位置,问删除所有空地上的石头需要花费的最少时间
二分枚举答案,贪心判断是否满足 即可。关键在于如何贪心,我们可以 从1向最后一个有石头的位置枚举(因为后面的都无关了,而且这样做方便后面的贪心),将中间遇到的石头都加起来,如果此时到当前位置所需花费时间+删除石头所需时间>=枚举值,这时候就需要一个学生,从0~枚举时间 对这段区间进行删除石头的操作,此时可以完美利用该学生,使其在枚举的时间内一刻不停地干活,剩下以此类推
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,tot;
ll a[100005];
bool solve(ll limit)
{
ll cnt=m,sum=0,k=0;
for(int i=1;i<=tot;i++)
{
sum+=a[i];
while(sum+i>=limit)//如果>=枚举时间,就需要一个学生
{
sum-=limit-i;
cnt--;
//学生不够用了,枚举时间不满足
if(cnt<0)return false;
}
}
//学生数为0,且石头搬完返回真
if(cnt==0)return sum<=0;
//学生没用完,那就更应该返回真了
return true;
}
int main()
{
ll sum=0,ans;
cin>>n>>m;
for(ll i=1; i<=n; i++)
{
cin>>a[i];
sum+=a[i];
if(a[i])tot=i;
}
ll l=tot,r=tot+sum,mid;
while(l<=r)
{
mid=(l+r)/2;
if(solve(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%lld\n",ans);
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
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.
Examples
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 (1second) 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个空地(1~n),空地上有a[i]块石头,m个学生,每个学生在0这个位置,有两个操作均需要消耗1秒
操作1:将当前所在位置的石头删除
操作2:往右走一个位置,问删除所有空地上的石头需要花费的最少时间
二分枚举答案,贪心判断是否满足 即可。关键在于如何贪心,我们可以 从1向最后一个有石头的位置枚举(因为后面的都无关了,而且这样做方便后面的贪心),将中间遇到的石头都加起来,如果此时到当前位置所需花费时间+删除石头所需时间>=枚举值,这时候就需要一个学生,从0~枚举时间 对这段区间进行删除石头的操作,此时可以完美利用该学生,使其在枚举的时间内一刻不停地干活,剩下以此类推
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,tot;
ll a[100005];
bool solve(ll limit)
{
ll cnt=m,sum=0,k=0;
for(int i=1;i<=tot;i++)
{
sum+=a[i];
while(sum+i>=limit)//如果>=枚举时间,就需要一个学生
{
sum-=limit-i;
cnt--;
//学生不够用了,枚举时间不满足
if(cnt<0)return false;
}
}
//学生数为0,且石头搬完返回真
if(cnt==0)return sum<=0;
//学生没用完,那就更应该返回真了
return true;
}
int main()
{
ll sum=0,ans;
cin>>n>>m;
for(ll i=1; i<=n; i++)
{
cin>>a[i];
sum+=a[i];
if(a[i])tot=i;
}
ll l=tot,r=tot+sum,mid;
while(l<=r)
{
mid=(l+r)/2;
if(solve(mid))
{
ans=mid;
r=mid-1;
}
else
l=mid+1;
}
printf("%lld\n",ans);
}
相关文章推荐
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes 贪心/二分
- codeforce 825E. Minimal Labels 拓扑排序 贪心思想
- T-shirt buying Codeforce 贪心
- codeforce 3B. Lorry(贪心)
- 【贪心+二分查找】Office Keys CodeForces - 830A
- 【Codeforces Round 339 (Div 2)D】【暴力 贪心 二分】Skills 最高技能人数乘cm+最低技能乘cf权值最大
- codeforce 731C Socks(并查集+贪心)
- 洛谷1084/codevs1218 二分+倍增+贪心,疫情控制,分步讲解
- Codeforce 487B Strip 动归+二分+线段树
- codevs 2744 养鱼喂妹纸(二分+贪心)
- coderforce Educational Codeforces Round 10 C. Foe Pairs(贪心)
- codeforce 689C 【二分】
- Codeforce 839 -- B Game of the Rows 【思维 + 贪心】
- Codeforces Round #352 (Div. 2) D 二分贪心两头 + 细节
- Codeforces Round #211 (Div. 2) D题(二分,贪心)解题报告
- Codeforecs 493C Vasya and Basketball 贪心+二分
- CodeVS1725 探险 【二分答案】【贪心】
- Codeforce~545C - Woodcutters【贪心】
- Codeforce round# 380 E(贪心,构造)
- Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]