From 9d3c8c0e6e1a7ba43bf3dc19350d1dca68b657a3 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Sun, 10 Feb 2019 11:16:07 +0800 Subject: Initial commit. --- sduacm2017/lec1/lec.pdf | Bin 0 -> 1393291 bytes sduacm2017/lec1/lec.tex | 440 ++++++++++++++++++++++++++++++++++++++++++++++++ sduacm2017/lec1/zz.png | Bin 0 -> 3638 bytes sduacm2017/lec1/zz1.png | Bin 0 -> 9919 bytes sduacm2017/lec1/zz2.png | Bin 0 -> 864349 bytes 5 files changed, 440 insertions(+) create mode 100644 sduacm2017/lec1/lec.pdf create mode 100644 sduacm2017/lec1/lec.tex create mode 100644 sduacm2017/lec1/zz.png create mode 100644 sduacm2017/lec1/zz1.png create mode 100644 sduacm2017/lec1/zz2.png (limited to 'sduacm2017/lec1') diff --git a/sduacm2017/lec1/lec.pdf b/sduacm2017/lec1/lec.pdf new file mode 100644 index 0000000..1e79812 Binary files /dev/null and b/sduacm2017/lec1/lec.pdf 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 | in a terminal. + \item Run it! + Type \verb|./| 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] 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