summaryrefslogtreecommitdiff
path: root/sduacm2017/lec1/lec.tex
blob: af1f66d0db9e261231152667fd1bcd4c243f7107 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
\documentclass[aspectratio=169,hyperref={pdfencoding=auto,psdextra}]{beamer}
\usepackage[utf8]{inputenc}
\usepackage{CJKutf8}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{fancyvrb}
\usetheme{Malmoe}
\usecolortheme{default}
\begin{CJK*}{UTF8}{gbsn}
\title{「知道错了没」}
\subtitle{——如何让队友认错}
\author{Chris Xiong}
\date{2017-07-21}
\begin{document}
	\frame{\titlepage}
	\begin{frame}
		\frametitle{「知道错了没」}
		\framesubtitle{Outline}
		\begin{itemize}
			\item 采访
			\item dp: d(ui)p(ai)
			\item dp: Knapsack problem
			\item 如何让队友认错之如何殴打队友
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{采访}
		\framesubtitle{\sout{a.k.a. 教学质量检查}}
		\begin{itemize}
			\item 上次课讲的内容大家都听懂了吗?\pause
			\item 什么?不懂?\pause
			\item 那还记得上次讲的什么吗?\pause
			\item A Water Problem \pause
			\item 已知$$f(x+1) =
				\begin{cases}
				a & x=0
				\\
				b & x=1
				\\
				f(x)+f(x-1)+sin(\frac{\pi x}{2}) & otherwise
				\end{cases}$$
			对于给定的$a,b,n$,求$f(n)$$n\leq 10^{18}$\item 给大家5分钟的思考时间。
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{采访}
		\framesubtitle{怎么样,是不是不会啊?}
		\begin{itemize}
			\item 都怪宇宙智障。\\
			\includegraphics[scale=0.75]{zz.png}\pause
			\item 提示:\pause周期!!\pause
			\item 还不会的话就去找宇宙智障。\\
			\includegraphics[scale=0.75]{zz1.png}
			\item (听说你想要表扬 厚颜无耻)
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{d(ui)p(ai)}
		\begin{itemize}
			\item WTF is duipai?\pause
			\item Automated generation of test data and execution of several programs.\pause
			\item And most importantly, compare their results.\pause
			\item \sout{A nice way to waste time if you are stuck.}
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{A sample script for UNIX-like OS}
		\begin{Verbatim}
#!/bin/bash
i=0
while(true)
do
	./170312cgen > test.in
	./170312ca < test.in > aa.out
	./170312cb < test.in > bb.out
	diff aa.out bb.out
	if [ $? -ne 0 ]
	then
		break
	fi
	echo $i passed
	let i++
done
		\end{Verbatim}
\end{frame}
	\begin{frame}[fragile]
		\frametitle{How to use it?}
		\begin{itemize}
			\item Modify the script to your needs.
			\item Save it as a script, e.g.: "xxx.sh".
			\item Give it the permission to execute.
			Run \verb|chmod +x <your_script_name_here>| in a terminal.
			\item Run it!
			Type \verb|./<your_script_name_here>| in a terminal.
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{What does this script do?}
		\begin{itemize}
			\item Run the input generator.
			\item Feed the generated input to the compared program A and gather results from it.
			\item Do the same thing with program B.
			\item Check the output. If they differ, terminate the script. Otherwise loop. 
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Explanation}
		\begin{itemize}
			\item
				\begin{verbatim}
					while(true)
					do
					done
					break
				\end{verbatim}
			\item
				\begin{verbatim}
					> <
				\end{verbatim}
				redirection
			\item
				\begin{verbatim}
				if
				then
				fi
				$?
				[, -ne
				\end{verbatim}
			\item Verification: \verb|diff| / custom program
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Alternative approaches}
		\begin{itemize}
			\item Write a \verb|C/C++| program instead of a shell script?
			\item \verb|system()| in \verb|stdlib.h| (\verb|cstdlib|)
			\item return value of \verb|system()|
			\item Windows batch file:
				\begin{itemize}
					\item \verb|IF %ERRORLEVEL% EQU 0(GOTO :loop)|
				\end{itemize}
			\item \sout{Powershell}?
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Writing input generators}
		\begin{itemize}
			\item Random?
			\item Constructed special cases?
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{I suck at this}
		\begin{itemize}
			\item Unbounded knapsack problem
			\item Bounded knapsack problem
				\begin{itemize}
					\item 0/1 knapsack problem
				\end{itemize}
			\item NP-complete!
			\item A No-Dynamic-Programming-At-All variant
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{The No-DP-At-All variant}
		Fractional knapsack problem (a.k.a. Continuous knapsack problem)
		\begin{itemize}
			\item A knapsack of capacity $W$.
			\item $N$ items, each having its weight $w_i$ and value per unit weight $v_i$.
			\item Select an amount $x_i$ of each item so that
				the total weight doesn't exceed the capacity (
				$\displaystyle\sum_{i}^{}x_i\leq W$
			) and maximizing the total value
				$\displaystyle\sum_{i}^{}x_i \times v_i$, where $x_i \in \mathbb{R}, x_i \geq 0$.
			\pause
			\item Greedy.
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		\begin{itemize}
			\item Still a knapsack of capacity $W$.
			\item Still $N$ items, each having its weight $w_i$
				and value $v_i$.
			\item For each item, determine whether to put it in
				the knapsack so that the total weight doesn't
				exceed the capacity and the total value is maximum.
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		A brute-force solution:
		\begin{Verbatim}
