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 |
这题一定要用right才能ac...二分的输出应该怎么确定啊?此题的二分输出一定是right才是对的(left, mid都wa)。。。以前遇到过一定要left才是ac的情况,请问大大这是为什么呢? #include <iostream> #include <algorithm> using namespace std; const int N=100005; int arr[N]; int n, c; bool jdg(int x) { int num=arr[0]; int i, j; j = 0; for (i=1; i<c; i++) { num += x; bool flag = false; for (j++; j<n; j++) if (num <= arr[j]) { num = arr[j]; flag = true; break; } if (flag == false) return 0; } return 1; } int main() { //freopen ("in.txt", "r", stdin); int i; while (scanf("%d%d", &n, &c) ==2) { for (i=0; i<n; i++) scanf ("%d", &arr[i]); sort (arr, arr+n); int left, right, mid; right = (arr[n-1]-arr[0])/(c-1); left = 0; while (left <= right) { mid = (left + right)/2; if (jdg(mid)) left = mid+1; else right = mid-1; } printf("%d\n", right); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator