2012年8月9日

hdoj 1232 畅通工程

摘要: 裸的并查集,把每个集合看成一个点,然后点的总和减1就是道路条数,比如两个点之间连通最少需要1条路; 1 #include <stdio.h> 2 3 int set[1001]; 4 5 int find(int n)//寻找祖先,类似双亲树 6 { 7 while (set[n] != n)//o(logN) 8 n = set[n]; 9 return n;10 }11 12 void merge(int a, int b)//合并集合13 {14 b = find(b);15 set[b] = a;//用a的值覆盖b祖先的值,a的祖先就... 阅读全文

posted @ 2012-08-09 19:30 Xor<>OR 阅读(125) 评论(0) 推荐(0)

括号匹配(栈做法)

摘要: 1 #include <stdio.h> 2 #include <string.h> 3 4 char s[10002]; //栈 5 6 int main() 7 { 8 int n; 9 scanf("%d", &n);10 getchar();11 memset(s, 0, sizeof(s));12 while (n--)13 {14 int top = 0;//栈标始终指向栈顶元素15 char c;16 while ((c = getchar()) != '\n' )17 {18 if ((c == ')& 阅读全文

posted @ 2012-08-09 14:03 Xor<>OR 阅读(160) 评论(0) 推荐(0)

hdoj 2602 (01背包问题)

摘要: 裸的背包 1 #include <stdio.h> 2 #include <string.h> 3 4 struct t 5 { 6 int w; 7 int v; 8 }; 9 10 int r[1002];11 t a[1002];12 13 int main()14 {15 int n;16 scanf("%d", &n);17 while (n--)18 {19 memset(r, 0, sizeof(r)); //数组清零不能忘记!20 int N, V, i, j;21 scanf("... 阅读全文

posted @ 2012-08-09 10:10 Xor<>OR 阅读(146) 评论(0) 推荐(0)

嵌套矩形

摘要: 其实就是最大子序列的变形,首先排序; 1 #include <stdio.h> 2 #include <algorithm> 3 4 using namespace std; 5 6 struct re 7 { 8 int a;//长 9 int b;//宽10 int c;//计数器11 };12 13 re r[1001];14 15 bool cmp(re t1, re t2)16 {17 return t1.a < t2.a;18 }19 20 int main()21 {22 int n;23 scanf("%d", &n); 阅读全文

posted @ 2012-08-09 09:45 Xor<>OR 阅读(308) 评论(0) 推荐(0)

导航