def dfs(i,remaining_capacity):
	if(i==0): return 0;
	if(remaining_capacity<0):
		return -inf;
	r1=dfs(i-1,remaining_capacity);
	r2=dfs(i-1,remaining_capacity-w[i])+v[i];
	return max(r1,r2);
		\end{Verbatim}
		\begin{itemize}
			\item Call dfs(N,W) for answer.
			\item Each non-trivial invocation of dfs branch into two paths.
			\item Time complexity: $O(2^N)$.
			\item A minor optimization: replace the second condition
				statement with
\verb|if(remaining_capacity<w[i]): return dfs(i-1,remaining_capacity);|
		\end{itemize}
\end{frame}
	\begin{frame}[fragile]
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		A effective optimization: memoization.
		\begin{Verbatim}
f=[[-1 for i in range(N)] for j in range(W)]
def dfs(i,remaining_capacity):
	if(i==0): return 0;
	if(remaining_capacity<w[i]):
		return dfs(i-1,remaining_capacity);
	if(f[i][remaining_capacity]!=-1):
		return f[i][remaining_capacity];
	f[i][remaining_capacity]=max(
			dfs(i-1,remaining_capacity),
			dfs(i-1,remaining_capacity-w[i])+v[i]);
	return f[i][remaining_capacity];
		\end{Verbatim}
\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		\begin{itemize}
			\item For each parameter tuple of dfs, the function may only branch once.
			\item Time complexity: $O(NW)$.\\
				\tiny It's a pseudo-polynomial algorithm, so the knapsack problem is still NP-complete.
				\normalsize
			\pause
			\item Why does this work?
			\pause
			\item Once the result for a specific parameter tuple has been calculated, will it change any further?
			\item Non-aftereffect property.
			\pause
			\item Recursion? Phooey! That will be a lot of context switches!
			\pause
			\item Time for some black magic!
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		The iteration version.
		\begin{Verbatim}
f=[[0 for i in xrange(N)] for j in xrange(W)]
for i in xrange(1,N):
	for j in xrange(0,W):
		f[i][j]=max(f[i-1][j],
				f[i-1][j-w[i]]+v[i] if j>=w[i] else 0);
		\end{Verbatim}
\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		\begin{itemize}
			\item Recall that in the memoization version, in order to calculate results for $f[i,remaining\_capacity]$
				we must already have at least two results for $f[i-1,x]$.
			\item Why don't we calculate all $f[i-1,x]$ before calculating $f[i,x]$?
				\pause
			\item Got the maximum value now! Want the list of selected items?
				\pause
			\item Traceback.
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		\begin{itemize}
			\item When we are at $i=x$ of the outer loop, all values in $f[y],y<x-1$ are no longer used.
			\item If we don't need to traceback, can we save a bit of memory?
				\pause
			\item Yes! Just throw them away!
			\begin{Verbatim}
f=[0 for i in xrange(W)]
for i in xrange(1,N):
	for j in xrange(W,w[i],-1):
			f[j]=max(f[j],f[j-w[i]]+v[i]);
			\end{Verbatim}
		\end{itemize}
\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{0/1 knapsack problem}
		\begin{itemize}
			\item How does this work?\\
				\pause
			($g[i][j]$ denotes the original $f[i][j]$ from the two dimensional iterative solution.)
			\item When we are at $j=y$ of the inner loop, $f[0..y]$ are values from $g[i-1]$ and
				$f[y+1..W]$ contains values from $g[i]$.
			\item Why reverse the inner loop?
				\pause
			\item Because we still need the values with smaller $remaining\_capacity$ from the last iteration!
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{Unbounded knapsack problem}
		\begin{itemize}
			\item Same as the 0/1 knapsack problem, but each item has unlimited copies.
				\pause
			\item Converting to 0/1 knapsack problem?
				\pause
			\item Imitating Binary. We can obtaining any multiplicity of items from a combination of 1x, 2x, 4x, 8x, ... of that item.
				\pause
			\item Any other solutions?
		\end{itemize}
	\end{frame}
	\begin{frame}[fragile]
		\frametitle{Knapsack problem}
		\framesubtitle{Unbounded knapsack problem}
		Another solution:
		\begin{Verbatim}
