2. Q1
• Given a 2-dimensional n × n matrix of 0/1
integers, design a linear time algorithm to find
a subrectangle with the largest size such that
all the elements in the sub-rectangle are equal
to 1.
2014/1/3
Q1 Q8
2
7. Q1 – efficient algorithm(1)
• l[i][j] express that the size of (x, y) expand
maximum left columns
• r[i][j] express that the size of (x, y) expand
maximum right columns
• h[i][j] express that the height of (x, y) expand
maximum rows (backword)
• wl[i][j] the maximum left width when it has
max height
• wr[i][j] the maximum right width when it has
max height
2014/1/3
Q1 Q8
7
8. Q1 – efficient algorithm(1)
l [ i ][ j ]
r [ i ][ j ]
h [ i ][ j ]
wl [ i ][ j ]
wr [ i ][ j ]
l [ i ][ j
1, if
A[i][j] is empty.
1]
0 , otherwise
r [ i ][ j
1, if
A[i][j] is empty.
1]
0 , otherwise
h [i
1][ j ]
1, if
A[i][j] is empty.
0 , otherwise
min( wl [ i 1][ j ] 1, l [ i ][ j ]), if
A[i][j] is empty.
0 , otherwise
min( wr [ i
area [ i ][ j ]
1][ j ]
1, r [ i ][ j ]), if
A[i][j] is empty.
0 , otherwise
( h [ i ][ j ])( wl [ i ][ j ]
wr [ i ][ j ]
1) if
A[i][j] is empty.
0 , otherwise
Maximum empty area = max(area[i][j]), (0 ≦ i, j < n )
2014/1/3
Q1 Q8
8
9. Q1 – efficient algorithm(1)
area [ i ][ j ]
( h [ i ][ j ])( wl [ i ][ j ]
wr [ i ][ j ]
1) if
A[i][j] is empty.
0 , otherwise
• Final result = max(area[i][j]), (0 < i, j ≦ n)
2014/1/3
Q1 Q8
9
10. Q1 – efficient algorithm(1)
int wl[205] = {}, wr[205] = {};
int r[205] = {}, l[205] = {}, h[205] = {};
int sum;
int ret = 0;
for(i = 0; i < n; i++) {
for(j = 0, sum = 0; j < m; j++) {
if(g[i][j] == 0)
sum = 0;
sum = wl[j] = sum + g[i][j];
}
for(j = m-1, sum = 0; j >= 0; j--) {
if(g[i][j] == 0)
sum = 0;
sum = wr[j] = sum + g[i][j];
}
for(j = 0; j < m; j++) {
if(g[i][j] == 0)
h[j] = 0;
else
h[j]++;
}
for(j = 0; j < m; j++) {
if(l[j] == 0)
l[j] = wl[j];
else
l[j] = min(l[j], wl[j]);
if(r[j] == 0)
r[j] = wr[j];
else
r[j] = min(r[j], wr[j]);
}
for(j = 0; j < m; j++)
ret = max(ret, (l[j]+r[j]-1)*h[j]);
2014/1/3
Q1 Q8
}
10
12. Q1 – efficient algorithm(2)
• New Problem : Largest Rectangle in a Histogram
• Histogram : (@*#&U(@*U(!*
•
Picture from http://poj.org/problem?id=2559
2014/1/3
Q1 Q8
12
14. Q1 – efficient algorithm(2)
int solve(int n, int h[]) {
int ret = 0;
int i, height;
stack< pair<int, int> > stk;// <height, position>
pair<int, int> e;
stk.push(make_pair(-1, 0));
h[n] = 0;// visual height.
for(i = 0; i <= n; i++) {
height = h[i];
e = make_pair(height, i);
while(height < stk.top().first) {
e = stk.top(), stk.pop();
ret = max(ret, (i - e.second)*e.first);
}
if(height > stk.top().first)
stk.push(make_pair(height, e.second));
}
return ret;
}
2014/1/3
Q1 Q8
14
15. Q1 – more …
• Online Judge
POJ 2559 - Largest Rectangle in a Histogram
ZJ b123 最大矩形 (Area)
• More extended problem …
maximum rectangle with all same element.
O(NM)
maximum rectangle which
|maximum element – minimum element| < L
O(NML)
2014/1/3
Q1 Q8
15
16. Q2
• Improve the space utilization in the algorithm
of solving the 0/1 knapsack (sum-of-subsets)
problem discussed in the class.
• 優化 0/1 背包問題的記憶體空間
2014/1/3
Q1 Q8
16
19. Q3 – a
• Solve each of the following variations of the
0/1 knapsack problem (sum-of-subsets):
Pack items of given sizes in a given-sized
knapsack fully, but there is an unlimited
supply (無限量供應) of each item.
• 固定大小,無限量供應
2014/1/3
Q1 Q8
19
21. Q3 – b
• The assumptions are the same as in v1 (n
items, unlimited supply, fixed-sized knapsack),
but now each item has an associated value.
Design an algorithm to find how to pack the
knapsack fully, such that the items in it have
the maximal value among all possible ways to
pack the knapsack.
• 計算背包容量的方法數
2014/1/3
Q1 Q8
21
23. Q3 – c
• The assumptions are the same as in v2 (n
items with sizes and values, unlimited supply,
fixed-sized knapsack, and the goal of
maximizing the value), but now we are not
restricted to filling the knapsack exactly to
capacity. We are interested only in maximizing
the total value, subject to the constraint that
there is enough room for the chosen items in
the knapsack.
2014/1/3
Q1 Q8
23
25. Q3 – more
• limited supply, fixed-sized
O(NK)
POJ 1742 - Coins
UVa 711 - Dividing up
• group item choose one with weight and value
O(NKG)
ex. ZJ d682 TOI2010 第三題:職棒簽約問題
2014/1/3
Q1 Q8
25
26. Q3 – tricky
• 分給 n < 6 個人等值
UVa 1163 - The Right Tip
with another core algorithm.
2014/1/3
Q1 Q8
26
27. Q4
• Design an algoritm to solve Problem 10482
“The Candayman Can” in the web site:
• http://acm.uva.es/problemset/
• 將物品分成三堆,最大堆與最小堆差最小
為何 ?
2014/1/3
Q1 Q8
27
29. Q5
• Design an algoritm to solve Problem 10261
“Ferry Loading” in the web site:
• http://acm.uva.es/problemset/
• 依序放入 n 台車於渡口左側或右側,如果
不能放時,則後面的所有車都不能放。
渡口長 L,每台車長度 x。
2014/1/3
Q1 Q8
29
30. Q5
• 維護其中一側即可,與 Q4 相同。
• dp[i][j] : 表示前 i 台車,左側長 j。
sum = sigma(x[i])
• dp[i][j] = dp[i-1][j-x[i]] |
(sum-j <= L && dp[i-1][j])
2014/1/3
Q1 Q8
30
31. Q6
• Solve the even partition problem: Given a list
of n positive integers, partition the list into
two sublists, each of size floor(n/2) or ceil(n/2),
such that the difference between the sums of
the integers in the two sublists is minimized.
• 0/1 背包問題
2014/1/3
Q1 Q8
31
32. Q7
• Given a set of n sticks of various lengths,
design an algorithm to determine if it is
possible to join them end-to-end to form a
square.
• Uva 10364 - Square
2014/1/3
Q1 Q8
32
34. Q8
• Suppose you have one machine and a set of n jobs a1, a2, … ,
an to process on that machine. Each job aj has a processing
time tj, a profit pj, and a deadline dj. The machine can process
only one job at a time, and job aj must run uninterruptedly for
tj consecutive time units. If job aj is completed by its deadline
dj, you receive a profit pj, but if it is completed after its
deadline, you receive a profit of 0. Give an algorithm to find
the schedule that obtains the maximum amount of profit. For
each of the following special case, is it possible to find a more
efficient algorithm (comparing to the general case.) ?
• a) Each job has the same processing time.
• b) Each job has the same profit.
• c) Each job has the same deadline.
2014/1/3
Q1 Q8
34
35. Q8
• a) P 工作時間相同
最大利益一定先做,Greedy + disjoint set O(nlogn)
從最大利益開始挑,盡可能將它靠近到 deadline。
對於 processing time > 1,則將 deadline in
[processing time, processing time*2] 之間的設定為
deadline = processing time
• b) P 工作利益相同
烏龜塔,前一份作業討論過。
• c) NPC 截止日期相同
最大價值背包問題。
• 當三者都不同時,根據截止日期排序
dp[i][j] 表示討論前 i 個工作,截止日期為 j 的最大獲益。
2014/1/3
Q1 Q8
35
36. Q9
• Given a weighted directed acyclic graph (DAG;
a directed graph is acyclic if it contains no
directed cycles), and a pair of vertices u and v,
design an algorithm to find a longest and a
shortest path from u to v. Here, the length of a
path is defined as the sum of weights of edges
in the path.
2014/1/3
Q1 Q8
36
38. Q10
• Given a weighted directed graph G=(V, E),
design an algorithm to detect if G contains a
negative cycle. Moreover, if G contains no
negative cycles, design an algorithm to find a
cycle in G of minimum weight.
2014/1/3
Q1 Q8
38
39. Q10
• Floyd - Warshell algorithm
• Let g[i][i] = infinity large
Floyd - Warshell algorithm O(V3)
find minimum weight cycle
• Detect negative cycle by Bellman-ford
algorithm or SPFA(Shortest Path Faster
Algorithm with SLF and LLL strategy)
2014/1/3
Q1 Q8
39
40. Q10 - more
• Minimum Mean Cycle Problem
2014/1/3
Q1 Q8
40