Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
唔 这种二分的终止条件还是没想太明白 贴个代码以后再看#include <iostream> #include <algorithm> using namespace std; int money[100005]; int main() { int N;//总共的天数 int M;//分成fajomonths月数 cin>>N>>M; int low=0; int high=0; for(int i=1;i<=N;i++) { cin>>money[i]; high+=money[i]; if(money[i]>low) low=money[i]; } int mid; while(low<=high) { mid=(low+high)/2; int sum=0; int duanshu=1; for(int i=1;i<=N;i++) { sum+=money[i]; if(sum<=mid) ;//计算在月最大消费控制在mid下时能分的段数 else { sum=money[i]; duanshu++; } } if(duanshu>M)//分的段数比需要的大,说明标准高了 low=mid+1; else if(duanshu<M)//分的段数比需要的小,说明标准低了 high=mid-1; else if(duanshu==M)//分的段数和需要的相同,这时还有可能再严格一点,即使每月的最大支出更小一点 high=mid-1; } cout<<low<<endl; return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator