上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 20 下一页
摘要: 题目大意:给出若干对朋友关系,每给出一对朋友关系,就输出二者所在关系网中的人数。 首先是对每个人编号,使用map简化编号过程,然后就是使用并查集更新集合。要注意的是当给出A和B的朋友关系时,无论A和B在此之前是否是朋友(属于同一个集合),都要输出建立关系后的朋友网中的人数。 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 #define MAXN 100000*2+100 7 typedef map msi; 8 9 int p_cnt; // the number of people1... 阅读全文
posted @ 2013-09-01 16:56 xiaobaibuhei 阅读(285) 评论(0) 推荐(0)
摘要: 题目大意:给你一个无向图的顶点和边集,让你求图中连通分量的个数。使用并查集解决。 1 #include 2 #include 3 #define MAXN 30 4 5 bool G[MAXN][MAXN]; 6 int p[MAXN]; 7 8 int find(int x) 9 {10 return x == p[x] ? x : p[x]=find(p[x]);11 }12 13 int main()14 {15 #ifdef LOCAL16 freopen("in", "r", stdin);17 #endif18 int T;19 scan.. 阅读全文
posted @ 2013-09-01 16:05 xiaobaibuhei 阅读(437) 评论(0) 推荐(0)
摘要: 题目大意:给出n棵树(有重复),统计每种树出现的频率。使用STL的map。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 typedef map msi; 9 10 int main()11 {12 #ifdef LOCAL13 freopen("in", "r", stdin);14 #endif15 int T;16 scanf("%d", &T);17 getchar();18 stri 阅读全文
posted @ 2013-09-01 12:34 xiaobaibuhei 阅读(554) 评论(0) 推荐(0)
摘要: 题目大意:超市进行促销活动,顾客可以把账单放到一个箱子里,每天超市会从箱子中抽出最高消费者和最低消费者,最高消费者可以得到(最高消费-最低消费)的金钱。询问超市在n天的促销活动结束后应支付多少钱。 找出动态集合的最大值和最小值,可以用multiset进行计算。 1 #include 2 #include 3 using namespace std; 4 5 int main() 6 { 7 #ifdef LOCAL 8 freopen("in", "r", stdin); 9 #endif10 int n;11 multiset urn;12 whi.. 阅读全文
posted @ 2013-09-01 12:27 xiaobaibuhei 阅读(354) 评论(0) 推荐(0)
摘要: 题目大意:给出一些原料和价钱和若干份菜谱,每份菜谱都标明所需的原料和数量,找出所有不超过预算的菜谱。 没什么好说的,主要是对map的运用。 1 #include 2 #include 3 #include 4 #include 5 #include 6 #include 7 using namespace std; 8 typedef map msi; 9 10 struct Recipe11 {12 string name;13 int cost;14 bool operator > T;28 getchar();29 msi ing... 阅读全文
posted @ 2013-08-31 21:41 xiaobaibuhei 阅读(319) 评论(0) 推荐(0)
摘要: 题目大意:给一个中缀表达式,转换成后缀表达式。 这类题一直不太会,让我想就是建一棵表达式树,然后后续遍历算了,可是建树的过程实在太麻烦了。今天才看到有中缀表达式转换成后缀表达式的算法,可以用栈进行实现,我现在才知道...算法如下: 这里假定操作数均为一位数字,操作符只有(、)、+、-、*和/,结果保存到postfix数组里,还要用到一个栈来保存运算符。 从左向右扫描表达式,如果当前字符为: (1)数字,将该数字添加到postfix末尾。 (2)(,将(压入栈中。 (3)),如果当前栈顶元算不是(,将栈中的运算符逐个弹出并追加到postfix末尾,知道栈顶为(,弹出但不追加。 ... 阅读全文
posted @ 2013-08-31 17:41 xiaobaibuhei 阅读(474) 评论(0) 推荐(0)
摘要: 题目大意:给一个有n个数的序列,通过交换相邻的逆序数使这个序列最终有序,求需要交换的次数。 本来可以用冒泡排序解决,但是n达到105,用冒泡排序会超时,用O(nlogn)的归并排序可以达到要求。《算法竞赛入门经典》第八章的“逆序对数”有详细介绍。 1 #include 2 #define MAXN 100000+10 3 4 int a[MAXN], tmp[MAXN]; 5 int cnt; 6 7 void merge_sort(int l, int r) 8 { 9 if (r-l == 1) return;10 int mid = l + (r-l)/2;... 阅读全文
posted @ 2013-08-31 15:02 xiaobaibuhei 阅读(258) 评论(0) 推荐(0)
摘要: 题目大意:给n个数的一个序列,通过交换相邻的两个数使得这n个数按照从小到大的顺序排列。 Inversion index problem: count how many swaps are needed to make the list sorted. 使用冒泡排序解决。 1 #include 2 3 int main() 4 { 5 #ifdef LOCAL 6 freopen("in", "r", stdin); 7 #endif 8 int T; 9 scanf("%d", &T);10 int a[60];11 whil 阅读全文
posted @ 2013-08-31 13:02 xiaobaibuhei 阅读(219) 评论(0) 推荐(0)
摘要: 题目大意:给若干个球队和比赛结果,按照给定的规则给球队排名。 多关键字域排序,题目不难,只是太繁琐,注意在按名字排名时是大小写不敏感的,在这里WA了一次... 1 #include 2 #include 3 #include 4 #include 5 using namespace std; 6 7 struct Team 8 { 9 char name[35], tname[35]; 10 int point, game, win, tie, loss, diff, scored, against; 11 bool operator ... 阅读全文
posted @ 2013-08-30 21:26 xiaobaibuhei 阅读(347) 评论(0) 推荐(0)
摘要: 题目大意:给一个由字母构成的序列,输出按字典序的下一个排列。 用c++ STL的next_permutation可以很容易地解决。 1 #include 2 #include 3 #include 4 using namespace std; 5 6 int main() 7 { 8 #ifdef LOCAL 9 freopen("in", "r", stdin);10 #endif11 char str[60];12 while (gets(str) && str[0] != '#')13 {14 if (next_pe 阅读全文
posted @ 2013-08-30 19:35 xiaobaibuhei 阅读(192) 评论(0) 推荐(0)
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 20 下一页