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 |
Re:AC代码,二分查找In Reply To:AC代码,二分查找 Posted by:zxj1015 at 2015-06-20 22:46:05 > #include <stdlib.h> > #include <stdio.h> > #include <vector> > #include <queue> > #include <stack> > #include <string> > #include <set> > #include <map> > #include <algorithm> > #include <cmath> > #include <utility> > #include <set> > #include <functional> > #include <string.h> > #include <iostream> > #include <numeric> > #include <list> > > using namespace std; > > > > int main() { > int n, k; > scanf("%d %d", &n, &k); > vector<int> data(k); > for (int i = 0; i < k; ++i) { > scanf("%d", &data[i]); > } > int low = 1; > int high = n + 1; > while (high - low > 1) { > int mid = (high + low) / 2; > int cols = 0; > for (int i = 0; i < k; ++i) { > cols += data[i] / mid; > } > if (cols >= n) { > low = mid; > } else { > high = mid; > } > } > printf("%d\n", low); > for (int i = 0; i < k; ++i) { > for (int j = data[i] / low; j > 0; --j) { > // 注意这里一定要加上! > if (n-- > 0) { > printf("%d\n", i + 1); > } else { > break; > } > } > } > } 谢谢!我用的也是二分,一直纠结怎么避免死循环。看到你的代码,豁然开朗^-^ Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator