
USACO 题解
文章平均质量分 78
USACO 的题目解答
DamonHao
明光村某附属大学
展开
-
USACO 4.1 Fence Loops (fence6)
/* 这题是求无向图中的一个最小环的长度。主要思路是:因为边都是直线,边的两点之间的最短距离必然是这个边长。那么,再求一条到两顶点的最短距径,这个路径与边构成了一个环。这个环是包含该边的最小环。枚举一下所有边,计算出最小环即可。对于每个边,删除该边,然后计算两顶点的最短路径,再恢复该边。 但是这个图的输入是用边表示的,一个难点就是将其转换成用点表示。这里用边的集合来表示一个点原创 2014-03-02 15:15:10 · 1703 阅读 · 0 评论 -
USACO 4.1 Fence Rails (fence8)
/*既然要装下尽可能多的物品,那么就应该先选入小的物品。所以,先把物品按照重量递增排序。那么:1)如果前k物品不能装入背包,那么即使把其中一个物品P换成k+1~R中的一个物品Q, 由于Q的重量大于P,因此也绝对不可能成功装入背包。2)如果前k个物品可以装入背包,那么前k-1个物品也一定能装入背包。3)如果前k个物品不能装入背包,那么前k+1个物品也一定不能装入背包。这样,一个最值问原创 2014-03-02 15:14:13 · 2111 阅读 · 0 评论 -
USACO 4.1 Beef McNuggets (nuggets)
/*这题有些难。虽然知道是动态规划题,但是不知道要开多大的数组,后来看analysis用一个256大小的数组循环使用,方法很巧妙。先将box进行排序。如果box里面的数的最大公约数不为1的话,那么所有组成的数,只可能是这个公约数的倍数,因此没有上限,输出为0.用last记录最小的“不能组成的数”。这样当last之后有boxs[0]个连续数都可以组成的话,那么所有的数都可以组成。last+原创 2014-03-02 15:12:18 · 1304 阅读 · 0 评论 -
USACO 3.4 Raucous Rockers (rockers)
/*Main idea:一道动态规划题,但观察数据规模,穷举就行了。 穷举每首歌是否选取所有的组合可能(2^20种),算出每种情况所有光盘上一共能存的歌曲数目,保留最大值即可。对于穷举每首歌是否选取所有的组合可能,我采用了位运算的高效方法limit=(1 << N)-1;for (i=0;i<=limit;i++)然后i对应的每种状况计算能装进光盘中的最大的歌曲数目即可。refer原创 2014-03-02 14:57:57 · 1754 阅读 · 0 评论 -
USACO 3.4 Electric Fence (fence9)
/*可以算是一道数学题吧。如果知道皮克定理就好写多了。皮克定理说明了其面积S和内部格点数目a、边上格点数目b的关系:S = a + b/2 - 1。 根据三角形面积公式求出S。如果知道了b,那么三角形内部格点数目a也就求出来了。 可以证明,一条直线((0,0),(n,m))上的格点数等于n与m的最大公约数+1。 即b=gcd(n,m)+1. gcd(n,m)为n与m的最大公约数。 代入皮原创 2014-03-01 14:00:42 · 1136 阅读 · 0 评论 -
USACO 3.4 American Heritage (heritage)
/*Main idea:Choose node in pre order, and then use in order info to jude left or right.Througt this we can build a tree. Finally, we wall through the tree in post order to get answer;From this pro原创 2014-03-01 13:59:50 · 1000 阅读 · 0 评论 -
USACO 3.4 Closed Fences (fence4)
/*Main idea:1.判断多边形是否合法任两条边都不相交即合法,注意这里的相交是严格相交,顶点相交不算相交。2.二分法判断当前线段 seg_a 是否可见假设观察点为 eye,seg_a 的两个端点分别为st 和 end。判断视线(eye,st)和(eye,end)是否与其他线段(即 fence)相交。如果都不相交,则seg_a 可见。如果两视线均与某一 fence 相交,则s原创 2014-03-01 13:58:52 · 1759 阅读 · 0 评论 -
USACO 3.3 A Game (game1)
/*博弈问题,可以使用动态规划求解。 状态定义:用F[i][j]表示第一个玩家先取时,在第i到第j的子序列中能拿到的最高分;用S[i][j]表示第i到第j的子序列中所有数字的和;用num[i]表示第1到第n的序列中第i个数。边界条件:F[i][i]=num[i]状态转移方程: F[i][j]=max{num[i]+S[i+1][j]-F[i+1][j],num[j]+S[i][j-1]-原创 2014-03-01 13:57:36 · 1406 阅读 · 0 评论 -
USACO 3.3 Home on the Range (range)
/*Main idea这道题可以动态规划。二维的动态规划。状态定义:G[i][j]为以(i,j)为左上角顶点的正方形的最大边长。边界条件:G[i][j]为初始读入的矩阵。状态转移方程: G[i][j]=min{ G[i+1][j] , G[i][j+1] , G[i+1][j+1] } + 1;解析: G[i+1][j] , G[i][j+1] , G[i+1][j+1]分别为(i,j原创 2014-03-01 13:56:38 · 1069 阅读 · 0 评论 -
USACO 3.3 Camelot (camelot)
/*Main Idea;dist[x1][y1][x2][y2] denote the knight's min move from (x1,y1) to (x2,y2);king_move_num[x][y] denote the king's min move from its initial place to the square it meet a knight.We can co原创 2014-02-28 13:49:46 · 1667 阅读 · 0 评论 -
USACO 3.3 Shopping Offers (shopping)
/*Main idearDP problem. Take each offer as a single choise for DP.dp[a1][a2][a3][a4][a5] = min{dp[a1-prodcut_num[i][1]][a2-prodcut_num[i][2]] [a1-prodcut_num[i][3]][a1-prodc原创 2014-02-28 13:48:00 · 1430 阅读 · 0 评论 -
USACO 3.3 Riding The Fences (fence)
/*Main idea Find Euler Path, not circuit, so take care to choose a start node whosedegree is odd;*//*Executing... Test 1: TEST OK [0.000 secs, 3512 KB] Test 2: TEST OK [0.000 secs, 3512 KB原创 2014-02-28 13:46:24 · 1367 阅读 · 0 评论 -
USACO 3.2 Sweet Butter (butter)
/*Main idea:Use SPFA(i) to compute the shortest paths from single source i; i is in [1..P], P is the number of pastures; and then compute the total distance of paths each cow to source i; SPFA m原创 2014-02-28 13:43:55 · 1432 阅读 · 0 评论 -
USACO 3.2 Magic Squares (msquare)
/*这道题类似于八数码难题,基本思想是宽搜,使用Hash判重。如果使用一般的八维数组空间可以达到8^8=16777216,会超过USACO的16MB空间限制。所以我们应该对状态进行散列存储,观察发现每位的数字不能重复,存在空间冗余。我们可以对于每个状态建立一个映射,采用康托展开算法。(参见Nocow) 定义cantor(s)为s串大大小顺序。可样将哈希容量缩减到8!=40320。另外发原创 2014-02-28 13:42:32 · 1682 阅读 · 0 评论 -
USACO 3.2 Feed Ratios (ratios)
/*Main ideaEnumeration, because there are 100^3 cases;Note the case where ratios may be 0;*//*ID: haolink1PROG: ratiosLANG: C++*///#include #include using namespace std;int goal[3];in原创 2014-02-27 13:51:38 · 1452 阅读 · 0 评论 -
USACO 3.2 Spinning Wheels (spin)
/*Main ideaThe key point of this problem is you should note the fact that at 360 second, all 5 wheels will go back to his initial state; see 360 * speed is a full period;So only the time [0,360) i原创 2014-02-27 13:50:16 · 1425 阅读 · 0 评论 -
USACO 3.2 Stringsobits (kimbits)
/*动态规划。尝试暴搜,但是超时了分析:设长度为n的01串,1的个数不大于v的个数为dp[n,v]方程:dp[n,v]=dp[n-1,v]+dp[n-1,v-1]; //分别表示在当前位加上0和加上1时的两种状况边界:dp[i,0]=dp[0,j]=1;这样我们得到了所有的dp[n,v],需要做的就是据此构造出所求字符串.设所求串为S,假设S的位中最高位的1在自右向左第K+1位,那原创 2014-02-27 13:48:25 · 1526 阅读 · 0 评论 -
USACO 3.2 Factorials (fact4)
/*F[i] denote the 5 digits from rightmost non-zero digit of i! ;There is a trap, we can't just store the rightmost non-zero digit of i!Eg: 15 * 2 = 30 may affect the second last digit;Since N is 4原创 2014-02-27 13:47:05 · 1162 阅读 · 0 评论 -
USACO 3.1 Stamps (stamps)
/*Main idea:This is a good Dynamic programming problem;dp[i]=min{dp[i-value[j]]}+1 // (j=1..n and i-value[j]>=0) dp[i] means that to make up postage i,we need dp[i] stamps at least;So, we increa原创 2014-02-27 13:45:49 · 1106 阅读 · 0 评论 -
USACO 3.1 Contact (contact)
/* 经典的位运算解题,边读入边计算。 设置极限掩码为limit=(1<<(B))-1; //2的B此次方-1 每读入一个二进制数0或1,令unsigned int数字串str=((str<<1)+c) & limit; 然后扫描str,从末尾向前扫描i=(A到B)位,把所得的数字串t最高位添加1,以区别有前导0的串,例如001和01,添加后变为1001和101mask=(1<<i)-1原创 2014-02-26 13:46:49 · 5821 阅读 · 0 评论 -
USACO 3.1 Shaping Regions (rect1)
/*思路大概是这样的,首先读入所有的矩形。我们可以发现最后覆盖的矩形不会被其他矩形覆盖,所以可以考虑从后向前覆盖。对于每个矩形,我们把它和有可能覆盖在它上面的矩形(就是出现在当前矩形后面的矩形)比较,如果两个矩形有重叠部分就把重叠部分去掉,把当前矩形分成几个小矩形递归进行分割。直到当前矩形与后面的矩形全部没有公共部分,累加矩形的面积。Refer to byvoid;*//*ID:原创 2014-02-26 13:44:42 · 1298 阅读 · 0 评论 -
USACO 3.1 Humble Numbers (humble)
/*usaco official solutionWe compute the first n humble numbers in the "hum" array. For simplicity of implementation, we treat 1 as a humble number, and adjust accordingly.Once we have the first k原创 2014-02-26 13:42:01 · 2031 阅读 · 0 评论 -
USACO 3.1 Score Inflation (inflate)
//Main idea://Dynamic Programming //f[i][j] = max{f[i-1][j],f[i][j-cost[i]]+weight[i]}; //f[i][j] denote the max sum of weights(in this case, that is points) //from the first i categories(start fr原创 2014-02-26 13:40:36 · 1281 阅读 · 0 评论 -
USACO 3.1 Agri-Net (agrinet)
//Main idea//Minimun Spannig Tree, we use Kruskal's algorithm;/*ID: haolink1PROG: agrinetLANG: C++*///#include #include #include using namespace std;class Edge{public: int x_; i原创 2014-02-26 13:38:25 · 1523 阅读 · 0 评论 -
USACO 2.4 Fractions to Decimals (fracdec)
// Main idea// mimic the division process; During this process, we record the // remainder. If the one remainder have appeared before, then the the decimal // representation has a repeating sequen原创 2014-02-25 13:59:38 · 1555 阅读 · 0 评论 -
USACO 2.4 Bessie Come Home (comehome)
//Main idea//Calculate the shortest path from 'Z' to ohter pastures by Dijkstra algorithm;//And then choose shortest path among 'A' to 'Y';/*ID: haolink1PROG: comehomeLANG: C++*///#include原创 2014-02-25 13:58:17 · 1177 阅读 · 0 评论 -
USACO 2.4 Cow Tours (cowtour)
//Main Idea//Use floyd algorithm to calculate shortest path of each pair of pastures //The compute the smallest possible diameter of the new connected field by enumeration//The key point is that th原创 2014-02-25 13:56:18 · 1445 阅读 · 0 评论 -
USACO 2.4 Overfencing (maze1)
//Main idea//First we find the two exits and use flood fill to calculate their distance//to any other block;Compare each block distance to exits and we can find the minimal number of //steps that g原创 2014-02-25 13:54:59 · 1434 阅读 · 0 评论 -
USACO 2.4 The Tamworth Two (ttwo)
//Main idea://brute force//We use record[farmer.x][farmer.y][cows.x][cows.y][farmer.direct][cows.direct] to record //all the state of farmer and cows in one minutes, if they come back to one state原创 2014-02-25 13:53:30 · 1073 阅读 · 0 评论 -
USACO 2.3 Controlling Companies (concom)
//Main Idea://brute force;//shares[i][j] is the initial shares company i own for company j;//fianl_shares[i][j] is the shares company i own for j after calculate;//used[i][j] == 1 denote i control原创 2014-02-24 13:40:29 · 1421 阅读 · 0 评论 -
USACO 2.3 Money Systems (money)
//Main idea://Dynamic Programming//dp[i][j] denote the number of ways to construct money j by coins[0...i]//state transition equation://dp[i][j] = dp[i-1][j] + dp[i-1][j-coins[i]] + ...dp[i-1][j-原创 2014-02-24 13:39:00 · 1243 阅读 · 0 评论 -
USACO 2.3 Zero Sum (zerosum)
//Main Idea://Search by DFS;//In ASCII order means that cout the sum of string's ASCII value, and sort//them in ascending order;/*ID: haolink1PROG: zerosumLANG: C++*///#include #include u原创 2014-02-24 13:30:14 · 1314 阅读 · 0 评论 -
USACO 2.3 Cow Pedigrees (nocows)
//Main idea: //Dynamic Programming//dp[i][j] denote the number of binary trees whose node number is i and height is less or equal to j;//state transition equation://dp[i][j] = dp[m][j-1]*dp[i-1-m]原创 2014-02-24 13:28:40 · 1042 阅读 · 0 评论 -
USACO 2.3 The Longest Prefix (prefix)
//Main Idea://Dynamic Programming//For String srt which length is len,//match_str_len[i] means the max len prefix of str[i...len-1];//DP state transition equation://If str[i...i+primitive_len] ma原创 2014-02-24 13:27:06 · 999 阅读 · 0 评论 -
USACO 2.2 Party Lamps (lamps)
//Main idea://Reading the problem condition, we can know that//1: the order we press the button mean nothing for the final lamp state;//2: when the button is pressed even times, it mean nothing for原创 2014-02-23 15:22:36 · 954 阅读 · 0 评论 -
USACO 2.2 Runaround Numbers (runround)
//Main Idea//First check whether the digits are unique//then check the runround property;/*ID: haolink1PROG: runroundLANG: C++*///#include #include using namespace std;typedef unsigned原创 2014-02-23 15:21:31 · 941 阅读 · 0 评论 -
USACO 2.2 Subset Sums (subset)
//Main Idea//Dynamic Programming.This problem is variant of 0-1 knapsack problem.// ans[i][j] means the subset number whose sum is j for the set {1,2,..,i};/*ID: haolink1PROG: subsetLANG: C++*原创 2014-02-23 15:20:20 · 1205 阅读 · 0 评论 -
USACO 2.2 Preface Numbering (preface)
//Main Idea://Number form 1 to 9 in each digit compose of fixed letters;//For example//6 is VI,60 is CL,and 600 is DC; /*ID: haolink1PROG: prefaceLANG: C++*///#include #include using nam原创 2014-02-23 15:19:13 · 990 阅读 · 0 评论 -
USACO 2.1 Hamming Codes (hamming)
//Main Idea//We use the greedy algorithm and start to check the hamming distance of nummber from//the minimun num.The pity is that I can't prove the correctness of it.I will try it later./*ID: ha原创 2014-02-23 15:17:32 · 1021 阅读 · 0 评论 -
USACO 2.1 Healthy Holsteins (holstein)
//Main Idear//The key point of this problem is "Cows can be fed at most one scoop of any feed type"//Then the max conbination of feeds is 2^15 = 32768,because for each feed type//we can choose to a原创 2014-02-22 14:59:28 · 1092 阅读 · 0 评论