24:Minimum Sum

Description


You are given N positive integers, denoted as x1, x2 ... xN. Then give you some intervals [l, r]. For each interval, you need 
to find a number x to make |x-xl| + |x-x2| + … + |x-xr| as small as possible!


Input


The first line is an integer T (T <= 6), indicating the number of test cases. For each test case, an integer N 
(1 <= N <= 100,000) comes first. Then comes N positive integers x (1 <= x <= 1,000, 000,000) in the next line.
Finally, comes an integer Q (1 <= Q <= 5,000), indicting there are Q queries. Each query consists of two
integers l, r (1 <= l <= r <= N), meaning the interval you should deal with.

Output


For the k-th test case, first output “Case #k:” in a separate line. Then output Q lines, each line 
is x, which make as small as possible. If there are several integers x satisfying it, please output
the minimum x. Output a blank line after every test case.


SampleInput


2
5
3 6 2 2 4
2
2 5
1 3
2
7 7
2
1 2
1 1

SampleOutput


Case #1:
2
3

Case #2:
7
7

Hint


Huge input,scanf is recommended.

解题思路:找到依次相减最小的数,其实就是找排序后的数组中,中间的数,如果数列长度是奇数,则就是中间的数,如果数列长度是偶数,则是中间两个数中小的那个数。(在这里排序算法使用快速排序~)

package OJ;

import java.util.*;

public class P24_temp {
	
	/** 
     * 快速排序,伪代码: 
     * QUICKSORT(A, p, r) 
     * 1 if p < r 
     * 2    then q ← PARTITION(A, p, r) 
     * 3        QUICKSORT(A, p, q - 1) 
     * 4        QUICKSORT(A, q + 1, r) 
     *  
     * PARTITION(A, p, r) 
     * 1 x ← A[r] 
     * 2 i ← p - 1 
     * 3 for j ← p to r - 1 
     * 4    do if A[j] ≤ x 
     * 5        then i ← i + 1 
     * 6            exchange A[i] ↔ A[j] 
     * 7 exchange A[i + 1] ↔ A[r] 
     * 8 return i + 1 
     * 复杂度,最坏情况下:Θ(n^2) 
     * 一般平衡情况:Θ(nlgn) 
     * @param array 待排数组 
     * @param from 起始位置 
     * @param to 终止位置 
     */ 
	
	public static void quickSort(int[] array, int from, int to) { 
        if (from < to) { 
            int temp = array[to]; 
            int i = from - 1; 
            for (int j = from; j < to; j++) { 
                if (array[j] <= temp) { 
                    i++; 
                    int tempValue = array[j]; 
                    array[j] = array[i]; 
                    array[i] = tempValue; 
                } 
            } 
            array[to] = array[i+1]; 
            array[i+1] = temp; 
            quickSort(array, from, i); 
            quickSort(array, i + 1, to); 
        } 
    } 

	public static void main(String[] args) {
		int caseNum = 0;
		Scanner in = new Scanner (System.in);
		caseNum = in.nextInt();
		ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
		for(int c=0; c<caseNum; c++){
			int n = in.nextInt();
			int[] queue =  new int [n+1];
			for(int i=1; i<=n; i++){
				queue[i] = in.nextInt();
			}
//			int[] copy = queue.clone();
			int groupNum = in.nextInt();
			int[] interval = new int[groupNum*2+1];
			for(int j=1; j<=groupNum*2; j++){
				interval[j] = in.nextInt();
			}
			ArrayList<Integer> result = new ArrayList<Integer>();
			for(int k=1; k<=groupNum; k++){
				int[] copy = queue.clone();
				quickSort(copy, interval[k*2-1], interval[2*k]);
				int mid = copy[(interval[2*k-1] + interval[2*k])/2];
				result.add(mid);
			}
			results.add(result);
		}
		for(int t=0; t<results.size(); t++){
			int order = t+1;
			System.out.println("Case#" + order +":");
			for(int p=0; p<results.get(t).size(); p++){
				System.out.println(results.get(t).get(p));
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值