Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这题一定要用right才能ac...二分的输出应该怎么确定啊?

Posted by iYao at 2010-05-17 21:13:42 on Problem 2456
此题的二分输出一定是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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator