avatar
一道题:Vertical Sticks# JobHunting - 待字闺中
k*t
1
interviewstreet上的。brute-force很straightforward。
有没有妙招降复杂度?
===================================================
Vertical Sticks
Given array of integers Y=y1,...,yn, we have n line segments such that
endpoints of segment i are (i, 0) and (i, yi). Imagine that from the top of
each segment a horizontal ray is shot to the left, and this ray stops when
it touches another segment or it hits the y-axis. We construct an array of n
integers, v1, ..., vn, where vi is equal to length of ray shot from the top
of segment i. We define V(y1, ..., yn) = v1 + ... + vn.
For example, if we have Y=[3,2,5,3,3,4,1,2], then v1, ..., v8 = [1,1,3,1,1,3
,1,2], as shown in the picture below:
For each permutation p of [1,...,n], we can calculate V(yp1, ..., ypn). If
we choose a uniformly random permutation p of [1,...,n], what is the
expected value of V(yp1, ..., ypn)?
Input Format
First line of input contains a single integer T (1 <= T <= 100). T test
cases follow.
First line of each test-case is a single integer N (1 <= N <= 50). Next line
contains positive integer numbers y1, ..., yN separated by a single space (
0 < yi <= 1000).
Output Format
For each test-case output expected value of V(yp1, ..., ypn), rounded to two
digits after the decimal point.
Sample Input
6
3
1 2 3
3
3 3 3
3
2 2 3
4
10 2 4 4
5
10 10 10 5 10
6
1 2 3 4 5 6
Sample Output
4.33
3.00
4.00
6.00
5.80
11.15
Explanation
Case 1: We have V(1,2,3) = 1+2+3 = 6, V(1,3,2) = 1+2+1 = 4, V(2,1,3) = 1
+1+3 = 5, V(2,3,1) = 1+2+1 = 4, V(3,1,2) = 1+1+2 = 4, V(3,2,1) = 1+1+1 = 3.
Average of these values is 4.33.
Case 2: No matter what the permutation is, V(yp1, yp2, yp3) = 1+1+1 = 3,
so the answer is 3.00.
Case 3: V(y1 ,y2 ,y3)=V(y2 ,y1 ,y3) = 5, V(y1, y3, y2)=V(y2, y3, y1) = 4
, V(y3, y1, y2)=V(y3, y2, y1) = 3, and average of these values is 4.00.
avatar
b*e
2
分析:先按Y从大到小排序,V=1+2+..+N. 用STL next_permutation的算法,观察找下
一个排列的规律,递推求下一个V值。 e.g.,(8,7,2,3,4,5,6)的下一个排列是6,7 先
swap,然后2~7反转,即为(8,6,7,5,4,3,2)。
注意到6、7 swap不改变V值,反转后,V损失的值为1+2+..+K, 增加的值为1+1+..+1 =
K, 于是有
V(8,6,7,5,4,3,2) = V(8,7,2,3,4,5,6) - (1+2+...+K)+K = V(8,7,2,3,4,5,6) + k
(k-1)/2
这样可以大大加快速度。
更进一步的,如果{yi} 无重复,最后所求V与yi的具体值无关,只与n有关。即V(y1, .
.., yn) = V(1,2,...,n)
avatar
v*a
3
Just accepted. Calculate expected value for every position and sum up all.
Pos_1: expected value = 1.00
Pos_2: expected value = Sum of {if it is y_i, then how many chance of
length
1 * 1, how many chance of length 2 * 2, how ...} / N
Ans = Sum {Pos_i | for all position i}
avatar
f*t
4
试着写了一下,写不出来……
能发下代码么?

【在 v***a 的大作中提到】
: Just accepted. Calculate expected value for every position and sum up all.
: Pos_1: expected value = 1.00
: Pos_2: expected value = Sum of {if it is y_i, then how many chance of
: length
: 1 * 1, how many chance of length 2 * 2, how ...} / N
: Ans = Sum {Pos_i | for all position i}

avatar
s*a
5
E(n) = (n+1)/2 + (sum from{i=1} to {i=n-1} E(i) )*2/n
E(1) = 1
E(2) = 1.5 + 1 = 2.5
This works only if all the numbers is distinctive
avatar
d*l
6
思路非常棒!本来没什么想法的,看了这个思路1A了。

【在 v***a 的大作中提到】
: Just accepted. Calculate expected value for every position and sum up all.
: Pos_1: expected value = 1.00
: Pos_2: expected value = Sum of {if it is y_i, then how many chance of
: length
: 1 * 1, how many chance of length 2 * 2, how ...} / N
: Ans = Sum {Pos_i | for all position i}

avatar
f*t
7
Pos_2: expected value = Sum of {if it is y_i, then how many chance of length
1 * 1, how many chance of length 2 * 2, how ...} / N
chance怎么算?

【在 d*******l 的大作中提到】
: 思路非常棒!本来没什么想法的,看了这个思路1A了。
avatar
p*2
8

length
我看了一会儿也没太明白。而且这个思路也太怪了。这需要什么方面的知识才能想得到
呢?

【在 f*******t 的大作中提到】
: Pos_2: expected value = Sum of {if it is y_i, then how many chance of length
: 1 * 1, how many chance of length 2 * 2, how ...} / N
: chance怎么算?

avatar
f*t
9
我按这个思路做,主要是chance那边想不到合适的解法。用排列组合算很直观,但因为
数据量大(N可以到50),阶乘是天文数字,所以我的代码只能通过sample testcases。

【在 p*****2 的大作中提到】
:
: length
: 我看了一会儿也没太明白。而且这个思路也太怪了。这需要什么方面的知识才能想得到
: 呢?

avatar
d*l
10
阶乘直接用double就行。double其实能表示的范围挺大的,我们往往会低估。不过精度
就不能保证了。好在这题精度要求不高,反正我用double是可以的。
至于chance,把所有可能的长度枚举一遍,事先算好每个长度对应的比它小的元素有多
少个,然后就是个排列组合的问题。

length

【在 f*******t 的大作中提到】
: Pos_2: expected value = Sum of {if it is y_i, then how many chance of length
: 1 * 1, how many chance of length 2 * 2, how ...} / N
: chance怎么算?

avatar
v*a
11

// D[i], input
double solve() {
double ans = 1.0;
for (int pos = 2; pos <= N; pos++) {
double t1 = 0.0;
for (int i = 0; i < N; i++) { // choose i in position pos
int x = C[ i ][0]; // < D[i]
int y = C[ i ][1]; // >= D[i]
double pre = 1.0;
int k = 1;
for (; k <= (pos - 1); k++) {
double t2 = pre * ( (double)y / (double)(x + y) );
t1 += (t2 * k);
pre *= ( (double)x / (double) (x + y) );
if (x-- == 0)
break;
}
if (k == pos)
t1 += (pre * pos);
}
ans += (t1 / N);
}
return ans;
}

【在 v***a 的大作中提到】
: Just accepted. Calculate expected value for every position and sum up all.
: Pos_1: expected value = 1.00
: Pos_2: expected value = Sum of {if it is y_i, then how many chance of
: length
: 1 * 1, how many chance of length 2 * 2, how ...} / N
: Ans = Sum {Pos_i | for all position i}

avatar
d*l
12
还是这样比较直接,都不用算排列跟阶乘什么的,学习了

【在 v***a 的大作中提到】
:
: // D[i], input
: double solve() {
: double ans = 1.0;
: for (int pos = 2; pos <= N; pos++) {
: double t1 = 0.0;
: for (int i = 0; i < N; i++) { // choose i in position pos
: int x = C[ i ][0]; // < D[i]
: int y = C[ i ][1]; // >= D[i]
: double pre = 1.0;

avatar
w*c
13
还是没看懂,C[ i ][0],C[ i ][1] 是怎么来的?
avatar
w*c
14
自己太粗心,没看到注释,懂了,很巧妙的想法,谢谢了。

【在 v***a 的大作中提到】
:
: // D[i], input
: double solve() {
: double ans = 1.0;
: for (int pos = 2; pos <= N; pos++) {
: double t1 = 0.0;
: for (int i = 0; i < N; i++) { // choose i in position pos
: int x = C[ i ][0]; // < D[i]
: int y = C[ i ][1]; // >= D[i]
: double pre = 1.0;

avatar
d*n
15
銆鍦kirit (Jack) 鐨勫ぇ浣滀腑鎻愬埌: 銆br />
-force寰坰traightforward銆br />
of
n
top
avatar
z*c
16
Nice thought! viisa
但是这一行
pre *= ( (double)x / (double) (x + y) );
是不是应该改成
pre *= ( (double)x / (double) (x + y -1) );
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。