999久久久国产精品,国产精品一区二区久久精品无码,久久香蕉国产线看观看精品ywhttp://www.shnenglu.com/Joe/category/14161.html堅信:勤能補拙zh-cnWed, 22 Sep 2010 22:12:57 GMTWed, 22 Sep 2010 22:12:57 GMT60PKU 2777 Count Colorhttp://www.shnenglu.com/Joe/archive/2010/09/16/126741.htmlsimplyzhaosimplyzhaoThu, 16 Sep 2010 02:57:00 GMThttp://www.shnenglu.com/Joe/archive/2010/09/16/126741.htmlhttp://www.shnenglu.com/Joe/comments/126741.htmlhttp://www.shnenglu.com/Joe/archive/2010/09/16/126741.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/126741.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/126741.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=2777

思路:
線段樹的典型題

參考:
http://blog.csdn.net/xiaofengsheng/archive/2009/03/03/3953265.aspx

代碼:
  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #define MAX_N 100001
  5 #define MAX_T 31
  6 #define LEFT(i) (i<<1)
  7 #define RIGHT(i) ((i<<1)+1)
  8 int L, T, O;
  9 struct Node {
 10     int left, right;
 11     int color;
 12 };
 13 struct Node nodes[MAX_N*3];
 14 int rt, visited[MAX_T];
 15 
 16 void
 17 build(int begin, int end, int step)
 18 {
 19     int mid;
 20     nodes[step].left = begin;
 21     nodes[step].right = end;
 22     nodes[step].color = 1;
 23     if(begin == end)
 24         return;
 25     mid = (begin+end)>>1;
 26     build(begin, mid, LEFT(step));
 27     build(mid+1, end, RIGHT(step));
 28 }
 29 
 30 void
 31 insert(int begin, int end, int color, int step)
 32 {
 33     int mid;
 34     if(nodes[step].left==begin && nodes[step].right==end) {
 35         nodes[step].color = color;
 36         return;
 37     };
 38     if(nodes[step].color != -1) {
 39         nodes[LEFT(step)].color = nodes[RIGHT(step)].color = nodes[step].color;
 40         nodes[step].color = -1/* mixed color */
 41     }
 42     mid = (nodes[step].left+nodes[step].right)>>1;
 43     if(begin > mid) 
 44         insert(begin, end, color, RIGHT(step));
 45     else if(end<=mid)
 46         insert(begin, end, color, LEFT(step));
 47     else {
 48         insert(begin, mid, color, LEFT(step));
 49         insert(mid+1, end, color, RIGHT(step));
 50     }
 51 }
 52 
 53 void
 54 calculate(int begin, int end, int step)
 55 {
 56     if(nodes[step].color != -1) {
 57         if(!visited[nodes[step].color]) {
 58             visited[nodes[step].color] = 1;
 59             ++rt;
 60         }
 61         return;
 62     }
 63     int mid = (nodes[step].left+nodes[step].right)>>1;
 64     if(mid < begin)
 65         calculate(begin, end, RIGHT(step));
 66     else if(mid >= end)
 67         calculate(begin, end, LEFT(step));
 68     else {
 69         calculate(begin, mid, LEFT(step));
 70         calculate(mid+1, end, RIGHT(step));
 71     }
 72 }
 73 
 74 int
 75 main(int argc, char **argv)
 76 {
 77     int i, a, b, c;
 78     char ops[2];
 79     while(scanf("%d %d %d"&L, &T, &O) != EOF) {
 80         build(1, L, 1);
 81         for(i=1; i<=O; i++) {
 82             scanf("%s", ops);
 83             if(ops[0== 'C') {
 84                 scanf("%d %d %d"&a, &b, &c);
 85                 if(a <= b)
 86                     insert(a, b, c, 1);
 87                 else
 88                     insert(b, a, c, 1);
 89             } else if(ops[0== 'P') {
 90                 scanf("%d %d"&a, &b);
 91                 rt = 0;
 92                 memset(visited, 0sizeof(visited));
 93                 if(a <= b)
 94                     calculate(a, b, 1);
 95                 else
 96                     calculate(b, a, 1);
 97                 printf("%d\n", rt);
 98             }
 99         }
100     }
101 }


simplyzhao 2010-09-16 10:57 發表評論
]]>
PKU 1988 Cube Stackinghttp://www.shnenglu.com/Joe/archive/2010/08/09/122779.htmlsimplyzhaosimplyzhaoMon, 09 Aug 2010 06:59:00 GMThttp://www.shnenglu.com/Joe/archive/2010/08/09/122779.htmlhttp://www.shnenglu.com/Joe/comments/122779.htmlhttp://www.shnenglu.com/Joe/archive/2010/08/09/122779.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/122779.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/122779.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=1988

思路:
并查集的妙用
up[i]記錄節點i到根節點的距離(有多少元素)
sum[i]記錄以i作為根節點的樹所包含的節點的個數
重點是在進行union與find操作時如何更新這兩個數組,find操作所暗含路徑壓縮時up數組的更新較難理解

參考:
http://www.shnenglu.com/longzxr/archive/2009/07/13/89974.html

代碼:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_NUM 30005
 5 int father[MAX_NUM], up[MAX_NUM], sum[MAX_NUM];
 6 
 7 void
 8 init()
 9 {
10     int i;
11     for(i=1; i<MAX_NUM; i++) {
12         father[i] = i;
13         sum[i] = 1;
14         up[i] = 0;
15     }
16 }
17 
18 int
19 find(int item)
20 {
21     int tmp = father[item];
22     if(father[item] != item) {
23         father[item] = find(father[item]);
24         up[item] += up[tmp];
25     }
26     return father[item];
27 }
28 
29 void
30 uunion(int top, int down)
31 {
32     int a = find(top);
33     int b = find(down);
34     if(a == b)
35         return;
36     father[b] = a;
37     up[b] = sum[a];
38     sum[a] += sum[b];
39 }
40 
41 int
42 main(int argc, char **argv)
43 {
44     int p, top, down, r, cube;
45     char ch[2];
46     scanf("%d"&p);
47     init();
48     while(p--) {
49         scanf("%s", ch);
50         if(ch[0== 'M') {
51             scanf("%d %d"&top, &down);
52             uunion(top, down);
53         } else {
54             scanf("%d"&cube);
55             r = find(cube);
56             printf("%d\n", sum[r]-up[cube]-1);
57         }
58     }
59 }


simplyzhao 2010-08-09 14:59 發表評論
]]>
PKU 2524 Ubiquitous Religionshttp://www.shnenglu.com/Joe/archive/2010/08/09/122729.htmlsimplyzhaosimplyzhaoMon, 09 Aug 2010 01:11:00 GMThttp://www.shnenglu.com/Joe/archive/2010/08/09/122729.htmlhttp://www.shnenglu.com/Joe/comments/122729.htmlhttp://www.shnenglu.com/Joe/archive/2010/08/09/122729.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/122729.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/122729.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=2524

思路:
簡單并查集,求連通分支的個數,一次AC

代碼:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_NUM 50001
 5 int father[MAX_NUM];
 6 int rank[MAX_NUM];
 7 int total;
 8 
 9 void
10 init(int n)
11 {
12     int i;
13     for(i=1; i<=n; i++)
14         father[i] = i;
15     memset(rank, 0sizeof(rank));
16     total = n;
17 }
18 
19 int 
20 find(int item)
21 {
22     if(father[item] != item)
23         father[item] = find(father[item]);
24     return father[item];
25 }
26 
27 void
28 uunion(int item1, int item2)
29 {
30     int a = find(item1);
31     int b = find(item2);
32     if(a == b)
33         return;
34     --total;
35     if(rank[a] > rank[b])
36         father[b] = a;
37     else {
38         father[a] = b;
39         if(rank[a] == rank[b])
40             ++rank[b];
41     }
42 }
43 
44 int
45 main(int argc, char **argv)
46 {
47     int n, m, i, j, k, cnt=0;
48     while(scanf("%d %d"&n, &m)!=EOF) {
49         if(n==0 && m==0)
50             break;
51         init(n);
52         for(k=0; k<m; k++) {
53             scanf("%d %d"&i, &j);
54             uunion(i, j);
55         }
56         printf("Case %d: %d\n"++cnt, total);
57     }
58 }


simplyzhao 2010-08-09 09:11 發表評論
]]>
PKU 1611 The Suspectshttp://www.shnenglu.com/Joe/archive/2010/08/07/122572.htmlsimplyzhaosimplyzhaoSat, 07 Aug 2010 13:51:00 GMThttp://www.shnenglu.com/Joe/archive/2010/08/07/122572.htmlhttp://www.shnenglu.com/Joe/comments/122572.htmlhttp://www.shnenglu.com/Joe/archive/2010/08/07/122572.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/122572.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/122572.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=1611

思路:
話說是最基礎的并查集,每個分支的根節點賦予該分支節點個數的相反數,妙...

