摘要: 并查集的程序设计:为了解释并查集的原理,我将举一个更有趣的例子。话说江湖上散落着各式各样的大侠,有上千个之多。他们没有什么正当职业,整天背着剑在外面走来走去,碰到和自己不是一路人的,就免不了要打一架。但大侠们有一个优点就是讲义气,绝对不打自己的朋友。而且他们信奉“朋友的朋友就是我的朋友”,只要是能通过朋友关系串联起来的,不管拐了多少个弯,都认为是自己人。这样一来,江湖上就形成了一个一个的群落,通过两两之间的朋友关系串联起来。而不在同一个群落的人,无论如何都无法通过朋友关系连起来,于是就可以放心往死了打。但是两个原本互不相识的人,如何判断是否属于一个朋友圈呢?我们可以在每个朋友圈内推举出一个比较 阅读全文
posted @ 2011-10-26 21:46 迷茫者的旅途 阅读(160) 评论(0) 推荐(0) 编辑
摘要: 题目来源: USACO 1.1原题目:Broken NecklaceYou have a necklace of N red, white, or blue beads (3<=N<=350) some of which are red,others blue, and others white, arranged at random. Here are two examples for n=29: 1 2 1 2 r b b r b r r b r b b b r r b r r r w r b r w w b b r r b b b b b b r b r r b r b r 阅读全文
posted @ 2011-10-26 21:03 迷茫者的旅途 阅读(237) 评论(0) 推荐(0) 编辑
摘要: 题目来源:USACO 1.1原题目:Friday the ThirteenthIs Friday the 13th really an unusual event?That is, does the 13th of the month land on a Friday less often than on any other day ofthe week? To answer this question, write a program that will compute the frequency thatthe 13th of each month lands on Sunday, Mon 阅读全文
posted @ 2011-10-26 17:21 迷茫者的旅途 阅读(271) 评论(0) 推荐(0) 编辑
摘要: 继续数据结构的复习,本次的专题是:并查集。 并查集,顾名思义,干的就是“并”和“查”两件事。很多与集合相关的操作都可以用并查集高效的解决。 两个操作代码: int Find(int x) { if (tree[x].parent != x) { tree[x].parent = Find(tree[x].parent); } return tree[x].parent; } void Merge(int a, int b, int p, int q, int d) { if (tree[q].depth > tree[p].depth) tree[p].parent = q; else 阅读全文
posted @ 2011-10-24 17:33 迷茫者的旅途 阅读(203) 评论(0) 推荐(0) 编辑
摘要: 今天先来讨论一下树状数组.问题提出:已知数组a[],元素个数为n,现在更改a中的元素,要求得新的a数组中i到j区间内的和(1<=i<=j<=n).思考:对于这个问题,我们可以暴力地来解决,从a[i]一直累加到a[j],最坏的情况下复杂度为O(n),对于m次change&querry,合起来的复杂度为O(m*n),在n或m很大的情况下,这样的复杂度是让人无法忍受的.另外,如果没有元素的变更,我们完全可以存储sum[1,k](k=1,2,……),然后对任意给定的查找区间[i,j],都可以方便的用ans=sum[1,j]-sum[1,i-1],当然这只是没有元素改变的情况下 阅读全文
posted @ 2011-10-24 15:55 迷茫者的旅途 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 这篇文章只是想表一下决心,终于在昨天开通了博客园的blog,这里有着许多优秀的程序员,不管我能不能成为其中的一员,我想要做的就是朝着这条路走下去。 暑假搞了一段时间的ACM,接着就荒废了好久,现在慢慢的重新拾起,不管明年是否能拿牌,这对自己的编程水平也会有很大的提高。 先制定条硬性规定吧,每天至少一道USACO..至于各个专题的学习,再重新积累起来。 学术学术,holding on!! 阅读全文
posted @ 2011-10-24 15:41 迷茫者的旅途 阅读(134) 评论(0) 推荐(0) 编辑
摘要: 题目来源:USACO 1.1原题目:Greedy Gift GiversA group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange gifts of money.Each of these friends might or might not give some money to any or all of the other friends.Likewise, each friend might or might not receive money from any or all of the othe 阅读全文
posted @ 2011-10-24 15:10 迷茫者的旅途 阅读(162) 评论(0) 推荐(0) 编辑