├── README.md ├── 习题课课件 ├── 分治习题课.pptx ├── 动态规划习题课.pptx ├── 线性规划习题课1.pptx ├── 线性规划习题课2.pptx └── 贪心习题课.pptx ├── 分治1 ├── Count_Inversion.c └── River.c ├── 分治2 └── DrawACircle.c ├── 动态规划1 ├── DNA_alignment.c └── LIS.cpp ├── 动态规划2 ├── AliceFood.c └── Dance.c ├── 本科 算法设计与分析期末考试.pdf ├── 理论课作业 ├── DC.pdf ├── DC_assignment.pdf ├── DP-2022-1.pdf ├── DP_assignment.pdf ├── DP_assignment.tex ├── Greedy.pdf ├── Greedy_assignment.pdf ├── IP.pdf ├── IP_ Assignment .md └── IP_ Assignment .pdf ├── 理论课课件 ├── AIA2019-at-UCAS.pdf ├── LOA.pdf ├── Lec1.pdf ├── Lec10.pdf ├── Lec5-FFT.pdf ├── Lec5-ZengGang-alpha-beta-sort.pdf ├── Lec5.pdf ├── Lec6-HMM.pdf ├── Lec6.pdf ├── Lec7-Heap.pdf ├── Lec7-UnionFind.pdf ├── Lec7.pdf ├── Lec8-LP-InteriorPoint.pdf ├── Lec8.pdf └── Lec9.pdf ├── 算法分析与设计研讨课2022.pdf ├── 算法基础1 ├── A+B(advanced).c ├── A+B.c └── 回文串.c ├── 算法基础2 ├── MinMaxMinusMin.c └── Prime.c ├── 综合练习 └── MaxMatrix.cpp ├── 网络流 └── MaximumFlow.c ├── 贪心1 ├── Dijkstra.c └── LargestNumber.cpp └── 贪心2 ├── MidNumSumMax.c └── UnderCover.c /README.md: -------------------------------------------------------------------------------- 1 | # UCAS-AlgorithmDesignAndAnalysis 2 | 中国科学院大学2022秋季学期算法设计与分析 3 | 4 | 理论课资料+研讨课代码 5 | 6 | 仅供学习参考,请勿抄袭 7 | 8 | 更新:上传了2022-2023秋季学期算法期末考试试卷。 9 | 10 | 注:此次考试题目难度非常大,与考前老师所说“五道题是作业题小改动,两道题是不难的bonus”和考前助教所说“难度低于作业题”极度不符,且个别题目很搞心态(点名第四题)。 11 | 12 | 更新2024.7.10:上传了理论课课件 13 | -------------------------------------------------------------------------------- /习题课课件/分治习题课.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/习题课课件/分治习题课.pptx -------------------------------------------------------------------------------- /习题课课件/动态规划习题课.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/习题课课件/动态规划习题课.pptx -------------------------------------------------------------------------------- /习题课课件/线性规划习题课1.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/习题课课件/线性规划习题课1.pptx -------------------------------------------------------------------------------- /习题课课件/线性规划习题课2.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/习题课课件/线性规划习题课2.pptx -------------------------------------------------------------------------------- /习题课课件/贪心习题课.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/习题课课件/贪心习题课.pptx -------------------------------------------------------------------------------- /分治1/Count_Inversion.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/分治1/Count_Inversion.c -------------------------------------------------------------------------------- /分治1/River.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/分治1/River.c -------------------------------------------------------------------------------- /分治2/DrawACircle.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/分治2/DrawACircle.c -------------------------------------------------------------------------------- /动态规划1/DNA_alignment.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int scores(char x,char y){ 4 | if(x==y){ 5 | return 0; 6 | } 7 | if((x=='A'&&(y=='T'||y=='C'||y=='G')) || (y=='A'&&(x=='T'||x=='C'||x=='G'))){ 8 | return 5; 9 | } 10 | if((x=='T'&&(y=='C'||y=='G')) || (y=='T'&&(x=='C'||x=='G'))){ 11 | return 5; 12 | } 13 | if((x=='C'&&y=='G') || (y=='C'&& x=='G')){ 14 | return 4; 15 | } 16 | return 0; 17 | } 18 | 19 | int main(){ 20 | char a[1000]; 21 | char b[1000]; 22 | scanf("%s",a); 23 | scanf("%s",b); 24 | int m = strlen(a); 25 | int n = strlen(b); 26 | // char c; 27 | // while((c=getchar())!='\n'){ 28 | // *(a+m) = c; 29 | // m++; 30 | // } 31 | // while((c=getchar())!='\n'){ 32 | // *(b+n) = c; 33 | // n++; 34 | // } 35 | int dp[m+1][n+1]; 36 | dp[0][0] = 0; 37 | for(int i=1;i<=m;i++){ 38 | dp[i][0] = 3*i; 39 | } 40 | for(int i=1;i<=n;i++){ 41 | dp[0][i] = 3*i; 42 | } 43 | for(int j=1;j<=n;j++){ 44 | for(int i=1;i<=m;i++){ 45 | int min1 = (dp[i-1][j-1] + scores(a[i-1],b[j-1]) < (dp[i-1][j] + 3)) ? dp[i-1][j-1] + scores(a[i-1],b[j-1]):(dp[i-1][j] + 3); 46 | dp[i][j] = (min1 < (dp[i][j-1]+3)) ? min1:dp[i][j-1]+3; 47 | } 48 | } 49 | printf("%d\n",dp[m][n]); 50 | return 0; 51 | } 52 | 53 | 54 | -------------------------------------------------------------------------------- /动态规划1/LIS.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAX 1000000 4 | using namespace std; 5 | int n,a[MAX+1],l[MAX]; 6 | 7 | int lis(){ 8 | l[0] = a[0]; 9 | int length = 1; 10 | for(int i=1;i> n; 22 | for(int i=0;i>a[i]; 24 | } 25 | cout<< lis() << endl; 26 | } 27 | -------------------------------------------------------------------------------- /动态规划2/AliceFood.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/动态规划2/AliceFood.c -------------------------------------------------------------------------------- /动态规划2/Dance.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/动态规划2/Dance.c -------------------------------------------------------------------------------- /本科 算法设计与分析期末考试.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/本科 算法设计与分析期末考试.pdf -------------------------------------------------------------------------------- /理论课作业/DC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/DC.pdf -------------------------------------------------------------------------------- /理论课作业/DC_assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/DC_assignment.pdf -------------------------------------------------------------------------------- /理论课作业/DP-2022-1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/DP-2022-1.pdf -------------------------------------------------------------------------------- /理论课作业/DP_assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/DP_assignment.pdf -------------------------------------------------------------------------------- /理论课作业/DP_assignment.tex: -------------------------------------------------------------------------------- 1 | \documentclass{article} 2 | 3 | %设置页边距 4 | \usepackage{float} 5 | \usepackage{geometry} 6 | \usepackage{lmodern} 7 | \usepackage{tikz} 8 | \usepackage{graphicx} 9 | \usepackage {indentfirst} 10 | \usetikzlibrary{arrows,positioning} 11 | 12 | \geometry{left=2cm,right=2cm,top=2cm,bottom=2cm} 13 | 14 | %插入代码 15 | \usepackage{titlesec} 16 | \usepackage{listings} 17 | \usepackage{xcolor} 18 | \lstset{ 19 | numbers=left, 20 | numberstyle=\scriptsize, 21 | keywordstyle=\color{red!80}, 22 | commentstyle=\color{red!50!green!50!blue!50}\bf, 23 | frame=trbl, 24 | rulesepcolor=\color{red!20!green!20!blue!20}, 25 | backgroundcolor=\color[RGB]{245,245,244}, 26 | escapeinside=``, 27 | showstringspaces=false, 28 | xleftmargin=5em,xrightmargin=5em, 29 | aboveskip=1em, 30 | framexleftmargin=2em, 31 | } 32 | %\begin{lstlisting}[language=C++] 33 | %\end{lstlisting} 34 | %设置中文 35 | \usepackage{ctex} 36 | \usepackage{algorithm} 37 | \usepackage{algorithmicx} 38 | %\usepackage{algorithm2e} 39 | \usepackage{algpseudocode} 40 | \usepackage{amsmath} 41 | \renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm 42 | \renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm 43 | \begin{document} 44 | %\begin{CJK*}{UTF8}{gbsn} 45 | \title{Algorithm Design and Analysis - Assignment 2\\ [0.5ex] \begin{large} 2020K8009907032 \end{large}\\ [0.5ex] \begin{large} 唐嘉良 \end{large}} 46 | %\title{Assignment 1} 47 | \maketitle 48 | \tableofcontents 49 | 50 | 51 | \newpage 52 | \section{Problem One} 53 | A robber is planning to rob houses along a street. Each house has a certain amount of money 54 | stashed, the only constraint stopping you from robbing each of them is that adjacent houses have 55 | security system connected and it will automatically contact the police if two adjacent houses were 56 | broken into on the same night. 57 | 58 | 59 | 1. Given a list of non-negative integers representing the amount of money of each house, 60 | determine the maximum amount of money you can rob tonight without alerting the police. 61 | 62 | 63 | 2. What if all houses are arranged in a circle? 64 | \subsection{Algorithm Description} 65 | 采用动态规划的方法。设dp[n]为从第1个房屋开始打劫到第n个房屋所获得的总收益,circle[n-1]为从第2个房屋开始打劫到第n个房屋所获得的总收益。 66 | 对于问题1,分为是否抢劫第n个房屋给出dp的状态转移式。对于问题二,仍然分为是否抢劫第n个房屋给出状态转移式,但是必须同时给出dp和circle的状态转移式,并利用circle来求取dp[n-1]。 67 | \begin{algorithm}[htbp] 68 | \caption{Money Robbing} 69 | \begin{algorithmic}[1] 70 | \Function {MaxMoney}{$money, n$} 71 | \State dp[n],circle[n]; 72 | \State dp[0] = money[0]; 73 | \State dp[1] = max(money[0],money[1]); 74 | \State circle[0] = 0; 75 | \State circle[1] = money[1]; 76 | \State circle[2] = max(money[1],money[2]); 77 | %\State highA = n-1, highB = n-1; 78 | % \State midA = baseA[(lowA + highA) / 2], midB = baseB[(lowB + highB) / 2] 79 | % \While {highA != lowA} 80 | % \State medianA = baseA[(highA + lowA)/2]; 81 | % \State medianB = baseB[(highB + lowB)/2]; 82 | \If {n == 1} 83 | \State return money[0]; 84 | \EndIf 85 | \If {n == 2} 86 | \State return max(money[0],money[1]); 87 | \EndIf 88 | %\EndWhile 89 | \State //If houses are in a circle, do this 'for' to compute circle[] 90 | \For{$i=3$ to $n$} 91 | \State circle[i] = max(circle[i-1],circle[i-2]+money[i]); 92 | \EndFor 93 | 94 | \For{$i=2$ to $n-1$} 95 | \State dp[i] = max(dp[i-1],dp[i-2]+money[i]); 96 | \State //if in a circle, do this sentence instead of the front one 97 | \State dp[i] = max(dp[i-1],circle[i-2]+money[i]); 98 | \EndFor 99 | 100 | \State return dp[n-1]; 101 | \EndFunction 102 | \end{algorithmic} 103 | \end{algorithm} 104 | \newpage 105 | \subsection{the optimal substructure and DP equation} 106 | 最优子结构是偷取前n-1个房屋和偷取前n-2个房屋的最大收益。因为偷n个房屋的最大收益可以分为两种情况:偷第n个,那么最优子结构是偷前n-2个;不偷第n个,那么最优子结构是偷前n-1个。 107 | 动态规划的状态转移方程为dp[i] = max(dp[i-1],dp[i-2]+money[i]); 108 | 109 | 110 | 如果房屋呈环形,那么先利用同样的状态转移方程计算从第二个房屋开始偷的最大收益, 111 | 再利用计算出的最大收益计算总最大收益,状态转移方程为dp[i] = max(dp[i-1],circle[i-2]+money[i]); 112 | % \begin{figure}[H] 113 | % \centering 114 | %\includegraphics[width=18cm,height=8cm]{1.jpg} 115 | %\caption{Subproblem reduction graph in problem one} 116 | %\end{figure} 117 | \subsection{the correctness of the algorithm} 118 | 正确性的说明是简单的。对于只有一个或两个房子的情况,显然无论是否环形都是偷最有钱的那一家。 119 | 对于大于等于3个房子的情况,考虑最后一个房子是否偷,如果偷,那么倒数第二个房子不能被偷,而前n-2个房子则是自由偷, 120 | 是最优子结构,用最后一个房子的收益加上前n-2个房子的最大收益即可。 121 | 122 | 123 | 对于环形的情况,同样考虑最后一个房子是否被偷,与线性排列唯一的不同点是如果最后一个房子被偷, 124 | 那么第一个房子不能被偷,第二个到第n-2个房子是自由偷取的。于是先计算一个排除第一个房子之后的最大收益数组circle, 125 | 替换线性情况的dp[n-2]即可。 126 | \subsection{the complexity of the algorithm} 127 | 由于线性的计算方式,只需要扫一遍数组即可得到答案,无论环形还是线性排列,时间复杂度都为 T(n) = O(n). 128 | 129 | 130 | 最多只需要两个长度为n的数组,空间复杂度为 S(n) = O(n). 131 | 132 | 133 | 134 | 135 | 136 | \newpage 137 | \section{Problem Two} 138 | An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. 139 | 140 | 141 | Given an integer n, return the nth ugly number. 142 | \subsection{Algorithm Description} 143 | 本题思路是:第n个数肯定是前n-1个数中某个数的2倍或3倍或5倍,维护三个指针搜寻前n-1个数中自身2/3/5倍恰好大于第n-1个数的数即可。且下一次搜寻的时候接着上次搜寻的指针位置进行搜寻即可,减免时间开销。 144 | 145 | \begin{algorithm}[htbp] 146 | \caption{Ugly Number} 147 | \begin{algorithmic}[1] 148 | \Function {the nth ugly number}{$n$} 149 | \State uglynum[n]; 150 | \State point2=0,point3=0,point5=0; 151 | \State uglynum[0] = 1; 152 | \State uglynum[n]; 153 | \If {n == 1} 154 | \State return 1; 155 | \EndIf 156 | \For{$i=1$ to $n-1$} 157 | \While {uglynum[point2]*2 <= uglynum[i-1]} 158 | \State point2++; 159 | \EndWhile 160 | \While {uglynum[point3]*3 <= uglynum[i-1]} 161 | \State point3++; 162 | \EndWhile 163 | \While {uglynum[point5]*5 <= uglynum[i-1]} 164 | \State point5++; 165 | \EndWhile 166 | %\State return 1; 167 | \State uglynum[i] = min(uglynum[point2],uglynum[point3],uglynum[point5]); 168 | % \State //if in a circle, do this sentence instead of the front one 169 | % \State dp[i] = max(dp[i-1],circle[i-2]+money[i]); 170 | \EndFor 171 | \EndFunction 172 | \end{algorithmic} 173 | \end{algorithm} 174 | \newpage 175 | \subsection{the optimal substructure and DP equation} 176 | 最优子结构是前n-1个丑数,因为第n个丑数一定是前n-1个丑数的某一个的2或3或5倍。动态规划的状态转移方程为uglynum[i] = min(uglynum[point2],uglynum[point3],uglynum[point5])。 177 | \subsection{the correctness of the algorithm} 178 | 正确性是容易说明的。首先,第n个丑数由于只能是2、3、5的倍数,他要么是2、3、5,要么除以2或3或5之后仍然是只是2、3、5的倍数,这个数仍然是丑数,将1视作第一个丑数,我们得到:第n个丑数一定是前n-1个丑数的某一个的2或3或5倍。 179 | 180 | 181 | 因为第n个丑数肯定比第n-1个丑数大,而且按序寻找,那么三大指针目的在于寻找之前丑数的2、3、5倍的数中比第n-1个丑数大的最小的那个,只有这三个有可能是第n个丑数。 182 | \subsection{the complexity of the algorithm} 183 | 三大指针均最多需要扫一遍长度为n的数组,因此时间复杂度为T(n) = O(n). 184 | 只需要开一个数组存储计算出来的丑数,空间复杂度为S(n) = O(n). 185 | 186 | 187 | 188 | 189 | 190 | \newpage 191 | \section{Problem Three} 192 | Given n, how many structurally unique BST’s (binary search trees) that store values 1...n? 193 | \subsection{Algorithm Description} 194 | 考虑根节点的左右子树,两个二叉树同构当且仅当左右子树均同构,所以计算不同构的二叉树,仅需按节点数量分类,分别计算左右子树不同构的二叉树数目,相乘并按不同节点数量的情况进行累加即可。 195 | \begin{algorithm}[htbp] 196 | \caption{Unique Binery Search Trees} 197 | \begin{algorithmic}[1] 198 | \Function {UniqueBTNum}{$n$} 199 | \State num[n]; 200 | \State num[0] = 1, num[1] = 1; 201 | \If {$n == 0 || n==1$} 202 | \State return 1; 203 | \EndIf 204 | \For{$i=2$ to $n$} 205 | \For{$j=0$ to $i-1$} 206 | \State num[i] += num[j]*num[i-1-j]; 207 | \EndFor 208 | \EndFor 209 | \State return num[n]; 210 | \EndFunction 211 | \end{algorithmic} 212 | \end{algorithm} 213 | \newpage 214 | \subsection{the optimal substructure and DP equation} 215 | 最优子结构是前面0到n-1个节点的不同构二叉树的数目,不同构的二叉树,仅需按节点数量分类, 216 | 分别计算左右子树不同构的二叉树数目,相乘并按不同节点数量的情况进行累加即可。这个计算方法得到的答案正是卡特兰数! 217 | \subsection{the correctness of the algorithm} 218 | 对于0个或1个结点的情况,很显然只有1种独特二叉搜索树。 219 | 220 | 221 | 2个或以上节点情况下,两个二叉搜索树不同构等价于根节点左右子树之一不同构,所以对于左右子树根节点数目分类,计算不同结点数下不同构的左右子树的数量,根据组合数学的原理,对其相乘即为该节点数目情况下不同构二叉搜索树的数目,最后依节点数目情况累加即可。算法正确。 222 | \subsection{the complexity of the algorithm} 223 | 需要双重循环计算num[n],时间复杂度为T(n) = O($n^{2}$). 224 | 只需要一个数组存储计算结果,空间复杂度为S(n) = O(n). 225 | 226 | 227 | 进一步地,在发现该问题结果为卡特兰数之后,根据组合数学的知识,我们可以化简出卡特兰数的通项公式,以此代入n以进行不超过O(n)的计算(因为阶乘的计算只需要O(n)复杂度的乘除法),甚至无需写一个上面这样子的O($n^{2}$)的算法。 228 | 在允许这种优化的情况下,T(n) 不超过 O(n)。 229 | \end{document} -------------------------------------------------------------------------------- /理论课作业/Greedy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/Greedy.pdf -------------------------------------------------------------------------------- /理论课作业/Greedy_assignment.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/Greedy_assignment.pdf -------------------------------------------------------------------------------- /理论课作业/IP.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/IP.pdf -------------------------------------------------------------------------------- /理论课作业/IP_ Assignment .md: -------------------------------------------------------------------------------- 1 | # Algorithm Design and Analysis - Assignment 4 2 | 3 | ## 2020K8009907032 4 | 5 | ## 唐嘉良 6 | 7 | ### 2022年12月10日 8 | 9 | 10 | 11 | 12 | 13 | ### Problem1 14 | 15 | **Analysis**:Assume $x_{ij}=1$ means j-th job is assigned to i-th worker. Vice versa. Considering workers can work at the same time(same time used, multiple jobs done). 也就是说考虑可以并行工作。 16 | 17 | then we have: 18 | 19 | $min\space max_{i}\sum_{j=1}^{n}{C_{ij}x_{ij}}$ 20 | 21 | $s.t. \sum_{i=1}^{m}{x_{ij}} = 1$ for all $j$ 22 | 23 | $x_{ij} = 0/1$ for all $i$ and $j$ 24 | 25 | 26 | 27 | 28 | 29 | ### Problem2 30 | 31 | **Analysis**:Assume $x_{i}=1$ means $i$-th food is bought. Vice versa. At the same time, we say the nutrition is satisfied when it is greater than 0. 32 | 33 | then we have: 34 | 35 | $min \sum_{i=1}^{m}{C_{i}x_{i}}$ 36 | 37 | $s.t. \sum_{i=1}^{m}{x_{i}W_{ij}}>0$ for all $j$ 38 | 39 | $x_{i} = 0/1$ for all $i$ 40 | 41 | 42 | 43 | ### Problem3 44 | 45 | **Analysis**:Assume $x_{ij}$ is the amount of A of i-th place that is delivered to j-th market. Assume that the balance is achieved if and only if all A delivered is below the sum-output and all A accepted is below the sum-demand. 46 | 47 | then we have: 48 | 49 | $min \sum_{i=1}^{n}\sum_{j=1}^{m}{C_{ij}x_{ij}}$ 50 | 51 | $s.t. \sum_{j=1}^{m}{x_{ij}} \le a_i$ for all $i$ 52 | 53 | $\sum_{i=1}^{n}{x_{ij}} \le b_i$ for all $j$ 54 | 55 | $x_{ij} \ge 0$ for all $i$ and $j$ 56 | 57 | 58 | 59 | 60 | 61 | ### Problem4 62 | 63 | **Analysis**:Assume $x_{ij}=1$ means i-th stuff is packed into j-th box. Vice versa. 64 | 65 | then we have: 66 | 67 | $min \sum_{j=1}^{n}\vee_{i=1}^{m}{x_{ij}}$ 68 | 69 | $s.t. \sum_{j=1}^{n}{x_{ij}} \le 1$ for all $i$ 70 | 71 | $\sum_{i=1}^{n}{x_{ij}C_{i}} \le S_j$ for all $j$ 72 | 73 | $x_{ij} = 0/1$ for all $i$ and $j$ 74 | 75 | 76 | 77 | 78 | 79 | ### Problem6 80 | 81 | **Analysis**:Assume $x_{i}=1$ means $i$-th project is fund. Vice versa. 82 | 83 | then we have: 84 | 85 | $max \sum_{i=1}^{n}{(c_{i}-b_i)x_{i}}$ 86 | 87 | $s.t. \sum_{i=1}^{n}{x_{i}b_{i}}\le B$ 88 | 89 | $x_{i} = 0/1$ for all $i$ 90 | 91 | 92 | 93 | 94 | 95 | ### Problem 7 96 | 97 | **Analysis**:Assume we bulid $x$ A-dorms and $y$ B-dorms. Assume that profit is sum of $c_i$. 98 | 99 | then we have: 100 | 101 | $max\space xc_i+yc_j$ 102 | 103 | $s.t. x \le n_i$ 104 | 105 | $y \le n_j$ 106 | 107 | $xs_i + ys_j\le s$ 108 | 109 | $x,y\ge 0$ 110 | 111 | 112 | 113 | 114 | 115 | ### Problem 8 116 | 117 | **Analysis**:Assume $x_i=1$ means place $A_i$ is chosen. Vice versa. 118 | 119 | then we have: 120 | 121 | $max\sum_{i=1}^{7}{(c_{i}-b_i)x_{i}}$ 122 | 123 | $s.t. \sum_{i=1}^{7}{x_{i}b_{i}}\le B$ 124 | 125 | $x_1+x_2+x_3\le2$ 126 | 127 | $x_4+x_5\ge 1$ 128 | 129 | $x_6 + x_7 \le 1$ 130 | 131 | $x_{i} = 0/1$ for all $i$ 132 | 133 | 134 | 135 | 136 | 137 | ### Problem 9 138 | 139 | **Analysis**:Assume $x_i=1$ means i-th item is packed. Vice versa. Assume there are n items in total. 140 | 141 | then we have: 142 | 143 | $max\sum_{i=1}^{n}{c_{i}x_{i}}$ 144 | 145 | $s.t. \sum_{i=1}^{n}{x_{i}a_{i}}\le b$ 146 | 147 | $x_{i} = 0/1$ for all $i$ 148 | 149 | 150 | 151 | 152 | 153 | ### Problem 10 154 | 155 | **Analysis**:Assume $x_{ij}=1$ means j-th job is assigned to i-th worker. Vice versa. Assume that num of jobs is less than num of workers, and there are m workers and n jobs. So m$\le$n. Considering every job is assigned, but not every worker has job to do. 156 | 157 | then we have: 158 | 159 | $max \sum_{i=1}^{m}\sum_{j=1}^{n}{c_{ij}x_{ij}}$ 160 | 161 | $s.t. \sum_{i=1}^{m}{x_{ij}} = 1$ for all $j$ 162 | 163 | $\sum_{j=1}^{n}{x_{ij}} \le 1$ for all $i$ 164 | 165 | $x_{ij} = 0/1$ for all $i$ and $j$ 166 | 167 | -------------------------------------------------------------------------------- /理论课作业/IP_ Assignment .pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课作业/IP_ Assignment .pdf -------------------------------------------------------------------------------- /理论课课件/AIA2019-at-UCAS.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/AIA2019-at-UCAS.pdf -------------------------------------------------------------------------------- /理论课课件/LOA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/LOA.pdf -------------------------------------------------------------------------------- /理论课课件/Lec1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec1.pdf -------------------------------------------------------------------------------- /理论课课件/Lec10.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec10.pdf -------------------------------------------------------------------------------- /理论课课件/Lec5-FFT.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec5-FFT.pdf -------------------------------------------------------------------------------- /理论课课件/Lec5-ZengGang-alpha-beta-sort.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec5-ZengGang-alpha-beta-sort.pdf -------------------------------------------------------------------------------- /理论课课件/Lec5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec5.pdf -------------------------------------------------------------------------------- /理论课课件/Lec6-HMM.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec6-HMM.pdf -------------------------------------------------------------------------------- /理论课课件/Lec6.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec6.pdf -------------------------------------------------------------------------------- /理论课课件/Lec7-Heap.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec7-Heap.pdf -------------------------------------------------------------------------------- /理论课课件/Lec7-UnionFind.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec7-UnionFind.pdf -------------------------------------------------------------------------------- /理论课课件/Lec7.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec7.pdf -------------------------------------------------------------------------------- /理论课课件/Lec8-LP-InteriorPoint.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec8-LP-InteriorPoint.pdf -------------------------------------------------------------------------------- /理论课课件/Lec8.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec8.pdf -------------------------------------------------------------------------------- /理论课课件/Lec9.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/理论课课件/Lec9.pdf -------------------------------------------------------------------------------- /算法分析与设计研讨课2022.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JialiangTang/UCAS-AlgorithmDesignAndAnalysis/1615b026ceb95fb08604693ca2e0727b82c95b39/算法分析与设计研讨课2022.pdf -------------------------------------------------------------------------------- /算法基础1/A+B(advanced).c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAXLEN 210 4 | int res[MAXLEN]; 5 | int main(){ 6 | int a[MAXLEN],b[MAXLEN]; 7 | int sign_a=1,sign_b=1; 8 | int i=0,j=0; 9 | char c; 10 | while((c=getchar())!='\n'){ 11 | if(c=='-'){ 12 | sign_a=-1; 13 | } 14 | else{ 15 | a[i++]=c-'0'; 16 | } 17 | } 18 | while((c=getchar())!='\n'){ 19 | if(c=='-'){ 20 | sign_b=-1; 21 | } 22 | else{ 23 | b[j++]=c-'0'; 24 | } 25 | } 26 | int index_a = i-1; 27 | int index_b = j-1; 28 | int a_big=big(a,b,index_a,index_b); 29 | //printf("%d",strlen(a)); 30 | //reverse(a); 31 | /*for(int m=0;m=0 && index_b>=0){ 59 | s=a[index_a] + b[index_b] + c; 60 | c=(s>=10)?1:0; 61 | if(s>=10){ 62 | s=s-10; 63 | } 64 | 65 | res[index_res++]=s; 66 | index_a-=1; 67 | index_b-=1; 68 | } 69 | //printf("%d\n",c); 70 | while(index_a>=0){ 71 | s=a[index_a--]+c; 72 | c=(s>=10)?1:0; 73 | if(s>=10){ 74 | s=s-10; 75 | } 76 | 77 | res[index_res++]=s; 78 | } 79 | while(index_b>=0){ 80 | s=b[index_b--]+c; 81 | c=(s>=10)?1:0; 82 | if(s>=10){ 83 | s=s-10; 84 | } 85 | //c=(s>=10)?1:0; 86 | res[index_res++]=s; 87 | } 88 | if(c!=0){ 89 | res[index_res]=1; 90 | }else{ 91 | res[index_res]=0; 92 | } 93 | 94 | while(res[index_res]==0 && index_res>0){ 95 | index_res--; 96 | }//deal with the front 0,but leave the situation that res is all 0!!! 97 | if(sign_a<0){ 98 | printf("-"); 99 | }//res is minus?? 100 | while(index_res>=0){ 101 | printf("%d",res[index_res]); 102 | index_res--; 103 | }//print the res 104 | printf("\n"); 105 | } 106 | 107 | /////1111 108 | /////1000 109 | /////0000 110 | void minus(int a[],int b[],int a_big,int sign_a,int index_a,int index_b){ 111 | if(a_big == 0){//a==b, result is 0 112 | printf("0\n"); 113 | }else if(a_big == 1){//|a|>|b| 114 | int c=0,index_res=0,s; 115 | while(index_a>=0 && index_b>=0){ 116 | s=a[index_a] - b[index_b] + c; 117 | c=(s<0)?-1:0; 118 | if(s<0){ 119 | s=s+10; 120 | } 121 | 122 | res[index_res++]=s; 123 | index_a-=1; 124 | index_b-=1; 125 | } 126 | while(index_a>=0){ 127 | s=a[index_a--]+c; 128 | c=(s<0)?-1:0; 129 | if(s<0){ 130 | s=s+10; 131 | } 132 | 133 | res[index_res++]=s; 134 | } 135 | index_res--; 136 | while(res[index_res]==0 && index_res>0){ 137 | index_res--; 138 | }//deal with the front 0,but leave the situation that res is all 0!!! 139 | if(sign_a<0){ 140 | printf("-"); 141 | }//res is minus?? 142 | while(index_res>=0){ 143 | printf("%d",res[index_res]); 144 | index_res--; 145 | }//print the res 146 | printf("\n"); 147 | }else if(a_big == -1){//|a|<|b| 148 | int c=0,index_res=0,s; 149 | while(index_a>=0 && index_b>=0){ 150 | s=b[index_b] - a[index_a] + c; 151 | c=(s<0)?-1:0; 152 | if(s<0){ 153 | s=s+10; 154 | } 155 | 156 | res[index_res++]=s; 157 | index_a-=1; 158 | index_b-=1; 159 | } 160 | while(index_b>=0){ 161 | s=b[index_b--]+c; 162 | c=(s<0)?-1:0; 163 | if(s<0){ 164 | s=s+10; 165 | } 166 | 167 | res[index_res++]=s; 168 | } 169 | index_res--; 170 | while(res[index_res]==0 && index_res>0){ 171 | index_res--; 172 | }//deal with the front 0,but leave the situation that res is all 0!!! 173 | if(sign_a>0){ 174 | printf("-"); 175 | }//res is minus?? 176 | while(index_res>=0){ 177 | printf("%d",res[index_res]); 178 | index_res--; 179 | }//print the res 180 | printf("\n"); 181 | } 182 | } 183 | 184 | int big(int a[],int b[],int index_a,int index_b){ 185 | int i=0; 186 | if(index_a>index_b) 187 | return 1; 188 | else if(index_ab[i]){ 193 | return 1; 194 | }else if(a[i] 2 | int main(){ 3 | int a,b; 4 | scanf("%d %d\n",&a,&b); 5 | printf("%d\n",a+b); 6 | return 0; 7 | } -------------------------------------------------------------------------------- /算法基础1/回文串.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #define MAXLEN 10000 4 | int main(){ 5 | char s[MAXLEN]; 6 | char cin; 7 | int j = 0; 8 | while((cin=getchar())!=EOF){ 9 | if((cin>='0' && cin<='9')||(cin>='A' && cin<='Z')||(cin>='a' && cin<='z')){ 10 | s[j++] = cin; 11 | } 12 | } 13 | j--; 14 | //int len = strlen(s); 15 | int i = 0; 16 | int flag = 0; 17 | for(;i<=j;i++,j--){ 18 | if((s[i] == s[j]) || (s[i]>='A' && s[i]<='Z' && s[j] == s[i] + 'a'-'A') || (s[j]>='A' && s[j]<='Z' && s[i] == s[j] + 'a'-'A')) 19 | ; 20 | else{ 21 | flag = 1; 22 | } 23 | } 24 | printf(flag?"NO\n":"YES\n"); 25 | return 0; 26 | } -------------------------------------------------------------------------------- /算法基础2/MinMaxMinusMin.c: -------------------------------------------------------------------------------- 1 | #include 2 | //#define MAXLEN 100000 3 | int main(){ 4 | int n,k; 5 | scanf("%d %d",&n,&k); 6 | char c; 7 | int s[n]; 8 | int i=0; 9 | for(i=0;i=0){ 17 | if(s[i]b_max[i+1]){ 23 | b_max[i]=s[i]; 24 | } else{ 25 | b_max[i]=b_max[i+1]; 26 | } 27 | i--; 28 | } 29 | 30 | f_min[0]=s[0]; 31 | f_max[0]=s[0]; 32 | i=1; 33 | while(i<=n-1){ 34 | if(s[i]f_max[i-1]){ 40 | f_max[i]=s[i]; 41 | } else{ 42 | f_max[i]=f_max[i-1]; 43 | } 44 | i++; 45 | } 46 | 47 | //int min_all[n-k+1]; 48 | int min_min; 49 | int res; 50 | for(i=0;ib_max[i+k])?f_max[i-1]:b_max[i+k]; 61 | int min=(f_min[i-1]>b_min[i+k])?b_min[i+k]:f_min[i-1]; 62 | res=max-min; 63 | if(res 2 | int main(){ 3 | int t; 4 | scanf("%d",&t); 5 | //getchar() 6 | int num[t]; 7 | int i=0,j=0,m=0; 8 | while(i 3 | using namespace std; 4 | #define INF 2147483 5 | #define MAXN 30001 6 | int n,m,s,t; 7 | struct node{ 8 | int net,to; 9 | int w; 10 | }e[MAXN]; 11 | int head[MAXN],tot; 12 | void add(int x,int y,int z){ 13 | e[++tot].net=head[x]; 14 | e[tot].to=y; 15 | e[tot].w=z; 16 | head[x]=tot; 17 | } 18 | int de[MAXN]; //层次 19 | int now[MAXN]; 20 | 21 | bool bfs(){ 22 | queueq; 23 | for(int i=1;i<=n;i++) de[i]=INF; 24 | q.push(s); 25 | de[s]=0; 26 | now[s]=head[s]; //副本 27 | while(!q.empty()){ 28 | int x=q.front(); 29 | q.pop(); 30 | for(int i=head[x]; i; i=e[i].net){ 31 | int y=e[i].to,z=e[i].w; 32 | if(z!=0 && de[y]==INF){ 33 | q.push(y); 34 | now[y]=head[y]; 35 | de[y]=de[x]+1; //更新层次 36 | if(y==t) return true; 37 | } 38 | } 39 | } 40 | return false; 41 | } 42 | 43 | int dfs(int x,int ff){ 44 | if(x==t) return ff; 45 | int k,ans=0; 46 | for(int i=now[x] ;i && ff; i=e[i].net){ 47 | now[x]=i; 48 | int y=e[i].to; 49 | if(e[i].w!=0 && (de[y]==de[x]+1)){ 50 | k=dfs(y,min(ff,e[i].w)); 51 | if(!k) de[y]=INF; //剪枝 52 | e[i].w-=k; 53 | e[i^1].w+=k; //更新 54 | ans+=k; 55 | ff-=k; 56 | } 57 | } 58 | return ans; 59 | } 60 | int main(){ 61 | scanf("%d%d%d%d",&n,&m,&s,&t); 62 | tot=1; 63 | for(int i=0;i 2 | #include 3 | #define MAX 1000000 4 | //只过了9个测试点,第十个测试点规模n=10000,为过这个测试点,需要同时修改数据结构和算法 5 | //应采用邻接表,并使用优先队列 6 | int main(){ 7 | int n,m,s,t; 8 | scanf("%d %d %d %d\n",&n,&m,&s,&t); 9 | int dis[n+1][n+1]; 10 | int dist[n+1],explore[n+1]; 11 | for(int i=1;iminn)?minn:dist[i]; 52 | } 53 | } 54 | } 55 | // for(int i=1;i 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | #define max 100000 7 | bool cmp(string x,string y) 8 | { 9 | return (x+y>y+x)?1:0; 10 | } 11 | int main() 12 | { 13 | int n; 14 | string num[max]; 15 | scanf("%d\n",&n); 16 | for(int i=0; i>num[i]; 18 | } 19 | sort(num,num+n,cmp); 20 | for(int i=0; i 2 | #include 3 | #include 4 | #define MAX 1000000 5 | int cmp(const void* a, const void* b) 6 | { 7 | return (*(int*)a > *(int*)b)?1:-1; 8 | } 9 | 10 | int main(){ 11 | int n,m; 12 | scanf("%d %d\n",&n,&m); 13 | int matrix[n*m]; 14 | for(int i=0;i 2 | #include 3 | #include 4 | #define MAX 1000000 5 | typedef struct{ 6 | int a; 7 | int b; 8 | }wodi; 9 | int cmp(const void* a, const void* b) 10 | { 11 | wodi ac = *(wodi*)a; 12 | wodi bd = *(wodi*)b; 13 | // if (ac.a != bd.a) 14 | // return (ac.a > bd.a)?1:-1; 15 | // else return (ac.b > bd.b)?1:-1; 16 | return (ac.a - bd.b - bd.a + ac.b); 17 | } 18 | 19 | int main(){ 20 | int n; 21 | scanf("%d\n",&n); 22 | 23 | wodi p[n]; 24 | //int a[n+1],b[n+1]; 25 | for(int i=0;i