代碼:
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define MAX_LEN 30001
 5 int parent[MAX_LEN];
 6 
 7 void
 8 init(int size)
 9 {
10     int i;
11     for(i=0; i<size; i++)
12         parent[i] = -1;
13 }
14 
15 int
16 find(int item)
17 {
18     int tmp, root = item;
19     while(parent[root] >= 0)
20         root = parent[root];
21     while(item != root) {
22         tmp = parent[item];
23         parent[item] = root;
24         item = tmp;
25     }
26     return root;
27 }
28 
29 void
30 uunion(int item1, int item2)
31 {
32     int root1 = find(item1);
33     int root2 = find(item2);
34     if(root1 != root2) {
35         if(parent[root1] < parent[root2]) { /* tree with 'root1' has more nodes */
36             parent[root1] += parent[root2];
37             parent[root2] = root1;
38         } else {
39             parent[root2] += parent[root1];
40             parent[root1] = root2;
41         }
42     }
43 }
44 
45 int
46 main(int argc, char **argv)
47 {
48     int n, m, gp, i, j, r, stu;
49     while(scanf("%d %d"&n, &m)!=EOF) {
50         if(n==0 && m==0)
51             break;
52         init(n);
53         for(i=0; i<m; i++) {
54             scanf("%d"&gp);
55             for(j=0; j<gp; j++) {
56                 scanf("%d"&stu);
57                 if(j==0)
58                     r = stu;
59                 else
60                     uunion(r, stu);
61             }
62         }
63         printf("%d\n"-parent[find(0)]);
64     }
65 }


simplyzhao 2010-08-07 21:51 發表評論
]]>
PKU 3253 Fence Repair http://www.shnenglu.com/Joe/archive/2010/07/04/119286.htmlsimplyzhaosimplyzhaoSun, 04 Jul 2010 02:39:00 GMThttp://www.shnenglu.com/Joe/archive/2010/07/04/119286.htmlhttp://www.shnenglu.com/Joe/comments/119286.htmlhttp://www.shnenglu.com/Joe/archive/2010/07/04/119286.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/119286.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/119286.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=3253

思路:
哈夫曼樹(詳見CLRS)
這題設計的很巧妙
理解的關鍵是: 將切割木板的過程反過來觀察,也就是將所有最終的短木板進行拼接的過程,這其實就是哈夫曼樹的構造過程
CLRS上關于哈夫曼樹的構造是以優先級隊列為基礎的
由于傾向于用C來寫,所以并沒有使用C++的STL(當然,這要簡單的多)
別忘了: 目的并不是要簡單,而是要鍛煉自己的算法及數據結構基礎

以最小堆為基礎的優先級隊列實現:
 1 #define PARENT(i) ((i-1)>>1)
 2 #define LEFT_CLD(i) ((i<<1)+1)
 3 #define RIGHT_CLD(i) ((i+1)<<1)
 4 #define SWAP(a, b) a=a+b; b=a-b; a=a-b /* tricky */
 5 int heap_size;
 6 
 7 void
 8 min_heapify(long long *heap, int i)
 9 {
10     int min = i;
11     int left = LEFT_CLD(i);
12     int right = RIGHT_CLD(i);
13     if(left<heap_size && heap[min]>heap[left])
14         min = left;
15     if(right<heap_size && heap[min]>heap[right])
16         min = right;
17     if(min != i) {
18         SWAP(heap[i], heap[min]);
19         min_heapify(heap, min);
20     }
21 }
22 
23 void
24 build_min_heap(long long *heap, int length)
25 {
26     int i;
27     heap_size = length;
28     for(i=(heap_size>>1)-1; i>=0; i--)
29         min_heapify(heap, i);
30 }
31 
32 long long
33 extract_min(long long *heap)
34 {
35     if(heap_size < 1) {
36         fprintf(stderr, "heap underflow\n");
37         exit(1);
38     }
39     long long rt = heap[0];
40     SWAP(heap[0], heap[heap_size-1]);
41     --heap_size;
42     min_heapify(heap, 0);
43     return rt;
44 }
45 
46 void
47 decrease_key(long long *heap, int i, long long new_key)
48 {
49     int c, p;
50     if(heap[i] < new_key) {
51         fprintf(stderr, "new key is larger than current key\n");
52         exit(1);
53     }
54     heap[i] = new_key;
55     c = i;
56     p = PARENT(i);
57     while(c>0 && heap[p]>heap[c]) {
58         SWAP(heap[c], heap[p]);
59         c = p;
60         p = PARENT(c);
61     }
62 }
63 
64 void
65 insert(long long *heap, long long value)
66 {
67     ++heap_size;
68     heap[heap_size-1= MAX_INT;
69     decrease_key(heap, heap_size-1, value);
70 }

哈夫曼樹的構造(這里是簡化版,根據題意只需要計算cost即可):
 1 long long
 2 huffman_tree_way(long long *heap, int length)
 3 {
 4     int i;
 5     long long fst, snd, sum, rt=0;
 6     build_min_heap(heap, length);
 7     for(i=0; i<length-1; i++) {
 8         fst = extract_min(heap);
 9         snd = extract_min(heap);
10         sum = fst + snd;
11         rt += sum;
12         insert(heap, sum);
13     }
14     return rt;
15 }






simplyzhao 2010-07-04 10:39 發表評論
]]>
PKU 1028 Web Navigationhttp://www.shnenglu.com/Joe/archive/2010/07/04/119280.htmlsimplyzhaosimplyzhaoSun, 04 Jul 2010 01:45:00 GMThttp://www.shnenglu.com/Joe/archive/2010/07/04/119280.htmlhttp://www.shnenglu.com/Joe/comments/119280.htmlhttp://www.shnenglu.com/Joe/archive/2010/07/04/119280.html#Feedback0http://www.shnenglu.com/Joe/comments/commentRss/119280.htmlhttp://www.shnenglu.com/Joe/services/trackbacks/119280.htmlhttp://acm.pku.edu.cn/JudgeOnline/problem?id=1028

