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 dist[50005]; int main() { int L;//表示最长的距离 int N;//表示中间的石头数 int M;//表示最多可移除的石头数 cin>>L>>N>>M; int low,high; dist[N+1]=L; low=L; high=L; for(int i=1;i<=N;i++) { cin>>dist[i]; } sort(dist,dist+N+1); for(int i=1;i<=N;i++) { if(low>dist[i]-dist[i-1]) low=dist[i]-dist[i-1]; } while(low<=high) { int mid=(high+low)/2; //判断在当前的mid下移除的的石头数delnum int delnum=0; int sumlength=0; for(int i=1;i<=N;i++) { sumlength=dist[i]-dist[i-1]+sumlength; if(sumlength<mid) { delnum++; } else sumlength=0; } if(delnum<=M) low=mid+1; else high=mid-1; } cout<<high<<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