锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
2 #include <iostream>
3
4 using namespace std;
5
6 int n;
7 int s[6000];
8 int p[6000];
9
10 int f[6000][2];
11
12 void search(int i)
13 {
14 int SonCnt = 0;
15 for(int k = 0; k < n; k++)
16 if(p[k] == i)
17 {
18 search(k);
19 SonCnt++;
20 }
21
22 if(SonCnt == 0)
23 {
24 f[i][0] = 0; f[i][1] = s[i];
25 return;
26 }
27
28 f[i][1] = s[i];
29 for(int k = 0; k < n; k++)
30 if(p[k] == i)
31 {
32 f[i][0] += max(f[k][0], f[k][1]);
33 f[i][1] += f[k][0];
34 }
35 }
36
37 int main()
38 {
39 memset(p, 255, sizeof(p));
40
41 scanf("%d", &n);
42 for(int i = 0; i < n; i++)
43 scanf("%d", s + i);
44 while(true)
45 {
46 int s, t;
47 scanf("%d %d", &s, &t);
48 if(s == 0 && t == 0)
49 break;
50 p[s - 1] = t - 1;
51 }
52
53 int root;
54 for(int i = 0; i < n; i++)
55 if(p[i] == -1)
56 {
57 root = i; break;
58 }
59
60 search(root);
61
62 cout << max(f[root][0], f[root][1]) << endl;
63
64 return 0;
65 }
66
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 const int maxn = 30000;
7
8 template <class T>
9 class Heap
10 {
11 private:
12 T A[maxn + 1]; int len;
13 inline int Parent(int i) { return i / 2; }
14 inline int Lchild(int i) { return i * 2; }
15 inline int Rchild(int i) { return i * 2 + 1; }
16
17 public:
18 Heap(const T * x, const int & n)
19 {
20 len = n;
21 for(int i = 1; i <= n; i++)
22 A[i] = x[i];
23 for(int i = n / 2; i >= 1; i--)
24 modify(i);
25 }
26 Heap(int s, int t)
27 {
28 len = t - s + 1;
29 for(int i = 1; i <= len; i++)
30 A[i] = s + i - 1;
31 }
32 Heap() { len = 0; }
33 void modify(int i)
34 {
35 int min = i;
36 int l = Lchild(i);
37 int r = Rchild(i);
38 if(l <= len && A[l] < A[min]) min = l;
39 if(r <= len && A[r] < A[min]) min = r;
40 if(i != min)
41 {
42 swap(A[i], A[min]);
43 modify(min);
44 }
45 }
46 bool empty() { return len == 0; }
47 T & getmin() { return A[1]; }
48 void push(const T & item)
49 {
50 A[len + 1] = item;
51 int i = len + 1;
52 while(i > 1 && A[i] < A[Parent(i)])
53 {
54 swap(A[i], A[Parent(i)]);
55 i = Parent(i);
56 }
57 len++;
58 }
59 void pop()
60 {
61 swap(A[1], A[len]);
62 len--;
63 modify(1);
64 }
65 bool update(int, int);
66 } ;
67
68 template <class T>
69 bool Heap<T>::update(int num, int latest)
70 {
71 for(int i = 1; i <= len; i++)
72 if(A[i].num == num)
73 {
74 A[i].latest = latest;
75 modify(i);
76
77 return true;
78 }
79 return false;
80 }
81
82 struct rec
83 {
84 int num, latest;
85
86 bool operator < (const rec & x) const
87 {
88 return latest < x.latest;
89 }
90 } ;
91
92 //=============================================
93 int currentTime, accessNum;
94 Heap <int> X(1, 30000);
95 Heap <rec> Y;
96
97 void allocate()
98 {
99 while(Y.empty() == false && currentTime - Y.getmin().latest >= 600)
100 {
101 X.push(Y.getmin().num);
102 Y.pop();
103 }
104
105 cout << X.getmin() << endl;
106 rec r = { X.getmin(), currentTime };
107 Y.push(r);
108 X.pop();
109 }
110
111 void access()
112 {
113 while(Y.empty() == false && currentTime - Y.getmin().latest >= 600)
114 {
115 X.push(Y.getmin().num);
116 Y.pop();
117 }
118
119 cout << (Y.update(accessNum, currentTime) ? '+' : '-') << endl;
120 }
121
122 int main()
123 {
124 char c;
125 while(true)
126 {
127 if(scanf("%d %c", ¤tTime, &c) == EOF)
128 break;
129
130 if(c == '+')
131 allocate();
132 else
133 {
134 scanf("%d", &accessNum);
135 access();
136 }
137 }
138
139 return 0;
140 }
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 int n, m, map[101][101], opt[101][101];
7
8 struct BinaryTree
9 {
10 int num, apple;
11 BinaryTree * left, * right;
12
13 BinaryTree()
14 {
15 left = right = NULL;
16 }
17 void PostOrder()
18 {
19 if(left == NULL && right == NULL)
20 {
21 opt[num][1] = apple;
22 return;
23 }
24 if(left)
25 left -> PostOrder();
26 if(right)
27 right -> PostOrder();
28
29 for(int i = 1; i <= m; i++)
30 {
31 int max = 0;
32 for(int j = 0; j < i; j++)
33 if(max < opt[left -> num][j] + opt[right -> num][i - j - 1])
34 max = opt[left -> num][j] + opt[right -> num][i - j - 1];
35 opt[num][i] = max + apple;
36 }
37 }
38 }Tree[101];
39
40 bool visited[101];
41 void dfs(int p)
42 {
43 visited[p] = true;
44 for(int i = 1; i <= n; i++)
45 if(map[p][i] && visited[i] == false)
46 {
47 Tree[i].num = i;
48 Tree[i].apple = map[p][i];
49
50 if(Tree[p].left == NULL)
51 Tree[p].left = &Tree[i];
52 else
53 Tree[p].right = &Tree[i];
54 dfs(i);
55 }
56 }
57
58 int main()
59 {
60 cin >> n >> m; m++;
61
62 int s, t, l;
63 while(cin >> s >> t >> l)
64 map[s][t] = map[t][s] = l;
65
66 dfs(1);
67
68 Tree[1].num = 1;
69 Tree[1].apple = 0;
70 Tree[1].PostOrder();
71
72 cout << opt[1][m] << endl;
73
74 return 0;
75 }
76
]]>
2 #include <queue>
3 #include <iostream>
4
5 using namespace std;
6
7 struct rec { int n, m; };
8
9 int main()
10 {
11 int n = 0; char c;
12 for(int i = 0; i < 16; i++)
13 {
14 cin.get(c);
15 if(c == '\n')
16 {
17 i--;
18 continue;
19 }
20 if(c == 'b')
21 n |= (1 << i);
22 }
23
24 rec cur = {n, 0};
25 queue <rec> q;
26 q.push(cur);
27
28 bool repeat[65536] = {false}, find = false;
29 while(q.empty() == false)
30 {
31 cur = q.front(); q.pop();
32
33 if(cur.n == 0 || cur.n == 65535)
34 {
35 cout << cur.m << endl;
36 find = true;
37 break;
38 }
39
40 for(int i = 0; i < 16; i++)
41 {
42 int t = cur.n;
43
44 t ^= (1 << i);
45 if(i - 4 >= 0)
46 t ^= (1 << (i - 4));
47 if(i + 4 < 16)
48 t ^= (1 << (i + 4));
49 if(i % 4 - 1 >= 0)
50 t ^= (1 << (i - 1));
51 if(i % 4 + 1 < 4)
52 t ^= (1 << (i + 1));
53
54 if(repeat[t] == false)
55 {
56 repeat[t] = true;
57 rec tmp = {t, cur.m + 1};
58 q.push(tmp);
59 }
60 }
61 }
62
63 if(find == false)
64 cout << "Impossible" << endl;
65
66 return 0;
67 }
68
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 int main()
7 {
8 struct { int x, y; } p[200];
9
10 int n;
11 cin >> n;
12 for(int i = 0; i < n; i++)
13 cin >> p[i].x >> p[i].y;
14
15 int max = 0;
16 for(int i = 0; i < n; i++)
17 for(int j = 0; j < n; j++)
18 if(i != j)
19 {
20 int x = p[j].x - p[i].x;
21 int y = p[j].y - p[i].y;
22
23 int cnt = 2;
24 for(int k = 0; k < n; k++)
25 if(k != i && k != j)
26 if(x * (p[k].y - p[i].y) == y * (p[k].x - p[i].x))
27 cnt++;
28 if(max < cnt)
29 max = cnt;
30 }
31
32 cout << max << endl;
33
34 return 0;
35 }
36
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 int n, m, match[1000];
7 bool map[1000][1000], visited[1000];
8
9 bool dfs(int p)
10 {
11 for(int i = 0; i < m; i++)
12 if(map[p][i] && visited[i] == false)
13 {
14 visited[i] = true;
15 if(match[i] == -1 || dfs(match[i]))
16 {
17 match[i] = p;
18 return true;
19 }
20 }
21 return false;
22 }
23
24 int main()
25 {
26 int s, t;
27 cin >> n >> m >> s;
28 while(cin >> s >> t)
29 {
30 s--, t--;
31 map[s][t] = true;
32 }
33
34 int cnt = 0;
35 memset(match, 0XFF, sizeof(match));
36 for(int i = 0; i < n; i++)
37 {
38 memset(visited, false, sizeof(visited));
39 cnt += dfs(i);
40 }
41
42 cout << n + m - 2 * cnt + cnt << endl;
43
44 return 0;
45 }
46
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 int main()
7 {
8 int n, k;
9 unsigned long long cnt[20][2] = {0};
10
11 cin >> n >> k;
12 cnt[1][1] = k - 1;
13 for(int i = 2; i <= n; i++)
14 {
15 cnt[i][0] = cnt[i - 1][1];
16 cnt[i][1] = (cnt[i - 1][0] + cnt[i - 1][1]) * (k - 1);
17 }
18 cout << cnt[n][0] + cnt[n][1] << endl;
19
20 return 0;
21 }
22
]]>
2 #include <string>
3 #include <iostream>
4
5 using namespace std;
6
7 int main()
8 {
9 int n;
10 cin >> n;
11 string word;
12 while(cin >> word)
13 {
14 if(word.size() == n - 1)
15 for(int i = 0; i < n; i++)
16 {
17 int sum = 0;
18 for(int j = 0;j < i; j++)
19 sum += (word[j] == '1' ? j + 1: 0);
20 for(int j = i; j < n - 1; j++)
21 sum += (word[j] == '1' ? j + 2: 0);
22 if(sum == 0 || sum % (n + 1) == 0 || (sum + i + 1) % (n + 1) == 0)
23 {
24 for(int j = 0;j < i; j++)
25 cout << word[j];
26 cout << (sum % (n + 1) == 0 ? '0' : '1');
27 for(int j = i; j < n - 1; j++)
28 cout << word[j];
29 cout << endl;
30 break;
31 }
32 }
33 if(word.size() == n)
34 {
35 for(int i = 0; i < n; i++)
36 if(word[i] == '1')
37 {
38 int sum = 0;
39 word[i] = '0';
40 for(int j = 0; j < n; j++)
41 sum += (word[j] == '1' ? j + 1 : 0);
42 if(sum == 0 || sum % (n + 1) == 0)
43 break;
44 word[i] = '1';
45 }
46 cout << word << endl;
47 }
48 if(word.size() == n + 1)
49 for(int i = 0; i < n + 1; i++)
50 {
51 int sum = 0;
52 for(int j = 0; j < i; j++)
53 sum += (word[j] == '1' ? j + 1 : 0);
54 for(int j = i + 1; j < n + 1; j++)
55 sum += (word[j] == '1' ? j : 0);
56 if(sum == 0 || sum % (n + 1) == 0)
57 {
58 for(int j = 0; j < i; j++)
59 cout << word[j];
60 for(int j = i + 1; j < n + 1; j++)
61 cout << word[j];
62 cout << endl;
63 break;
64 }
65 }
66 }
67
68 return 0;
69 }
70
]]>
2 #include <queue>
3 #include <cctype>
4 #include <string>
5 #include <iostream>
6
7 using namespace std;
8
9 struct pixel { int x, y; };
10
11 int main()
12 {
13 int n, x, y;
14 bool map[12][12] = {false};
15
16 cin >> n;
17 if(cin.get() == '\n')
18 {
19 int sx, sy;
20 cin >> sx >> sy;
21 cout << sx << ' ' << sy << endl;
22
23 map[sx][sy] = true;
24 while(cin >> x >> y)
25 map[x][y] = true;
26
27 pixel cur = {sx, sy};
28 queue <pixel> q;
29 q.push(cur);
30
31 while(q.empty() == false)
32 {
33 n--;
34 cur = q.front(); q.pop();
35 map[cur.x][cur.y] = false;
36 if(map[cur.x + 1][cur.y])
37 {
38 cout << 'R';
39 map[cur.x + 1][cur.y] = false;
40 pixel tmp = {cur.x + 1, cur.y};
41 q.push(tmp);
42 }
43 if(map[cur.x][cur.y + 1])
44 {
45 cout << 'T';
46 map[cur.x][cur.y + 1] = false;
47 pixel tmp = {cur.x, cur.y + 1};
48 q.push(tmp);
49 }
50 if(map[cur.x - 1][cur.y])
51 {
52 cout << 'L';
53 map[cur.x - 1][cur.y] = false;
54 pixel tmp = {cur.x - 1, cur.y};
55 q.push(tmp);
56 }
57 if(map[cur.x][cur.y - 1])
58 {
59 cout << 'B';
60 map[cur.x][cur.y - 1] = false;
61 pixel tmp = {cur.x, cur.y - 1};
62 q.push(tmp);
63 }
64 cout << (n ? ',' : '.') << endl;
65 }
66 }
67 else
68 {
69 int x = n, y, cnt = 0; cin >> y;
70 pixel cur = {x, y};
71
72 queue <pixel> q;
73 q.push(cur);
74
75 string s;
76 while(q.empty() == false)
77 {
78 cnt++;
79 cin >> s;
80 cur = q.front(); q.pop();
81 map[cur.x][cur.y] = true;
82 for(int i = 0; isalpha(s[i]); i++)
83 {
84 if(s[i] == 'R')
85 {
86 pixel tmp = {cur.x + 1, cur.y};
87 q.push(tmp);
88 }
89 if(s[i] == 'T')
90 {
91 pixel tmp = {cur.x, cur.y + 1};
92 q.push(tmp);
93 }
94 if(s[i] == 'L')
95 {
96 pixel tmp = {cur.x - 1, cur.y};
97 q.push(tmp);
98 }
99 if(s[i] == 'B')
100 {
101 pixel tmp = {cur.x, cur.y - 1};
102 q.push(tmp);
103 }
104 }
105 }
106 cout << cnt << endl;
107 for(int i = 1; i <= 10; i++)
108 for(int j = 1; j <= 10; j++)
109 if(map[i][j])
110 cout << i << ' ' << j << endl;
111 }
112
113 return 0;
114 }
115
]]>
2 #include <iostream>
3
4 using namespace std;
5
6 int n, w[20], cnt, best = 0X7FFFFFFF;
7
8 void search(int i, int sum)
9 {
10 if(i == n)
11 return;
12 if(best > abs(cnt - 2 * sum))
13 best = abs(cnt - 2 * sum);
14 search(i + 1, sum);
15 search(i + 1, sum + w[i]);
16 }
17
18 int main()
19 {
20 cin >> n;
21 for(int i = 0; i < n; i++)
22 {
23 cin >> w[i];
24 cnt += w[i];
25 }
26 search(0, 0);
27 cout << best << endl;
28
29 return 0;
30 }
31
]]>
2 #include <string>
3 #include <iostream>
4
5 using namespace std;
6
7 void str2digit(string & s, string & digit)
8 {
9 for(int i = 0; i < s.size(); i++)
10 {
11 if(s[i] == 'i' || s[i] == 'j') {
12 digit += '1'; continue;
13 }
14 if(s[i] == 'a' || s[i] == 'b' || s[i] == 'c') {
15 digit += '2'; continue;
16 }
17 if(s[i] == 'd' || s[i] == 'e' || s[i] == 'f') {
18 digit += '3'; continue;
19 }
20 if(s[i] == 'g' || s[i] == 'h') {
21 digit += '4'; continue;
22 }
23 if(s[i] == 'k' || s[i] == 'l') {
24 digit += '5'; continue;
25 }
26 if(s[i] == 'm' || s[i] == 'n') {
27 digit += '6'; continue;
28 }
29 if(s[i] == 'p' || s[i] == 'r' || s[i] == 's') {
30 digit += '7'; continue;
31 }
32 if(s[i] == 't' || s[i] == 'u' || s[i] == 'v') {
33 digit += '8'; continue;
34 }
35 if(s[i] == 'w' || s[i] == 'x' || s[i] == 'y') {
36 digit += '9'; continue;
37 }
38 if(s[i] == 'o' || s[i] == 'q' || s[i] == 'z') {
39 digit += '0'; continue;
40 }
41 }
42 }
43
44 int main()
45 {
46 string num;
47 while((cin >> num) && num != "-1")
48 {
49 int n;
50 cin >> n;
51
52 string * dict = new string[n];
53 string * digit = new string[n];
54
55 for(int i = 0; i < n; i++)
56 {
57 cin >> dict[i];
58 str2digit(dict[i], digit[i]);
59 }
60
61 unsigned opt[101], path[101] = {0}, choice[101] = {0};
62 memset(opt, 0XFF, sizeof(opt));
63
64 opt[0] = 0;
65 for(int i = 0; i < num.size(); i++)
66 {
67 if(opt[i] == 0XFFFFFFFF)
68 continue;
69 for(int j = 0; j < n; j++)
70 if(num.find(digit[j], i) == i)
71 if(opt[i] + 1 < opt[i + digit[j].size()])
72 {
73 opt[i + digit[j].size()] = opt[i] + 1;
74 path[i + digit[j].size()] = i;
75 choice[i + digit[j].size()] = j;
76 }
77 }
78
79 if(opt[num.size()] == 0XFFFFFFFF)
80 cout << "No solution." << endl;
81 else
82 {
83 int x[101], m = 0, pos = num.size();
84 while(pos)
85 {
86 x[m++] = choice[pos];
87 pos = path[pos];
88 }
89 for(int i = m - 1; i >= 0; i--)
90 cout << dict[x[i]] << (i == 0 ? '\n' : ' ');
91 }
92
93 delete []dict;
94 delete []digit;
95 }
96
97 return 0;
98 }
99
]]>
2 #include <stack>
3 #include <math.h>
4 #include <iostream>
5
6 using namespace std;
7
8 int main()
9 {
10 double n;
11 stack <double> set;
12 while(cin >> n)
13 set.push(n);
14
15 cout.setf(ios_base::showpoint);
16 cout.setf(ios_base::fixed);
17 cout.precision(4);
18
19 while(set.empty() == false)
20 {
21 cout << sqrt(set.top()) << endl;
22 set.pop();
23 }
24
25 return 0;
26 }
27
]]>