f=[0 for i in xrange(W)]
for i in xrange(1,N):
	for j in xrange(w[i],W):
			f[j]=max(f[j],f[j-w[i]]+v[i]);
		\end{Verbatim}
		Wait... Isn't this our final solution for the 0/1 knapsack problem?
\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{Unbounded knapsack problem}
		\begin{itemize}
			\item Not exactly! Note that the inner loop now iterate from $w[i]$ to $W$.
			\item Why?
				\pause
			\item Let's revisit the reason to iterate in reverse order in 0/1 knapsack problem:
			\item We still need the values with smaller $remaining\_capacity$ from the last iteration.
				\pause
			\item Why do we need \textit{those values}, instead of the shiney new values we just obtained?
				\pause
			\item Because these values do not take the current item into consideration, effectively ensuring
				that every item can be used at most once.
			\item But now we have unlimited copies of each item!
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{Bounded knapsack problem}
		\begin{itemize}
			\item Same as the 0/1 knapsack problem, but each item has $C_i$ copies.
			\item POJ 1276
				\pause
			\item Still solve by converting to a 0/1 knapsack problem.
				\pause
			\item How to limit the maximum number of copies?
				\pause
			\item By modifying the largest group so that if all groups are selected,
				the sum of multiplicity equals to $C_i$.
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{Knapsack problem}
		\framesubtitle{Bounded knapsack problem}
		Another "stupid" solution that can also be applied to the unbounded knapsack problem:
		\begin{itemize}
			\item For each item, we have $C_i+1$ choices.
			\item We just iterate through these choices to update $f[][]$.
			\item This solution runs for $O(W\Sigma C_i)$.
			\item However it can be further optimized to $O(NW)$ using some advanced DP optimization technics.
				\pause
			\item We are not covering that here today.
			\item More about knapsack problems:\\
				\url{https://github.com/tianyicui/pack}
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\begin{center}
		\Huge{以下内容仅供娱乐}
		\end{center}
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\framesubtitle{——何时应当考虑殴打队友?}
		\begin{itemize}
			\item 当队友占3个小时键盘什么都没写出来时
			\item 当队友开一题WA一题时
			\item 当队友开始表演口技时
			\item 当队友热身赛开可乐洒了一地时
			\item 当队友看到树就想重心分解时
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\framesubtitle{——正题}
		\begin{itemize}
			\item 像现在这么殴打(宣传光辉事迹)
			\item 比赛时不准碰键盘
			\item 表演口技时录音
			\item WA一题灌一瓶可乐,不准洒
			(大家可以算一下光这张图就要喝多少瓶)\\
			\includegraphics[scale=0.25]{zz2.png}\\
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\framesubtitle{——殴打队友时需要注意的地方}
		\begin{itemize}
			\item 注意殴打的度——虽然原则上是越重越好,但是如果你的队友是个卜力星人,殴打太重会导致其发射大量宇宙射线,导致「伤敌800,自损1000」的尴尬情形。
			\item 殴打方式要适当。比如其在表演口技不应该使用灌可乐的手法,因为容易洒一地。
			\item 适可而止。如果感觉队友能A题了就让其施展一发(没A就接着灌)。
		\end{itemize}
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\framesubtitle{Bonus: 利用宇宙射线}
		如果你发现你的队友会发射宇宙射线,那么它可能是可以被利用的。可利用的宇宙射线的发射者是会认错的。这里有一个正面例子和一个反面例子:
		\begin{itemize}
			\item 黄焖蓉 :发射射线导致临近的队伍接连两次CE。
			\item 宇宙智障:发射射线导致队友高数全部忘光。
		\end{itemize}
		如你所见,第一类射线是可以加以利用的;而第二类射线则是「射别人一个也射不中,射自己人一射一个准」的。大家要尽量做好对第二类射线的防护工作。关于这个问题我们下次再说(如果还有下次机会的话)。
	\end{frame}
	\begin{frame}
		\frametitle{如何殴打队友}
		\framesubtitle{So... what's the point?}
		\begin{itemize}
			\item 合理利用时间
			\item 卡题时的处理方式
			\item 队内的合作
			\item 其他队伍的影响
		\end{itemize}
	\end{frame}
\end{CJK*}
\end{document}