思路:
這是道簡單題,1次AC呵呵
最簡單的做法莫過于直接用兩個堆棧根據題目的description進行模擬
不過,這里,我只用了一個數組來模擬所有的動作
需要注意的一點是: FORWARD的動作只有在之前存在BACK動作的條件下才有可能有效
 1 #define LEN_MAX 71
 2 #define NUM_MAX 100
 3 #define CMD_MAX 8
 4 #define QUIT "QUIT"
 5 #define FORWARD "FORWARD"
 6 #define VISIT "VISIT"
 7 #define BACK "BACK"
 8 #define IGN "Ignored"
 9 char cmd[CMD_MAX];
10 char addr[LEN_MAX];
11 char arr[NUM_MAX][LEN_MAX];
12 int cur_pos, bk_pos, fw_pos;
13 int can_fw_count;
14 
15 int 
16 main(int argc, char **argv)
17 {
18     cur_pos = can_fw_count = 0;
19     bk_pos = fw_pos = -1;
20     strcpy(arr[cur_pos], "http://www.acm.org/");
21     while(scanf("%s", cmd)!=EOF && strcmp(cmd, QUIT)!=0) {
22         if(strcmp(cmd, VISIT)==0) {
23             scanf("%s", addr);
24             strcpy(arr[++cur_pos], addr);
25             bk_pos = cur_pos - 1;
26             can_fw_count = 0;
27             printf("%s\n", arr[cur_pos]);
28         } else if(strcmp(cmd, BACK)==0) {
29             if(bk_pos == -1)
30                 printf("%s\n", IGN);
31             else {
32                 fw_pos = cur_pos;
33                 cur_pos = bk_pos;
34                 bk_pos = cur_pos-1;
35                 ++can_fw_count;
36                 printf("%s\n", arr[cur_pos]);
37             }
38         } else if(strcmp(cmd, FORWARD)==0) {
39             if(fw_pos == -1 || !can_fw_count)
40                 printf("%s\n", IGN);
41             else {
42                 bk_pos = cur_pos;
43                 cur_pos = fw_pos;
44                 fw_pos = cur_pos+1;
45                 --can_fw_count;
46                 printf("%s\n", arr[cur_pos]);
47             }
48         }
49     }
50     return 0;
51 }



simplyzhao 2010-07-04 09:45 發表評論
]]>
亚洲欧美成人综合久久久| 青青草原综合久久大伊人精品| 久久精品国产久精国产| 久久免费视频网站| 区久久AAA片69亚洲| 天天躁日日躁狠狠久久| 激情综合色综合久久综合| 久久精品二区| 综合网日日天干夜夜久久| 亚洲国产成人久久综合一 | 国产一级持黄大片99久久| 久久成人影院精品777| 久久综合精品国产一区二区三区| 久久久久亚洲av成人网人人软件| 久久777国产线看观看精品| 久久天天躁狠狠躁夜夜不卡| 日韩精品国产自在久久现线拍| 无码人妻少妇久久中文字幕蜜桃 | 久久嫩草影院免费看夜色| 综合人妻久久一区二区精品 | 久久国产精品国产自线拍免费 | 久久综合综合久久综合| 色综合久久天天综合| 国产Av激情久久无码天堂| 久久精品成人欧美大片| 国产精品久久久久影院色| 午夜精品久久久久久久| 日本五月天婷久久网站| 久久精品18| 久久久久国产| 精品99久久aaa一级毛片| 精品国产一区二区三区久久| 久久精品国产免费观看三人同眠| 国产精品99久久精品爆乳| 国内精品伊人久久久久AV影院| 久久婷婷色综合一区二区| 亚洲中文字幕久久精品无码喷水| 欧美激情精品久久久久久久| 欧美精品福利视频一区二区三区久久久精品| 久久久无码一区二区三区| 亚洲AV无码1区2区久久|