随笔分类 - 算法--动态规划/贪心/基础算法
摘要:保存下当前点能走的最大步长,当下次从其他点走到该点,则能够立即判定步长. 因为起点不一定, 以及 走一次不一定能走完所有位置. 但是,每次从最高点走,总是好的.能够走的更多. 每次选择没有走过的记忆化搜索.就OK了. 注意,a[i][j]可以取0, 所以dp应该初始化小于0解题代码#include<stdio.h>#include<string.h>#include<stdlib.h>#include<vector>#include<iostream>#include<algorithm>using namespace s
阅读全文
摘要:因为 N = 100, 最大子矩阵行与行之间相邻,我们可以 O(N*N)内枚举子矩阵由哪些行组成,然后把多行合并成一行后,当一维数组求连续子序列和。时间复杂度接近 O(N*N*N) = 10^6解题代码:#include<stdio.h>#include<stdlib.h>#include<string.h>const int inf = 0x3fffffff;int a[110][110], b[110];int dp[110], ans, n;int main(){ while( scanf("%d", &n) != EOF)
阅读全文
摘要:题目大意 一颗含有N个顶点的树,节点间有权值, 节点分为黑点和白点.问题是 找一条黑点数量不超过K个的最大路径.解题思路: 因为路径只有 过根节点以及不过根节点, 所以我们可以通过找寻树重心分治下去. 问题就退化成了处理: 对于当前以 x 为根的树, 其最大路径 对于X的所有直接子节点, 定义函数 G( I, J ) 表示子节点 I为根的树不超过J个黑点的最大路径. 定义函数 dep( i ) 表示以 i为根的树,最多黑点数量. 则结果为 ans = MAX{ G(u,L1) + G(v,L2) } ( u != v, 且 L1+L2 <= K - (当前根节点...
阅读全文
摘要:我们根据置换群的性质,知道其经过 K 次置换回到最初位置,此时 K,为N元组的循环因子 a1,a2,...,ai 的最小公倍数。 现在题目要求输入N,求出最大的K,当K情况不唯一,则输出字典序最小,那么题目就转换成将 N 分解成 X个数,其最小公倍数最大的情况了。 这里关于分解N的处理,也是参考一神牛,(再次ORZ一下) 因为 N <= 100 ,我们可以使用 DP 来得到。 F[ i ][ j ] = Max { F[ i ][ j ] , LCM( F[ i-k ][ j ], k ) } 定义 F[ i ][ j ] 表示 和为 i 分解为 j 个数时 的最小公倍数最大的值...
阅读全文
摘要:题目大意和思路同上题类似, 本题特殊点为其需要将多组满足要求的结果按递增全部输出。解题代码: View Code #include<stdio.h>#include<string.h>#include<stdlib.h>using namespace std;#define MAX(a,b) (a)>(b)?(a):(b)#define MIN(a,b) (a)<(b)?(a):(b)const int N = 50010;int M[N], n;int head[N], idx;struct node{ int max, sum;}D[N];s
阅读全文
摘要:关于分治算法在树上的应用详情请查看09年QZC国家集训队论文。题目大意: 树含N个点,点之间有权值,求两点间权值和小于等于K的点对数量( N <= 10000 )解题思路:对于以rt为根节点的树,其树上两点间一条路径只有两种情况,分别为过根节点,不过根节点。 这样,启发了我们使用分治的思想来解决此题。 若不过根节点,则通过递归处理,其实也可理解为过根节点,但过了根的那部分为0.可简化代码 若过根节点, 则 dist(i)+dist(j) <= K 且 father(i) != father(j) 其中哦功能 dist(i) 为 子树上节点到根节点rt的距离, fath...
阅读全文
摘要:C. Anagramtime limit per test1 secondmemory limit per test256 megabytesStringxis ananagramof stringy, if we can rearrange the letters in stringxand get exact stringy. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but stri
阅读全文
摘要:A. Cards with Numberstime limit per test1 secondmemory limit per test256 megabytesinputinput.txtoutputoutput.txtPetya has got2ncards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from1to2n. We'll denote the number th
阅读全文
摘要:A. Boys and Girlstime limit per test1 secondmemory limit per test256 megabytesThere arenboys andmgirls studying in the class. They should stand in a line so that boys and girls alternated there as much as possible. Let's assume that positions in the line are indexed from left to right by numbers
阅读全文
摘要:1019: PalindromeTime Limit:1 SecMemory Limit:128 MBDescriptionNow we have a long long string, and we will have two kinds of operation on it.C i y: change the ith letter to y;Q i j: check whether the substring from ith letter to jth letter is a palindrome.InputThere are multiple test cases.The first
阅读全文
摘要:1014: Dice Dice DiceTime Limit:10 SecMemory Limit:128 MBDescriptionThere are 1111 ways in which five 6-sided dice (sides numbered 1 to 6) can be rolled so that the top three numbers sum to 15. Some examples are:D1,D2,D3,D4,D5 = 4,3,6,3,5D1,D2,D3,D4,D5 = 4,3,3,5,6D1,D2,D3,D4,D5 = 3,3,3,6,6D1,D2,D3,D4
阅读全文

浙公网安备 33010602011771号