SRM 515 DIV 2 1000P

1879 ワード

この問題が大好きですが、できません.コードを見てもこのDPを完全に理解していないので、ため息をついています.
ちなみに問題解を求めます^^;
1000P Problem
あなたは1つの商品があって、2人の客がいて、データのフォーマットはすべて“T 1、C 1、P 1 T 2、C 2、P 2...TN、CN、PN”で、Tは時間を表して、Cは価格を表して、Pは確率を表して、例えば“8、10、50”は50%の可能性があることを表して、この取引先は8時に10元値切って、1時間ごとに最大で1人の客だけが来ます.
最大期待値を求める.
1000P Solution
公式の問題解から
public class NewItemShopTwo {

	int[][] T = new int[2][25];
	int[][] C = new int[2][25];
	double[][] P = new double[2][25];
	
	// the expected profit if we know that he has not come before time t
	double getExpected(int customer, int index) {
		double p = 1;
		for (int i = 0; i < index; i++)
			p -= P[customer][i];
		
		double ret = 0;
		for (int i = index; P[customer][i] > -1; i++)
			ret += C[customer][i] * P[customer][i]/p;
		
		return ret;
	}
	
	public double getMaximum(String[] customers) {
		// Read in the values of the customers to T/C/P
		for (int i = 0; i < customers.length; i++) {
			Arrays.fill(P[i], -1);
			String[] tmp = customers[i].split(" ");
			for (int j = 0; j < tmp.length; j++) {
				String[] t = tmp[j].split(",");
				T[i][j] = Integer.valueOf(t[0]);
				C[i][j] = Integer.valueOf(t[1]);
				P[i][j] = Integer.valueOf(t[2])/100.0;
			}
		}
		
		double ret = 0, p1 = 1, p2 = 1;
		int t1 = 0, t2 = 0;
		
		for (int h = 0; h < 24; h++) {
			if (T[0][t1] == h) {
				ret += (P[0][t1]/p1)*p1*p2*Math.max(C[0][t1], getExpected(1, t2));
				p1 -= P[0][t1];
				t1++;
			} else if (T[1][t2] == h) {
				ret += (P[1][t2]/p2)*p1*p2*Math.max(C[1][t2], getExpected(0, t1));
				p2 -= P[1][t2];
				t2++;
			}
		}
		
		return ret;
	}
}