summaryrefslogtreecommitdiff
path: root/sduacm2017/lec1
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2019-02-10 11:16:07 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2019-02-10 11:16:07 +0800
commit9d3c8c0e6e1a7ba43bf3dc19350d1dca68b657a3 (patch)
tree339de0698c13e1763d3361d70fb1266621025c91 /sduacm2017/lec1
downloadweb-9d3c8c0e6e1a7ba43bf3dc19350d1dca68b657a3.tar.xz
Initial commit.
Diffstat (limited to 'sduacm2017/lec1')
-rw-r--r--sduacm2017/lec1/lec.pdfbin0 -> 1393291 bytes
-rw-r--r--sduacm2017/lec1/lec.tex440
-rw-r--r--sduacm2017/lec1/zz.pngbin0 -> 3638 bytes
-rw-r--r--sduacm2017/lec1/zz1.pngbin0 -> 9919 bytes
-rw-r--r--sduacm2017/lec1/zz2.pngbin0 -> 864349 bytes
5 files changed, 440 insertions, 0 deletions
diff --git a/sduacm2017/lec1/lec.pdf b/sduacm2017/lec1/lec.pdf
new file mode 100644
index 0000000..1e79812
--- /dev/null
+++ b/sduacm2017/lec1/lec.pdf
Binary files differ
diff --git a/sduacm2017/lec1/lec.tex b/sduacm2017/lec1/lec.tex
new file mode 100644
index 0000000..af1f66d
--- /dev/null
+++ b/sduacm2017/lec1/lec.tex
@@ -0,0 +1,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}
+
diff --git a/sduacm2017/lec1/zz.png b/sduacm2017/lec1/zz.png
new file mode 100644
index 0000000..6caa4c2
--- /dev/null
+++ b/sduacm2017/lec1/zz.png
Binary files differ
diff --git a/sduacm2017/lec1/zz1.png b/sduacm2017/lec1/zz1.png
new file mode 100644
index 0000000..8e5bcf8
--- /dev/null
+++ b/sduacm2017/lec1/zz1.png
Binary files differ
diff --git a/sduacm2017/lec1/zz2.png b/sduacm2017/lec1/zz2.png
new file mode 100644
index 0000000..08da2dc
--- /dev/null
+++ b/sduacm2017/lec1/zz2.png
Binary files differ