![](https://img-blog.csdnimg.cn/20201014180756930.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法竞赛入门经典
exmy
Less is more, and more is less.
展开
-
nyoj--16 矩形嵌套(经典DP)
nyoj 16题意有n个矩形,每个矩形可以用a,b来描述,表示长和宽。矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<da<c,b<d或者b<c,a<db<c,a<d(相当于旋转X90度)。例如(1,5)可以嵌套在(6,2)内,但不能嵌套在(3,4)中。你的任务是选出尽可能多的矩形排成一行,使得除最后一个外,每一个矩形都可以嵌套在下一个矩形内。题解按白书上的分析,矩形之间的“可嵌套”原创 2016-02-27 11:37:04 · 381 阅读 · 0 评论 -
UVa--1636 Headshot(概率)
UVa 1636题意你和人决斗。决斗规则如下:用一把有n个弹槽的左轮手枪,对着自己脑袋来一枪,孰生孰死看天意。现在对方已经装了若干发子弹,并随机转了一下转轮,子弹呢用一个01序列表示,0表示这个弹槽无子弹,1表示有子弹。对方先对着自个脑袋开了一枪,嗯,你只听到了一声’click’,人还好好的,是空枪。现在轮到你了,摆在你面前的有两个选择,一是直接对自己开射,二是转一下转轮再来开射,显然你会选择生还希原创 2016-02-19 14:27:47 · 1023 阅读 · 0 评论 -
UVa--12716 GCD XOR(math)
UVa 12716题意给定整数n,求有多少对整数(a, b)满足: 1 <= b <= a <= n,且 gcd(a, b) = x ^ b.题解设 c=gcd(a,b)=a Xor bc = gcd(a, b) = a \text{ Xor } b, 则: c 是 a 的约数,b=a Xor cb = a \text{ Xor } c 因此,可以枚举 a 和 c,为降低枚举的复杂度,利用 c原创 2016-02-18 23:07:35 · 390 阅读 · 0 评论 -
UVa--10791 Minimum Sum LCM(math)
UVa 10791题解算术基本定理的应用。 将n分解,n=pa11pa22...pakkn = p_1^{a_1}p_2^{a_2} ... p_k^{a_k},可知将每个pakkp_k^{a_k}作为一个因子时最优。 注意当 n = 1和 n 为素数的时候,最小和为 n+1n + 1.#include <bits/stdc++.h>using namespace std;int main()原创 2016-02-18 21:11:55 · 332 阅读 · 0 评论 -
UVa--10375 Choose and divide(math)
UVa 10375题解C(p,q)C(r,s)=p!(r−s)!s!q!(p−q)!r!\frac{C(p, q)}{C(r, s)} = \frac{p!(r - s)!s!}{q!(p - q)!r!} 根据算术基本定理:n=pa11pa22...pakkn = p_1^{a_1} p_2^{a_2}...p_k^{a_k},其中pkp_k为素数,ai∈N+a_i \in N_+. n!=1原创 2016-02-18 19:04:12 · 434 阅读 · 0 评论 -
UVa--400 Unix ls(格式输出)
UVa 400题解模拟 Unix 的 ls 命令。 输入n以及n个文件名,排序后按照列优先的次序左对齐输出。 注意: The rightmost column will be the width of the longest filename and all other columns will be the width of the longest filename plus 2. 据此原创 2016-02-15 16:19:18 · 522 阅读 · 0 评论 -
UVa--1594 Ducci Sequence(模拟)
UVa 1594题解既然输入保证最多1000步就会变成0或者循环,那么只要执行1000步判断是否会变成0即可.#include <bits/stdc++.h>using namespace std;const int maxn = 15;int a[maxn];int n;inline bool reachZero(){ for(int i = 0; i < n; ++i)原创 2016-02-15 15:06:42 · 497 阅读 · 0 评论 -
UVa--1593 Alignment of Code(string)
UVa 1593题意:输入若干行代码文本,要求各列单词的左边界对齐且尽量靠左。单词之间至少要空一格。Sample Input start: integer; // begins herestop: integer; // ends heres: string;c: char; // tempSample Outputstart: integer; // begins her原创 2016-02-15 14:31:57 · 435 阅读 · 0 评论 -
UVa--679 Dropping Balls(模拟)
UVa-679题解:如果模拟这这所有I个小球的小落,会超时。直接模拟最后一个小球的路线。#include #include #include #include using namespace std;int main(){ int t, D, I; cin >> t; while(t--) { cin >> D >原创 2016-02-14 17:20:03 · 432 阅读 · 0 评论 -
UVa--12657 Boxes in a line(双向链表)
UVA--12657题解:lrj大法。采用双向链表,编号为 i 的的盒子左右两边的盒子编号分别为 left[i] 和 right[i] 。技巧:操作4反转需要修改所有元素的指针,为避免时间消耗,增加一个标记 inv, 表示有无反转,这样就并不需要真的去执行操作4.inv = 1表示奇数次反转,inv = 0表示偶数次反转。当inv = 1的时候,操作1和2需要反过来,操作3原创 2016-02-13 22:31:28 · 453 阅读 · 0 评论 -
UVa--10491 Cows and Cars(math)
UVa 10491题解设 a = NCOWS, b = NCARS, c = NSHOW 选择某扇门后,主持人再打开 c 个牛门,这时还剩下 a + b - c - 1 扇门未开,也只有这些门是可换的。 假设一开始选择了牛门:最后选到车的概率为, aa+b∗ba+b−c−1\frac{a}{a + b} * \frac{b}{a + b - c - 1} 假设一开始选择了车门:最后选到车的概率原创 2016-02-19 15:23:13 · 405 阅读 · 0 评论 -
UVa--11181 Probability|Given(math)
UVa 11181题意n个人去超市逛,其中第ii个人买东西的概率为PiP_i,已知有rr个人买了东西,计算每个人实际买了东西的概率。 1<=n<=201 <= n <= 20, 0<=r<=n0 <= r <= n题解实际上这是计算条件概率:P(Ei|E)=P(EiE)P(E)P(E_i | E) = \frac{P(E_i E)}{P(E)} 其中 P(Ei)P(E_i)表示第ii个人买东西的原创 2016-02-19 22:14:19 · 359 阅读 · 0 评论 -
UVa--437 The Tower of Babylon(dp)
UVa 437题意有 n 种长方体石块(x,y,zx, y, z),每种无限多,从中挑选一些石块组成一座塔,要求每个长方体石块的底面长宽严格小于它下方石块的底面长宽。每个石块可以任意选择一边作为高。求塔的最大高度。n<=30n <= 30.题解每个长方体可以有6种形态,那么这 n 种长方体共有 6n6n种形态,两种形态的长方体的底面长宽是一个二元关系,因此,这便转化为一个DAG图,和矩形嵌套类似。#原创 2016-02-28 14:51:45 · 375 阅读 · 0 评论 -
UVa--548 Tree(二叉树)
UVa 548题解根据中序后序序列直接建立(lch[], rch[]),表示结点的左右子节点的权值。#include <iostream>#include <cstdio>#include <string>#include <sstream>#include <climits>#include <algorithm>using namespace std;const int maxn =原创 2016-07-14 20:02:57 · 516 阅读 · 0 评论 -
Uva--116 Unidirectional TSP(dp)
UVa 116题解设d(i, j)为从(i, j)出发到达最后一列的最小开销, 状态转移:d(i, j) = d(min{i, (i - 1 + m) % m, (i + 1) % m}, j + 1) 初始条件:d(i, j) = a(i, j), 当j = n - 1, 即最后一列的时候 从最后一列往前递推。#include <bits/stdc++.h>using namespace原创 2016-06-11 11:00:49 · 620 阅读 · 0 评论 -
hdoj--1532 Drainage Ditches(网络流/最大流)
hdoj 1532题解#include <iostream>#include <cstdio>#include <algorithm>#include <queue>#include <vector>#include <cstring>using namespace std;const int maxn = 200 + 10;const int inf = 0x7fffffff;st原创 2016-05-27 09:43:19 · 423 阅读 · 0 评论 -
UVa--11572 Unique Snowflakes(尺取法)
UVa 11572题意在序列中找到一个尽量长的连续子序列,使得该序列中没有相同的元素。题解尺取法。 设连续子序列为 S[L..R]S[L..R], 初始 L=R=0L = R= 0,R不断增加直到序列中会出现重复元素,这时子序列无法再向右延伸了,那么增加L,然后继续延伸R.判断是否可以延伸可以用set。 总的时间复杂度:O(nlogn)O(nlogn)#include <bits/stdc++.原创 2016-04-26 22:59:04 · 494 阅读 · 0 评论 -
UVa--10048 Audiophobia(floyd)
UVa 10048题意求图中两点最大噪声值最小的路径。题解Floyd算法变形一下: d[i][j]=min(d[i][j],max(d[i][k],d[k][j]))d[i][j] = min(d[i][j], max(d[i][k], d[k][j]))#include <bits/stdc++.h>using namespace std;const int maxn = 105;const原创 2016-03-29 17:32:16 · 412 阅读 · 0 评论 -
UVa--1395 Slim Span(生成树)
题意给定带权无向图 GG,定义: The slimness of a spanning tree T is defined as the difference between the largest weight and the smallest weight among the n−1 edges of T. 求 slimness 尽可能小的生成树。题解把边按权值从小到大排序,枚举每一个边的区原创 2016-02-22 22:08:40 · 442 阅读 · 0 评论 -
UVa--10129 Play on Words(欧拉通路)
UVa 10129题意n个单词,是否可以把它们排成一个序列,是的每个单词的第一个字母的前一个单词的最后一个字母相同。输入中可以有重复单词。题解把字母看作结点,单词看作边,那么这题便是判断构成的有向图中是否存在欧拉通路。 有向图存在欧拉通路需要满足两个条件: 1. 它的无向图连通; 2. 顶点度数满足:顶点的入度等于出度,或者有两个顶点,一个入度比出度大1,另一个出度比入度大1,其他顶点入度等于原创 2016-02-21 11:26:22 · 491 阅读 · 0 评论 -
UVa--10305 Ordering Tasks(拓扑排序)
UVa 10305题解简单的拓扑排序。#include <bits/stdc++.h>using namespace std;const int maxn = 100 + 5;int graph[maxn][maxn];int indegree[maxn];int n, m;void topo(){ queue<int> Q; vector<int> ans; for原创 2016-02-20 15:33:28 · 442 阅读 · 0 评论 -
UVa--514 Rails (stack)
UVa--514题意:判断一个出栈序列是否合法。题解:典型的栈应用,用栈模拟。注意蛋疼的格式。#include #include #include #include #include using namespace std;int n;vector a;bool judge(){ stack S; int j = 0; for(int原创 2016-02-13 15:18:15 · 402 阅读 · 0 评论