Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!
来源:互联网 发布:ubuntu xfce4美化 编辑:程序博客网 时间:2024/04/27 22:21
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) containingai boxes. Luckily,m students are willing to help GukiZ by removing all the boxes from his way. Students are working simultaneously. At time0, 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 timet in seconds for which they can remove all the boxes from GukiZ's way. Note that students can be positioned in any manner aftert seconds, but all the boxes must be removed.
The first line contains two integers n andm (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) whereai represents the number of boxes oni-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 11 1
4
3 21 0 2
5
4 1003 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个人,一开始所有人都站在第一个地方的左边,每个地方都有ai个物品需要拿走,每秒每个人可以进行两种操作:
①从i走到i+1。
②除掉一个物品。
问最少需要多少秒将所有物品都去掉。
思路:
1、考虑到时间的单调性:时间越长,完成任务的进度就越大。
那么我们可以考虑二分枚举时间,对于当前枚举出来的量进行测试,对于结果继续寻找区间二分。
2、那么对于当前二分出来的值mid如何判断能否完成任务呢?
对于二分出来的值mid,是一个时间量,那么对于每一个人来讲,每一个人都是有mid这么些秒可以进行操作的。
那么我们不如一个人一个人的派出去,对于每一个人,都尽力完成任务(遇到一个ai>0的位子,就要努力去除物品。)
一点一点处理,时间复杂度O(M);我们每一个派出去的人之前完成的部分我们就没有必要再一个一个格子去走了,维护一个pos,表示上一个人最后停在的位子即可。
最后O(n)枚举,看看是否每个格子的物品都去除干净了,对于结果继续寻找二分区间即可。
AC代码:
#include<stdio.h>#include<string.h>#include<stack>using namespace std;#define ll __int64ll n,m,tot;ll a[100060];ll b[100060];int Slove(ll mid){ for(int i=0;i<n;i++)a[i]=b[i]; int pos=0; for(int i=1;i<=m;i++) { ll tim=mid-(pos+1); while(tim>0) { if(pos==n)return 1; while(a[pos]==0&&pos<n&&tim>0)pos++,tim--; if(tim>0) { if(a[pos]>=tim) { a[pos]-=tim; tim=0; } else { tim-=a[pos]; a[pos]=0; } } else break; } } for(int i=0;i<n;i++) { if(a[i]>0)return 0; } return 1;}int main(){ while(~scanf("%I64d%I64d",&n,&m)) { tot=0; for(int i=0;i<n;i++)scanf("%I64d",&b[i]); ll ans=-1; ll l=0; ll r=10000000000000000; while(r-l>=0) { ll mid=(l+r)/2; if(Slove(mid)==1) { ans=mid; r=mid-1; } else l=mid+1; } printf("%I64d\n",ans); }}
- Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!
- Codeforces 551C - GukiZ hates Boxes (二分 + 贪心)
- Codeforces 551C - GukiZ hates Boxes(二分加贪心)
- CodeForces 551 C. GukiZ hates Boxes(二分+贪心)
- Codeforces 551C GukiZ hates Boxes 二分答案
- Codeforces 551C GukiZ hates Boxes(二分答案)
- codeforces 551 C GukiZ hates Boxes
- 【24.67%】【codeforces 551C】 GukiZ hates Boxes
- codeforces-551C GukiZ hates Boxes
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes(二分)
- codeforces #307 C. GukiZ hates Boxes(贪心+二分)
- 551C GukiZ hates Boxes
- GukiZ hates Boxes 二分
- 二分答案找最优解 codeforces problem/551/C GukiZ hates Boxes
- codeforce 551C GukiZ hates Boxes(二分+贪心)
- codeforce 551C GukiZ hates Boxes
- Codeforces Round #307 (Div. 2) C. GukiZ hates Boxes (贪心 + 二分)
- [二分、贪心]Codeforces Round #307 (Div. 2) C.GukiZ hates Boxes
- Angular学习:控制器(未翻译完)
- 小公司个人技术成长路线思考
- NOIP2016总结
- 多进程 fork()与vfork()
- 【完美解决系列】Can't finish GitHub sharing process
- Codeforces 551C GukiZ hates Boxes【二分+思维处理】好题!
- spring boot 学习(九)小工具篇:?秒防刷新
- 关于Linux的 High-Resolution Timer
- POJ1852_Ants(考察想象力)
- java-ocr技术
- 数据挖掘-K-近邻分类器-Iris数据集分析-PCA降维处理后显示分类情况(三)
- leetcode 283 Move Zeroes
- HDU4403:A very hard Aoshu problem(DFS暴搜)
- Codeforces 551B ZgukistringZ【思维+枚举】