随笔分类 - Algorithm
warmhole
摘要:心得: 这道题为第一章,扎心了。 首先,这道题是可以暴力的,一开始想太多,不甘心暴力; 其次,这道题递归写的不熟悉,不简洁,证明对递归理解还不到位。 我的递归的思路是:如果已经选了两个点作为一对,那么剩余的点的做法应该是一样的,前提是将他们放入一个新的数组,避免读取到已经匹配的点。这种写法比较麻烦。
阅读全文
usaco combo
摘要:心得: 学到了基本知识:取模和取余是不一样的,取模与取余的定义分别为: 取余:rem(x,y)=x-y.*fix(x/y) 取模:mod(x,y)=x-y.*floor(x/y) 其中,fix()向0取整,floor向负无穷取整,当商为正数时,二者方向一致,所以结果一致,当商为负数时,方向相反,所以
阅读全文
usaco 3.1.2inflate
摘要:标准的完全背包问题,不过完全背包空间复杂度为O(vn),这道题给出的v和n都是10000,直接开内存肯定MLE,所以新接触了一个名词:滚动数组,把不需要的空间重复利用就行了,因为dp只需要知道前一个n的所有值,不需要开10000*10000的空间,只需要2*10000就行了。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:inflate 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #inc
阅读全文
usaco 3.1.3humble
摘要:坑爹啊!!!一直以为O(nk)会超时,所以想了很久只想到了O(nlogn),就是用堆,可是会超内存!!!想了很久了,不得已去网上看题解居然没看懂………………一直到今天认真地看了题解才知道是最朴素的算法!!!足足花了我五天时间啊!真伤心,不过收获还是有的,就是会用二叉堆了,还有也会用单调队列了(学优先队列的副产品),算是安慰吧。贴出代码,以作纪念!!View Code 1 /*{ 2 ID:jzy3209981 3 PROG:humble 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #includ
阅读全文
usaco 3.1.1agrinet
摘要:最小生成树问题,印象中有一种算法跟狄杰斯卡尔算法差不多就把它稍微改了下交了,然后过了,有时间好好研究最小生成树算法。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:agrinet 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int grid[100][100];13 int distanc
阅读全文
usaco 2.4.5fracdec
摘要:简单题,感觉像重温小学时候做除法的时候打草稿的感觉……小心些就好了,下面贴代码作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:fracdec 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 char fracdec[100000];12 int remain;13 int cycle[100000];14 bool
阅读全文
usaco 2.4.3cowtour
摘要:弗洛伊德算法,也是首次接触,虽然思想蛮简单,但是写出来真心话了很多时间,总有一些细节错误,还有方向上的,比如DP数组一开始用了三维,结果超内存。还有就是判断连通分量的时候也是一错再错,…………最后还是用了广搜……最后就是题目上的一些问题了,没有想太仔细结果错了。下面贴代码,作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:cowtour 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<m
阅读全文
usaco 2.4.2 maze1
摘要:应该算蛮简单的题,不需要什么优化搜索,可是居然想了我很久…………下面贴代码,作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:maze1 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h> 10 using namespace std; 11 12 int door[4]; 13 int doorstate[100][138][2]; 14 bool state[100
阅读全文
usaco 2.4.4 comehome
摘要:研究了几天最短路径,果然对图论还是很难有兴趣啊!第一次写迪杰斯特拉算法,写的超级烂,有空改改再优化下,好好想清楚细节方面……下面贴出代码以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:comehome 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int shortest[60];13 int dista
阅读全文
usaco 2.4.1ttwo
摘要:水题,模拟题,贴代码,作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:ttwo 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h> 10 using namespace std; 11 12 int state[10][10][4][10][10][4]; 13 char grid[10][10]; 14 int cow[3]; 15 int farmer[3]; 1
阅读全文
usaco 2.3.1prefix
摘要:好吧,虽然很多人说这是很简单的DP题,但是对我来说还是蛮难的,从一开始的递归超时,到后来的DP超内存,再到后来的DP爆栈,直到现在才过。不过我知道这道题可以有很多种dp方法,还可以加上HASH,可以提速,不管怎么说,DP果然还是要更近一步啊!下面贴代码,以作纪念.View Code 1 /*{ 2 ID:jzy3209981 3 PROG:prefix 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>1
阅读全文
usaco 2.3.5concom
摘要:还是水题,不过我承认代码写的很烂……下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:concom 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 int control[101][101];12 int state[101][101];13 int sum[101];14 int main()15
阅读全文
usaco 2.3.3 zerosum
摘要:题目蛮简单,就是写起来蛮麻烦,可能有更好的写法。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:zerosum 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 char formula[20];13 14 bool caculate(int n)15 {16 int result=0,i,nu
阅读全文
usaco 2.3.2 nocows
摘要:坑爹!首先吐槽下,错了一个符号调试了我一个晚上!!然后想说DP方程有时候真不容易想出来…………下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:nocows 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int kind[200][100];13 int work(int n,int h)14
阅读全文
usaco 2.2.4 lamps
摘要:本来是蛮简单的题,但是没想到要按大小排序……所以取了点巧。有空会再研究下更好的方法。因为只可能有16种可能,所以枚举就可以了。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:lamps 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h> 10 using namespace std; 11 12 bool lamb[110]; 13 int off[1
阅读全文
usaco 2.2.3 runround
摘要:蛮简单的题,直接枚举就行了,稍微剪枝,注意数字不能重复出现并且没有0.下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:runround 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 13 int move(char *fin,int n,int len)14 {15 switch(fin[n
阅读全文
usaco 2.2.2 subset
摘要:这道题看起来蛮简单,却花费我不少时间,看来功夫还是不够啊。深搜超时、递归也超时,DP才能过,现在对DP理解更深了一次额。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:subset 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int dp[40][800];13 int sum[40];1
阅读全文
usaco 2.2.1 preface
摘要:题目蛮简单,不过代码写的有点长,可能有更好的方法……下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:preface 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h> 10 using namespace std; 11 12 void t(int n,char ch[20]) 13 { 14 int i=0; 15 while(n!=0) 16 {
阅读全文
usaco 2.1.5 hamming
摘要:简单题,感觉很久没做过这么简单的题了。最小的数肯定是0,接下来枚举就行了。下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:hamming 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int bitnum(int n)13 {14 int i=0;15 while(n!=0)16 {17 n
阅读全文
usaco 2.1.4 holstein II
摘要:这是用dfs写出来的,照样调试用了很多时间,有待加强啊!!下面贴出代码,以作纪念。View Code 1 /*{ 2 ID:jzy3209981 3 PROG:holstein 4 LANG:C++ 5 }*/ 6 #include<stdio.h> 7 #include<iostream> 8 #include<string.h> 9 #include<math.h>10 using namespace std;11 12 int scoop[15][25];13 int vitamin[25];14 int minn;15 int order
阅读全文
浙公网安备 33010602011771号