自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

Hello World!

Less is more

  • 博客(100)
  • 收藏
  • 关注

原创 eclipse 配置 C++ 11 -- ubuntu 12.04

安装g++ 4.8sudo apt-get install python-software-propertiessudo add-apt-repository ppa:ubuntu-toolchain-r/testsudo apt-get updatesudo apt-get install g++-4.8sudo update-alternatives --install

2014-07-04 17:16:24 1662

原创 阻塞与非阻塞IO -- 网络编程随想

阻塞和非阻塞IO阻塞IO指当进行IO操作时, 如果IO操作无法立即完成,当前线程进入阻塞状态,直到IO操作完成,IO函数返回。非阻塞IO指当进行IO操作时,如果IO操作无法立即完成,IO函数立即返回,线程不会阻塞。写与读操作对阻塞与非阻塞IO的语义写操作,只有完成所有指定数据的写入时,写操作才算完成。读操作,只要能读取到数据,读操作就算完成。

2014-06-08 17:22:09 1341

原创 由scanf() 和 fgets() 引发的思考

最近在学C的时候, 偶然观察到scanf()的一些“有趣”的表现。我总结了一下,写成此文。     首先是test1()。 一眼看去,程序期望用户输入两次数据。先是 scanf(),后是fgets()。但其实只要输入一次(输入和输出结果在test1()后面)。scanf() 重stdin中读取数据,所以应该是scanf()将换行符留在stdin中了。

2014-06-06 21:52:35 1932 1

原创 USACO 4.1 Fence Loops (fence6)

/* 这题是求无向图中的一个最小环的长度。主要思路是:因为边都是直线,边的两点之间的最短距离必然是这个边长。那么,再求一条到两顶点的最短距径,这个路径与边构成了一个环。这个环是包含该边的最小环。枚举一下所有边,计算出最小环即可。对于每个边,删除该边,然后计算两顶点的最短路径,再恢复该边。 但是这个图的输入是用边表示的,一个难点就是将其转换成用点表示。这里用边的集合来表示一个点

2014-03-02 15:15:10 1640

原创 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 1913

原创 USACO 4.1 Beef McNuggets (nuggets)

/*这题有些难。虽然知道是动态规划题,但是不知道要开多大的数组,后来看analysis用一个256大小的数组循环使用,方法很巧妙。先将box进行排序。如果box里面的数的最大公约数不为1的话,那么所有组成的数,只可能是这个公约数的倍数,因此没有上限,输出为0.用last记录最小的“不能组成的数”。这样当last之后有boxs[0]个连续数都可以组成的话,那么所有的数都可以组成。last+

2014-03-02 15:12:18 1250

原创 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 1688

原创 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 1078

原创 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 953

原创 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 1678

原创 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 1342

原创 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 1018

原创 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 1539

原创 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 1356

原创 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 1292

原创 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 1358

原创 USACO 3.2 Magic Squares (msquare)

/*这道题类似于八数码难题,基本思想是宽搜,使用Hash判重。如果使用一般的八维数组空间可以达到8^8=16777216,会超过USACO的16MB空间限制。所以我们应该对状态进行散列存储,观察发现每位的数字不能重复,存在空间冗余。我们可以对于每个状态建立一个映射,采用康托展开算法。(参见Nocow) 定义cantor(s)为s串大大小顺序。可样将哈希容量缩减到8!=40320。另外发

2014-02-28 13:42:32 1614

原创 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 1392

原创 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 1373

原创 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 1471

原创 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 1129

原创 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 1047

原创 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 4972

原创 USACO 3.1 Shaping Regions (rect1)

/*思路大概是这样的,首先读入所有的矩形。我们可以发现最后覆盖的矩形不会被其他矩形覆盖,所以可以考虑从后向前覆盖。对于每个矩形,我们把它和有可能覆盖在它上面的矩形(就是出现在当前矩形后面的矩形)比较,如果两个矩形有重叠部分就把重叠部分去掉,把当前矩形分成几个小矩形递归进行分割。直到当前矩形与后面的矩形全部没有公共部分,累加矩形的面积。Refer to byvoid;*//*ID:

2014-02-26 13:44:42 1235

原创 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 1985

原创 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 1232

原创 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 1466

原创 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 1493

原创 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 1047

原创 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 1361

原创 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 1355

原创 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 1028

原创 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 1293

原创 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 1193

原创 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 1260

原创 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 995

原创 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 947

原创 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 887

原创 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 883

原创 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 1133

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除