Codeforces Round #277.5 (Div. 2) E. Hiking(二分 DP)

题目:LINK

一个人从0处出发,一共有n个休息的地方,每个地方有距离0的距离x[i] 和picturesqueness b[i],要到达的距离>= x[n],(最后n-th必选),中间的休息点可供选择,使得 sigma(sqrt(x[j] - x[i] - l)) / sigma(b[j])尽可能小.j是当前休息点, i是上一个休息点.
可以二分题目要求的结果,对于每一个二分的值进行验证(可以DP一下)

感觉有点01分数规划的味道

import java.util.*; 
public class Main {	
	public static void main(String argsp[]) {
		Sol x = new Sol(); 
		x.sol(); 
	}
}
class Sol{
	static int N = 1111; 
	int n, m ; 
	int x[] = new int[N]; 
	int b[] = new int[N];
	double dp[] = new double[N];
	int pre[] = new int[1111]; 
	boolean test(double in) {
		dp[0] = 0; 
		for(int i = 1;i <= n; i++) {
			dp[i] = -1e10; 
			for(int j = 0; j < i; j++) {
				double tmp = dp[j] + in * b[i] - Math.sqrt(Math.abs(x[i] - x[j] - m)); 
				if(tmp > dp[i]) {
					dp[i] = tmp;
					pre[i] = j; 
				}
			}
		}
		return dp[n] >= 0; 
	}
	void sol() {
		Scanner cin = new Scanner(System.in);
		n = cin.nextInt(); 
		m = cin.nextInt(); 
		for(int i = 1;i <= n; i++) {
			x[i] = cin.nextInt(); 
			b[i] = cin.nextInt(); 
		}
		double l = 0, r = 1e10; 
		for(int i = 0; i <= 100; i++) {
			double mid = (l+r) / 2; 
			if(test(mid)) r = mid; 
			else l = mid; 
		}
		List L = new ArrayList(); 
		L.clear(); 
		for(int t = n; t != 0; t = pre[t])  L.add(t);
		for(int i = L.size() - 1; i >= 0; i--) {
			System.out.println((int)L.get(i) ); 
		}
	}
}


你可能感兴趣的:(DP,二分)