You are given N blocks of height 1…N. In how many ways can you arrange these blocks in a row such that when viewed from left you see only L blocks (rest are hidden by taller blocks) and when seen from right you see only R blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1}.
honestly, i am not familiar with DP, to me, it might be easier if we approach it by 1) finding the largest two values in the array of N, denoted as N_maxA and N _maxB and (N_maxA>N_maxB). 2) place N_maxA and N_maxB at the R^th and (L-1)^th position. Note that you can place it in the opposite way, by placing N_maxB and N_maxA at the L^th and (R-1)^th position 3) assuming we go with the first arrangement, then we will have a sequence ( in increasing order) placed at the left of the N_maxA, and a s
因为Harvard不在系统里面?在系统里面的好学校的学生我大都面过,Yale, Princeton ,Columbia, etc. Harvard计算机可能也不太强吧,比Stanford, MIT, Berkeley啥的是不是要差点?
【在 f******2 的大作中提到】 : 其实你说的abc最强根本道理在这个问题。 : social network那个电影里有答案
a*a
13 楼
bless
s*t
14 楼
Longest Increasing Subsequence
blocks }
【在 m*****f 的大作中提到】 : You are given N blocks of height 1…N. In how many ways can you arrange : these blocks in a row such that when viewed from left you see only L blocks : (rest are hidden by taller blocks) and when seen from right you see only R : blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} : while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1}.
c*i
15 楼
costco, cvs 照方便 diy 选照片麻烦 便宜
f*2
16 楼
记得有个场景,当时zackerburg和那兄弟俩进了校长办公室,校长的大意就是说,你们 不是想如何take a job,我们培养你们如何make jobs。 当时我就恍然大悟,哇,这可能就是我这么多年没有遇到过havard的人的原因。 时间太长,具体情节记不清了。
【在 u******p 的大作中提到】 : honestly, i am not familiar with DP, to me, it might be easier if we : approach it by : 1) finding the largest two values in the array of N, denoted as N_maxA and N : _maxB and (N_maxA>N_maxB). : 2) place N_maxA and N_maxB at the R^th and (L-1)^th position. Note that you : can place it in the opposite way, by placing N_maxB and N_maxA at the L^th : and (R-1)^th position : 3) assuming we go with the first arrangement, then we will have a sequence ( : in increasing order) placed at the left of the N_maxA, and a s
不知道有没有解析的公式,写了个递推的: #include int N,L,R; long long num[128][128]; int main() { int i,j,k; scanf("%d%d%d", &N, &L, &R); num[1][1] = 1; for (i=2;i<=N;++i) for (j=i;j>=1;--j) for (k=i-j+1;k>=1;--k) num[j][k] = num[j][k] * (i - 2) + num[j-1][k] + num[j][k-1]; printf("%lld\n", num[L][R]); return 0; }
blocks }
【在 m*****f 的大作中提到】 : You are given N blocks of height 1…N. In how many ways can you arrange : these blocks in a row such that when viewed from left you see only L blocks : (rest are hidden by taller blocks) and when seen from right you see only R : blocks? Example given N=3, L=2, R=1 there is only one arrangement {2, 1, 3} : while for N=3, L=2, R=2 there are two ways {1, 3, 2} and {2, 3, 1}.
g*y
50 楼
nice! But I think you can further improve, if you combine your idea with mudhoof's
【在 b****j 的大作中提到】 : 不知道有没有解析的公式,写了个递推的: : #include : int N,L,R; : long long num[128][128]; : int main() { : int i,j,k; : scanf("%d%d%d", &N, &L, &R); : num[1][1] = 1; : for (i=2;i<=N;++i) : for (j=i;j>=1;--j)
k*e
51 楼
why dont you just say the answer? just not that to get f(n, l, r) define a_ln means given n numbers that look from left we can see just l of them b_rn means given n numbers that look from right and we can see just r then a_ln=b_ln to get a_ln, decompose on the smallest element a_ln=a_l-1 n-1 + (n-1)a_l n-1 for n numbers with L R as given, the total time would be max(L, R)n to get a _ij then f(n,L,R) can be represented as a_ln, b_rn as have done by some ppl before thus the total time is just max(
【在 g*******y 的大作中提到】 : nice! : But I think you can further improve, if you combine your idea with mudhoof's
b*j
52 楼
I think his idea is wrong.
's
【在 g*******y 的大作中提到】 : nice! : But I think you can further improve, if you combine your idea with mudhoof's
I explained before: in your notation, f(a, b) means the number of permutations of a numbers with the longest increasing subsequence length being b. 4 1 2 3 is one of the sequence for f(4, 3), but when looking from left, only 4 can be seen.
【在 m*****f 的大作中提到】 : ...if you say someone is wrong, at least also explain why...
【在 b****j 的大作中提到】 : I explained before: : in your notation, f(a, b) means the number of permutations of a numbers with : the longest increasing subsequence length being b. : 4 1 2 3 is one of the sequence for f(4, 3), but when looking from left, only : 4 can be seen.
No, you haven't fully understood his codes. He used a common trick to reuse the variable/save space.
【在 c***y 的大作中提到】 : I guess you want to say: : num[j][k] = num[j-1][k] * (i - 2) + num[j-1][k] + num[j-1][k-1]; : ~~ ~~ : not : num[j][k] = num[j][k] * (i - 2) + num[j-1][k] + num[j][k-1];
c*y
65 楼
if you think this way from another perspective: f(a,b) can be constructed by either adding the lowest block (a-th) in either the first or the other places based on existing (a-1) blocks, then f(a,b) = f(a-1,b)*(a-1) + f(a-1, b-1) this is the interative way for your method
【在 m*****f 的大作中提到】 : 假设f(a,b)代表a个数的数列中有长度为b的最长递增子序列的排法 : 设最大的那个数最终位置为x, 显然b<=x<=a, 得 : f(a,b) = f(a-1, b-1) (x=a的情况) + f(a-2, b-1)*(a-1) (x=a-1的情况, a位置可放 : 任一数) + f(a-3, b-1)*(a-1)*(a-2) (x=a-2, a, a-1位置可放任意数) .... : until...f(b-1, b-1) * (a-1) * (a-2) ...(a-b) : 声明一个axb的数组, 显然 : f(*,1) = 1 : f(a, b) = 1 if a = b : f(a, b) = 0 if a < b : 然后填满这个数组, 我刚才懒得写完, 现在就写完罢
b*e
66 楼
Some small improvement: Instead of computing number of solutions, compute probability. Then you don't have to compute the crap of C(n-1, x).
n-L,
【在 m*****f 的大作中提到】 : 验证下我的想法。如下: : 左右两边必然看到最高block结束, 以最高点划分, 可以假设左边x个, 右边y个,可以假 : 定x= L-1, y >= R-1, : 假设f(a,b)代表a个数的数列中有长度为b的最长递增子序列的排法, 所求解为 : sum (C(n-1,x)*f(x, L-1) * f(y, R-1)), 已知 L-1 <= x <= n-R, R-1 <= y <= n-L, : y = n-x-1 : f(a,b) 可以用DP求出... : 觉得有点繁琐, 有人有别的想法么
r*o
67 楼
C(n-1,x)这里是什么意思阿,是组合数吗?
【在 b***e 的大作中提到】 : Some small improvement: : Instead of computing number of solutions, compute probability. Then you : don't have to compute the crap of C(n-1, x). : : n-L,
当L+R==N+1时f(N,L,R)=C(N-1,L) 如果L+R 和 N 接近时可以节省一点。 int HoofNumber(int length, int numLeft, int numRight) { int result=0; if (numLeft+numRight > length+1 || numLeft == 0 || numRight ==0) { return 0; } else if (numLeft+numRight == length+1) { result = Factorial(length-1) / (Factorial(numLeft-1) * Factorial( length-numLeft)); return result; } else { result = (HoofNumber(length-1, numLeft-1, numRight) + HoofNumber(length-1
d*n
74 楼
My solutio for f(a, b): f(a, b) = sum{(C(a-1,0)*f(a-1, b-1)*P(0,0), C(a-1, 1)*f(a-1-1, b-1)*P(1, 1), C(a-1, 2)*f(a-1-2, b-1)*P(2,2), ..., C(a-1, i)*f(a-1-i, b-1)*P(i,i)}, i = a -b; P(i, i) is the permutation.
不知道有没有解析的公式,写了个递推的: #include int N,L,R; long long num[128][128]; int main() { int i,j,k; scanf("%d%d%d", &N, &L, &R); num[1][1] = 1; for (i=2;i<=N;++i) for (j=i;j>=1;--j) for (k=i-j+1;k>=1;--k) num[j][k] = num[j][k] * (i - 2) + num[j-1][k] + num[j][k-1]; printf("%lld\n", num[L][R]); return 0; } blocks }
【在 b****j 的大作中提到】 : 不知道有没有解析的公式,写了个递推的: : #include : int N,L,R; : long long num[128][128]; : int main() { : int i,j,k; : scanf("%d%d%d", &N, &L, &R); : num[1][1] = 1; : for (i=2;i<=N;++i) : for (j=i;j>=1;--j)
【在 c***y 的大作中提到】 : if you think this way from another perspective: : f(a,b) can be constructed by either adding the lowest block (a-th) in either : the first or the other places based on existing (a-1) blocks, then : f(a,b) = f(a-1,b)*(a-1) + f(a-1, b-1) : this is the interative way for your method