Arthur Yoo's Tech Thoughts >> 差分约束系统(POJ 3169 Layout)



有N头牛,依次编号为1…N,并且按照编号大小的先后顺序排成一列。有些牛之间比较亲密,它们之间的距离不能大于某个值;有些牛之间相互厌恶,它们之间的距离必须大于某个值。求出满足上述条件中,第1头牛和第N头之间的最大距离;若无法实现,输出-1;若第1头牛和第N头之间可以相隔任意远的距离,则输出-2。

首先,设d[i]第i头牛距离座标起点的距离。根据题意,那些亲密的牛之间的距离必须小于某些值w,则对应不等式方程d[i] + w >= d[j];相互厌恶的牛之间的距离必须大于某些值w,对应的不等式方程d[i] + w <= d[j]。另外,题目中还有提到“ The cows are standing in the same order as they are numbered, and since they can be rather pushy, it is possible that two or more cows can line up at exactly the same location (that is, if we think of each cow as being located at some coordinate on a number line, then it is possible for two or more cows to share the same coordinate). ”也就是说,编号大的牛的位置一定要大于等于编号小的牛,但允许编号相邻的牛“挤”在一个点上。所以,据此可以对从前往后的所有牛列出不等式方程d[i+1] – d[i] >= 0(1<= i<N)。到这里,根据POJ 3169的Sample Input,可以列出下列差分约束系统的不等式方程组(*):

1
2
3
4
5
6
7
//查分约束系统_初始形态
d[1] + 10 >= d[3]
d[2] + 20 >= d[4]
d[2] + 3 <= d[3]
d[2] - d[1] >= 0
d[3] - d[2] >= 0
d[4] - d[3] >= 0

像上面的这样一组一次不等式方程组,每一个不等式方程斗都含有两个正负号相反的变量(d[i]和d[j]),以及一个常数,这样的一次不等式方程组就叫做查分约束系统。假如上面的不等式组进行适当的变换,就能够将所有的变量移动到不等式符号的右侧,而且所有的不等式号也能统一成一种(大于等于号或小于等于号)。用更为简洁的表述来描述上述的查分约束系统就是形如Ax=B,如:

题目最后要求的是满足差分约束系统的所有d[i](1<=i<=N)中,d[N]-d[1]的最大值。由于没有初始条件,如果得到了满足查分约束系统的某一组可行解(d[1], d[2], ..., d[N])的话,那么(d[1]+x, d[2]+x, ..., d[N]+x)也是一组可行解(x是任意实数)。也就是说,在各组可行解中,解向量中的某两个元素之间的差值是定值,即(d[i]+x) - (d[j]+x) 恒等于 d[i] - d[j]。因此,无论得到的可行解的具体数值是多少,题目要求的d[N]-d[1]的最大值肯定的确定的。因此,不妨将d[1]假设为0,那么求d[N]-d[1]的最大值就等价于求d[N]的最大值。


Read full article from Arthur Yoo's Tech Thoughts » 差分约束系统(POJ 3169 Layout)


No comments:

Post a Comment

Labels

Algorithm (219) Lucene (130) LeetCode (97) Database (36) Data Structure (33) text mining (28) Solr (27) java (27) Mathematical Algorithm (26) Difficult Algorithm (25) Logic Thinking (23) Puzzles (23) Bit Algorithms (22) Math (21) List (20) Dynamic Programming (19) Linux (19) Tree (18) Machine Learning (15) EPI (11) Queue (11) Smart Algorithm (11) Operating System (9) Java Basic (8) Recursive Algorithm (8) Stack (8) Eclipse (7) Scala (7) Tika (7) J2EE (6) Monitoring (6) Trie (6) Concurrency (5) Geometry Algorithm (5) Greedy Algorithm (5) Mahout (5) MySQL (5) xpost (5) C (4) Interview (4) Vi (4) regular expression (4) to-do (4) C++ (3) Chrome (3) Divide and Conquer (3) Graph Algorithm (3) Permutation (3) Powershell (3) Random (3) Segment Tree (3) UIMA (3) Union-Find (3) Video (3) Virtualization (3) Windows (3) XML (3) Advanced Data Structure (2) Android (2) Bash (2) Classic Algorithm (2) Debugging (2) Design Pattern (2) Google (2) Hadoop (2) Java Collections (2) Markov Chains (2) Probabilities (2) Shell (2) Site (2) Web Development (2) Workplace (2) angularjs (2) .Net (1) Amazon Interview (1) Android Studio (1) Array (1) Boilerpipe (1) Book Notes (1) ChromeOS (1) Chromebook (1) Codility (1) Desgin (1) Design (1) Divide and Conqure (1) GAE (1) Google Interview (1) Great Stuff (1) Hash (1) High Tech Companies (1) Improving (1) LifeTips (1) Maven (1) Network (1) Performance (1) Programming (1) Resources (1) Sampling (1) Sed (1) Smart Thinking (1) Sort (1) Spark (1) Stanford NLP (1) System Design (1) Trove (1) VIP (1) tools (1)

Popular Posts