USACO
文章平均质量分 78
DamonHao
明光村某附属大学
展开
-
USACO 1.3 Barn Repair (barn1)
/*ID: haolink1PROG: barn1LANG: C++*///#include #include #include using namespace std;bool CompareInterval(int free_interval_1, int free_interval_2){ if(free_interval_1 <= free_interval_原创 2014-02-20 13:38:02 · 787 阅读 · 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 · 1732 阅读 · 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 · 1386 阅读 · 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 · 1054 阅读 · 0 评论 -
USACO 1.1 Friday the Thirteenth (friday)
/*ID: haolink1PROG: fridayLANG: C++*///#include #include #include #include using namespace std;int isleap(int year){ return year%4==0 && (year%100 != 0 || year%400 == 0);}/* return l原创 2014-02-17 23:08:41 · 1127 阅读 · 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 · 1402 阅读 · 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 · 1670 阅读 · 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 · 1407 阅读 · 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 · 1348 阅读 · 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 · 1643 阅读 · 0 评论 -
USACO 1.1 Broken Necklace (beads)
/*ID: haolink1PROG: beadsLANG: C++*///#include #include #include using namespace std;int main(){ ifstream fin ("beads.in"); unsigned int num; fin >> num; string beads; fi原创 2014-02-17 23:11:29 · 1086 阅读 · 0 评论 -
USACO 1.1 Greedy Gift Givers (gift1)
/*ID: haolink1PROG: gift1LANG: C++*///#include #include #include #include #include using namespace std;//compute the member position for earnings arrayunsigned short MemberPosition(char**原创 2014-02-15 22:54:31 · 883 阅读 · 0 评论 -
USACO 1.1 Your Ride Is Here (ride)
/*ID: haolink1PROG: rideLANG: C++*///#include #include #include using namespace std;unsigned int StringValue(string name){ unsigned int value=1; for(unsigned int i=0;i < name.length(原创 2014-02-15 22:48:04 · 1123 阅读 · 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 · 1408 阅读 · 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 · 1088 阅读 · 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 · 985 阅读 · 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 · 1733 阅读 · 0 评论 -
USACO 1.3 Calf Flac (calfflac)
/*ID: haolink1PROG: calfflacLANG: C++*///#include #include #include using namespace std;typedef unsigned int u_int;bool IsEqual(char first,char second){ if(first == second || first +原创 2014-02-20 13:39:08 · 1174 阅读 · 0 评论 -
USACO 1.3 Mixing Milk (milk)
/*ID: haolink1PROG: milkLANG: C++*///#include #include #include using namespace std;class Milk{public: int price_; int amount_; Milk(int price, int amount): price_(pric原创 2014-02-19 13:57:18 · 817 阅读 · 0 评论 -
USACO 1.3 Prime Cryptarithm (crypt1)
/*ID: haolink1PROG: crypt1LANG: C++*///#include #include #include using namespace std;bool IsInSet(int digit, int digit_num, int* digits){ for(int i = 0; i < digit_num; i++){ if(原创 2014-02-20 13:40:31 · 796 阅读 · 0 评论 -
USACO 1.4 The Clocks (clocks)
/*ID: haolink1PROG: clocksLANG: C++*///#include #include #include using namespace std;short ModuloAdd(short value){ value=(value+3)%12; if(value == 0) return 12; else re原创 2014-02-20 13:45:31 · 1353 阅读 · 0 评论 -
USACO 1.4 Packing Rectangles (packrec)
/*ID: haolink1PROG: packrecLANG: C++*///#include #include #include // std::vector#include // std::sortusing namespace std;typedef unsigned int u_int;class Rect{public: int h原创 2014-02-20 13:42:55 · 995 阅读 · 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 · 1127 阅读 · 0 评论 -
USACO 1.2 Dual Palindromes (dualpal)
/*ID: haolink1PROG: dualpalLANG: C++*///#include #include #include using namespace std;typedef unsigned int u_int;char num_map[] = {'0','1','2','3','4','5','6','7','8','9'};//Transform de原创 2014-02-19 13:55:06 · 815 阅读 · 0 评论 -
USACO 1.2 Palindromic Squares (palsquare)
/*ID: haolink1PROG: palsquareLANG: C++*///#include #include #include using namespace std;typedef unsigned int u_int;char num_map[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D原创 2014-02-19 13:53:29 · 735 阅读 · 0 评论 -
USACO 1.2 Transformations (transform)
/*ID: haolink1PROG: transformLANG: C++*///#include#include using namespace std;typedef unsigned int u_int;bool Is90(char** first_pattern,char** second_pattern,u_int row_num){ for(u_int i原创 2014-02-19 13:51:19 · 1105 阅读 · 0 评论 -
USACO 1.2 Name That Number (namenum)
/*ID: haolink1PROG: namenumLANG: C++*///#include #include #include #include using namespace std;char map_table[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', 'P','R'原创 2014-02-19 13:49:06 · 1010 阅读 · 0 评论 -
USACO 1.2 Milking Cows (milk2)
/*ID: haolink1PROG: milk2LANG: C++*///#include #include #include #include using namespace std;typedef unsigned int u_int;class TimeInterval{public: int begin_; int end_; TimeI原创 2014-02-19 13:47:26 · 1223 阅读 · 0 评论 -
USACO 4.1 Fence Loops (fence6)
/* 这题是求无向图中的一个最小环的长度。主要思路是:因为边都是直线,边的两点之间的最短距离必然是这个边长。那么,再求一条到两顶点的最短距径,这个路径与边构成了一个环。这个环是包含该边的最小环。枚举一下所有边,计算出最小环即可。对于每个边,删除该边,然后计算两顶点的最短路径,再恢复该边。 但是这个图的输入是用边表示的,一个难点就是将其转换成用点表示。这里用边的集合来表示一个点原创 2014-03-02 15:15:10 · 1678 阅读 · 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 · 2075 阅读 · 0 评论 -
USACO 4.1 Beef McNuggets (nuggets)
/*这题有些难。虽然知道是动态规划题,但是不知道要开多大的数组,后来看analysis用一个256大小的数组循环使用,方法很巧妙。先将box进行排序。如果box里面的数的最大公约数不为1的话,那么所有组成的数,只可能是这个公约数的倍数,因此没有上限,输出为0.用last记录最小的“不能组成的数”。这样当last之后有boxs[0]个连续数都可以组成的话,那么所有的数都可以组成。last+原创 2014-03-02 15:12:18 · 1285 阅读 · 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 · 1438 阅读 · 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 · 1511 阅读 · 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 · 1402 阅读 · 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 · 937 阅读 · 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 · 927 阅读 · 0 评论 -
USACO 2.1 Sorting A Three-Valued Sequence (sort3)
//Main Idear//The problem is quite easy, the key point is to consider all kinds of situation.//We first calculate the number of 1,2,3,namely, counter1,counter2,counter3//So, when the sequence is in原创 2014-02-22 14:57:46 · 955 阅读 · 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 · 968 阅读 · 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 · 1003 阅读 · 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 · 1081 阅读 · 0 评论