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

悲剧啊,添加了二分,居然te

Posted by wskiwwwx at 2010-01-28 16:31:30 on Problem 2388
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	private static int[] cows;
	private static int len = 0;// 实际长度

	private static int binarySearch(int number) {
		int p = 0;
		int low = 0;
		int hight = len - 1;
		while (low <= hight) {
			int mid = (hight - low) >>> 1;
			if (mid < len - 1 && cows[mid] <= number && cows[mid + 1] >= number) {
				p = mid + 1;
				break;
			} else if (mid > 0 && cows[mid] >= number
					&& cows[mid - 1] <= number) {
				p = mid;
				break;
			} else if (cows[mid] < number)
				low = mid + 1;
			else if (cows[mid] > number)
				hight = mid - 1;
		}
		return p;
	}

	public static void add(int number) {
		if (len == 0)
			cows[len++] = number;
		else {
			int temp = number;
			if (temp > cows[len - 1])
				cows[len++] = temp;
			else {
				// System.out.println(binarySearch(number));
				int t = binarySearch(number);
				for (int j = len; j > t; --j) {
					cows[j] = cows[j - 1];
				}
				cows[t] = temp;
				// for (int i = 0; i < len; ++i) {
				// if (cows[i] > temp) {
				// System.out.println("i = " + i);
				// for (int j = len; j > i; --j) {
				// cows[j] = cows[j - 1];
				// }
				// cows[i] = temp;
				// break;
				// }
				// }
				len++;
			}
		}
	}

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int sum = sc.nextInt();
		cows = new int[sum];
		Arrays.fill(cows, 0);
		len = 0;

		for (int i = 0; i < sum; ++i) {
			int num = sc.nextInt();
			add(num);
		}

		System.out.println(cows[(sum / 2)]);
	}

}


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