题意:给定一堆stick(including endpoints and length),从中抽取一个subset组成凸多边形,要求subset中的endpoints不能重合。问有多少种组合。
这一题开始除了暴力没啥想法。。瞎写了个搜索,枚举所有组合(是否选择stick i)在除去不符合条件的,后来发现stick的个数是O(N^2)不是N,就呵呵了。
其实正解也是搜索,不过要在每个branch开始前略过不满足条件(endpoints重合)的branch。避免无用的枚举。
假设有M个stick,endpoints不重合的组合最多的case是所有stick一字排开,这样重合的stick最少,e.g.,(1,2),(2,3),(3,4),(4,5),...这种case下,至多可同时选择M/2个stick满足endpoints不重合的条件,所以subset个数f(M)=(M-1)*(M-3)*..*1。(or M*(M-2)*..*2?)
考虑所有的subset with different size i,组合数的Upper bound=sum C(M,i)f(i)。不过sample solution说i is even就不知道为啥了。。
判断一堆线段能否构成convex polygon,画个图看一看出来,for all i, length of stick i >sum of length of stick j, j!=i. 其实可以用一个constraint替代: max length of stick > sum of legth of other sticks.
实现code的时候纠结dfs何时退出。。如果是枚举所有组合,枚举到第M个stick退出即可。但是在正解中,搜索时只在endpoint满足条件的情况下才开启新的branch搜索。。本来以为加一个分支跳过当前stick即可,但是这样会出现重复的subset。
看到了别人的blog才发现,只要在dfs开始判断当前选择的subset是否符合条件即可。因为dfs分支中只会加入endpoint valid的stick,每个subset只会出现一次。e.g.,不会出现 select stick 0, select stick 1, not select stick 2, not select stick 3的情况。
另外,快速判断stick endpoints是否满足条件可以传入mask数组。通过或运算即可:mask|x|y,x,y是新选择的stick的两个端点。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include