|
1 /* Accepted 1160 C++ 00:00.23 836K */ 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 { 8 int N; 9 cin >> N; 10 11 while(N--) 12 { 13 int p, e, i, d, n = 0; 14 while(cin >> p >> e >> i >> d) 15 { 16 if(p == -1 && e == -1 && i == -1 && d == - 1) 17 break; 18 int x = (p * 5544 + i * 1288 + e * 14421) % 21252 - d; 19 while(x <= 0) 20 x += 21252; 21 cout << "Case " << ++n << ": the next triple peak occurs in " 22 << x << " days." << endl; 23 } 24 if(N) 25 cout << endl; 26 } 27 28 return 0; 29 } 30
1 /* Accepted 1195 C++ 00:00.00 844K */ 2 #include <iostream> 3 4 using namespace std; 5 6 int main() 7 { 8 int n, m, fuse, sequence = 0; 9 while((cin >> n >> m >> fuse) && n && m && fuse) 10 { 11 sequence++; 12 cout << "Sequence " << sequence << endl; 13 14 int c[21] = {0}; 15 for(int i = 1; i <= n; i++) 16 cin >> c[i]; 17 18 int cnt = 0, max = 0, t; 19 bool state[21] = {false}, blown = false; 20 for(int i = 0; i < m; i++) 21 { 22 cin >> t; 23 if(blown) 24 continue; 25 26 state[t] ^= 1; 27 max >?= cnt += (state[t] ? c[t] : -c[t]); 28 29 if(cnt > fuse) 30 { 31 blown = true; 32 cout << "Fuse was blown." << endl; 33 } 34 } 35 if(blown == false) 36 cout << "Fuse was not blown." << endl 37 << "Maximal power consumption was " << max << " amperes." << endl; 38 cout << endl; 39 } 40 41 return 0; 42 } 43
1 /* Accepted 1188 C++ 00:00.03 1340K */ 2 #include <string> 3 #include <iostream> 4 5 using namespace std; 6 7 int main() 8 { 9 int N; 10 cin >> N; 11 12 while(N--) 13 { 14 int n, m; 15 cin >> m >> n; 16 17 string s[100]; 18 for(int i = 0; i < n; i++) 19 cin >> s[i]; 20 21 int max = 0; 22 int cnt[1250][101] = {0}; 23 for(int k = 0; k < n; k++) 24 { 25 int sum = 0; 26 for(int i = 0; i < m; i++) 27 for(int j = i + 1; j < m; j++) 28 if(s[k][i] > s[k][j]) 29 sum++; 30 max >?= sum; 31 cnt[sum][0]++; 32 cnt[sum][cnt[sum][0]] = k; 33 } 34 35 for(int i = 0; i <= max; i++) 36 if(cnt[i][0]) 37 for(int j = 1; j <= cnt[i][0]; j++) 38 cout << s[cnt[i][j]] << endl; 39 40 if(N) 41 cout << endl; 42 } 43 44 return 0; 45 } 46
1 /* Accepted 1159 C++ 00:04.20 3584K */ 2 #include <stdio.h> 3 #include <ctype.h> 4 #include <string.h> 5 #include <stdlib.h> 6 7 struct string 8 { 9 int size; 10 char str[10]; 11 12 string() { size = 0; } 13 void clear() { size = 0; } 14 void operator += (const char & c) 15 { 16 str[size] = c; 17 size++; 18 } 19 bool operator == (const string & s) 20 { 21 return strcmp(str, s.str) == 0; 22 } 23 }s[100000]; 24 25 int cmp(const void * a, const void * b) 26 { 27 string * c = (string *) a; 28 string * d = (string *) b; 29 return strcmp(c -> str, d -> str); 30 } 31 32 int main() 33 { 34 int N; 35 scanf("%d", &N); 36 37 char cur[1000], letter2digit[] = "2223334445556667-77888999-"; 38 39 while(N--) 40 { 41 int n; 42 scanf("%d", &n); 43 for(int i = 0; i < n; i++) 44 { 45 s[i].clear(); 46 scanf("%s", cur); 47 for(int j = 0; j < strlen(cur); j++) 48 { 49 if(s[i].size == 3) 50 s[i] += '-'; 51 if(cur[j] == '-') 52 continue; 53 if(isdigit(cur[j])) 54 { 55 s[i] += cur[j]; 56 continue; 57 } 58 s[i] += letter2digit[cur[j] - 'A']; 59 } 60 } 61 62 qsort(s, n, sizeof(string), cmp); 63 64 int i = 0, cnt = 0; 65 while(i < n) 66 { 67 int j = i + 1; 68 while(j < n && s[i] == s[j]) 69 j++; 70 71 if(j - i > 1) 72 { 73 cnt++; 74 printf("%s %d\n", s[i].str, j - i); 75 } 76 77 i = j; 78 } 79 if(cnt == 0) 80 printf("No duplicates.\n"); 81 82 if(N) 83 putchar('\n'); 84 } 85 86 return 0; 87 } 88
I can't stand the problem. TLE many times just because of using cin/cout :(
1 /* Accepted 1168 C++ 00:00.21 472K */ 2 #include <stdio.h> 3 4 int f[21][21][21]; 5 int w(int a, int b, int c) 6 { 7 if(a <= 0 || b <= 0 || c <= 0) 8 return 1; 9 10 if(f[a][b][c]) 11 return f[a][b][c]; 12 13 if(a < b && b < c) 14 return f[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c); 15 return f[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1); 16 } 17 18 int main() 19 { 20 int a, b, c; 21 while(scanf("%d %d %d", &a, &b, &c) != EOF) 22 { 23 if(a == -1 && b == -1 && c == -1) 24 break; 25 26 printf("w(%d, %d, %d) = ", a, b, c); 27 if(a <= 0 || b <= 0 || c <= 0) 28 printf("1\n"); 29 else 30 { 31 if(a > 20 || b > 20 || c > 20) 32 a = 20, b = 20, c = 20; 33 printf("%d\n", w(a, b, c)); 34 } 35 } 36 37 return 0; 38 } 39
1 /* Accepted 1117 C++ 00:00.00 840K */ 2 #include <string> 3 #include <limits.h> 4 #include <iostream> 5 6 using namespace std; 7 8 int cnt[27], len, ans; 9 10 struct 11 { 12 int w; 13 int left, right; 14 bool used; 15 }Tree[100]; 16 17 void InOrder(int p, int n) 18 { 19 if(Tree[p].left) 20 InOrder(Tree[p].left, n + 1); 21 if(Tree[p].right) 22 InOrder(Tree[p].right, n + 1); 23 if(Tree[p].left == 0 && Tree[p].right == 0) 24 ans += Tree[p].w * n; 25 } 26 27 int main() 28 { 29 cout.setf(ios_base::showpoint); 30 cout.setf(ios_base::fixed); 31 cout.precision(1); 32 33 string s; 34 while(cin >> s && s != "END") 35 { 36 memset(cnt, 0, sizeof(cnt)); 37 for(int i = 0; i < s.size(); i++) 38 if(s[i] == '_') 39 cnt[0]++; 40 else 41 cnt[s[i] - 'A' + 1]++; 42 43 len = 0; 44 for(int i = 0; i < 27; i++) 45 if(cnt[i]) 46 { 47 len++; 48 Tree[len].w = cnt[i]; 49 Tree[len].left = Tree[len].right = 0; 50 Tree[len].used = false; 51 } 52 53 while(true) 54 { 55 int m1 = INT_MAX, m2 = INT_MAX, idx1, idx2; 56 57 for(int i = 1; i <= len; i++) 58 if(Tree[i].used == false) 59 if(m1 > Tree[i].w) 60 { 61 m1 = Tree[i].w; 62 idx1 = i; 63 } 64 if(m1 == INT_MAX) break; 65 Tree[idx1].used = true; 66 67 for(int i = 1; i <= len; i++) 68 if(Tree[i].used == false) 69 if(m2 > Tree[i].w) 70 { 71 m2 = Tree[i].w; 72 idx2 = i; 73 } 74 if(m2 == INT_MAX) break; 75 Tree[idx2].used = true; 76 77 len++; 78 Tree[len].w = m1 + m2; 79 Tree[len].left = idx1; 80 Tree[len].right = idx2; 81 Tree[len].used = false; 82 } 83 84 if(len == 1) 85 ans = s.size(); 86 else 87 { 88 ans = 0; 89 InOrder(len, 0); 90 } 91 92 cout << 8 * s.size() << ' ' << ans << ' ' 93 << double(8 * s.size()) / ans << endl; 94 } 95 96 return 0; 97 } 98
1 /* Accepted 1154 C++ 00:00.24 844K */ 2 #include <string> 3 #include <iostream> 4 5 using namespace std; 6 7 int main() 8 { 9 int N; 10 cin >> N; 11 while(N--) 12 { 13 string n; int base; 14 while((cin >> base) && base) 15 { 16 cin >> n; 17 18 int sum = 0, m = 0; 19 for(int i = 0; i < n.size(); i++) 20 { 21 sum += n[i] - '0'; 22 m = m * base + n[i] - '0'; 23 } 24 25 cout << (m % sum == 0 ? "yes" : "no") << endl; 26 } 27 if(N) 28 cout << endl; 29 } 30 31 return 0; 32 } 33
1 /* Accepted 1179 C++ 00:00.00 848K */ 2 #include <string> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 9 struct point { int x, y; char letter; } p[26]; 10 11 int cmp(const void * a, const void * b) 12 { 13 point * c = (point *) a; 14 point * d = (point *) b; 15 if(c -> x == d -> x) 16 return c -> y - d -> y; 17 return c -> x - d -> x; 18 } 19 20 int main() 21 { 22 int n, set = 0; 23 while((cin >> n) && n) 24 { 25 for(int i = 0; i < n; i++) 26 cin >> p[i].letter >> p[i].x >> p[i].y; 27 28 qsort(p, n, sizeof(point), cmp); 29 30 string rec[1000]; int m = 0; 31 for(int i = 0; i < n; i++) 32 for(int j = i + 1; j < n; j++) 33 { 34 int a = -1, b = -1; 35 for(int k = i + 1; k < n; k++) 36 if(p[k].x == p[i].x && p[k].y == p[j].y) 37 { 38 a = k; 39 break; 40 } 41 for(int k = i + 1; k < n; k++) 42 if(p[k].x == p[j].x && p[k].y == p[i].y) 43 { 44 b = k; 45 break; 46 } 47 if(a != -1 && b != -1) 48 { 49 rec[m] += p[a].letter; 50 rec[m] += p[j].letter; 51 rec[m] += p[b].letter; 52 rec[m] += p[i].letter; 53 m++; 54 } 55 } 56 sort(rec, rec + m); 57 58 cout << "Point set " << ++set << ':'; 59 if(m == 0) 60 cout << " No rectangles"; 61 else 62 { 63 cout << endl; 64 for(int i = 0; i < m; i++) 65 { 66 cout << ' ' << rec[i]; 67 if(i + 1 != m && (i + 1) % 10 == 0) 68 cout << endl; 69 } 70 } 71 cout << endl; 72 } 73 74 return 0; 75 } 76
1 /* Accepted 1134 C++ 00:07.71 9716K */ 2 #include <stdio.h> 3 #include <iostream> 4 5 using namespace std; 6 const int N = 1500; 7 8 struct { int cnt, son[1500]; } Tree[1500]; 9 10 int f[1500][2]; 11 void PostOrder(int p) 12 { 13 if(Tree[p].cnt == 0) 14 { 15 f[p][0] = 0; 16 f[p][1] = 1; 17 return; 18 } 19 for(int i = 0; i < Tree[p].cnt; i++) 20 PostOrder(Tree[p].son[i]); 21 22 f[p][1]++; 23 for(int i = 0; i < Tree[p].cnt; i++) 24 { 25 f[p][0] += f[Tree[p].son[i]][1]; 26 f[p][1] += min(f[Tree[p].son[i]][0], f[Tree[p].son[i]][1]); 27 } 28 } 29 30 int main() 31 { 32 int n; 33 while(cin >> n) 34 { 35 memset(Tree, 0, sizeof(Tree)); 36 memset(f, 0, sizeof(f)); 37 38 int s, m = 0; 39 bool x[1500] = {false}; 40 for(int i = 0; i < n; i++) 41 { 42 scanf("%d:(%d)", &s, &m); 43 for(int i = 0; i < m; i++) 44 { 45 Tree[s].cnt = m; 46 cin >> Tree[s].son[i]; 47 x[Tree[s].son[i]] = true; 48 } 49 } 50 int root; 51 for(int i = 0; i < n; i++) 52 if(x[i] == false) 53 { 54 root = i; 55 break; 56 } 57 58 PostOrder(root); 59 60 cout << min(f[root][0], f[root][1]) << endl; 61 } 62 63 return 0; 64 } 65
1 /* Accepted 1197 C++ 00:00.00 836K */ 2 #include <iostream> 3 4 using namespace std; 5 6 struct { int x1, y1, x2, y2, match; } slide[26]; 7 struct { int x, y; bool matched; } p[26]; 8 9 int n; 10 11 bool s1() 12 { 13 for(int i = 0; i < n; i++) 14 if(slide[i].match == -1) 15 { 16 int cnt = 0, idx; 17 for(int j = 0; j < n; j++) 18 if(p[j].matched == false) 19 if(slide[i].x1 <= p[j].x && p[j].x <= slide[i].x2) 20 if(slide[i].y1 <= p[j].y && p[j].y <= slide[i].y2) 21 { 22 cnt++; 23 idx = j; 24 if(cnt > 1) 25 break; 26 } 27 if(cnt == 1) 28 { 29 slide[i].match = idx; 30 p[idx].matched = true; 31 return true; 32 } 33 } 34 return false; 35 } 36 37 bool s2() 38 { 39 for(int i = 0; i < n; i++) 40 if(p[i].matched == false) 41 { 42 int cnt = 0, idx; 43 for(int j = 0; j < n; j++) 44 if(slide[j].match == -1) 45 if(slide[j].x1 <= p[i].x && p[i].x <= slide[j].x2) 46 if(slide[j].y1 <= p[i].y && p[i].y <= slide[j].y2) 47 { 48 cnt++; 49 idx = j; 50 if(cnt > 1) 51 break; 52 } 53 if(cnt == 1) 54 { 55 p[i].matched = true; 56 slide[idx].match = i; 57 return true; 58 } 59 } 60 return false; 61 } 62 63 int main() 64 { 65 int Heap = 0; 66 while((cin >> n) && n) 67 { 68 for(int i = 0; i < n; i++) 69 { 70 slide[i].match = -1; 71 cin >> slide[i].x1 >> slide[i].x2 >> slide[i].y1 >> slide[i].y2; 72 } 73 for(int i = 0; i < n; i++) 74 { 75 p[i].matched = false; 76 cin >> p[i].x >> p[i].y; 77 } 78 79 int m = 0; 80 while(s1())m++; 81 while(s2())m++; 82 83 cout << "Heap " << ++Heap << endl; 84 85 if(m == 0) 86 cout << "none" << endl; 87 else 88 { 89 while(m) 90 for(int i = 0; i < n; i++) 91 if(slide[i].match != -1) 92 { 93 cout << '(' << char(i + 'A') << ',' << slide[i].match + 1 << ')'; 94 cout << (--m == 0 ? '\n' : ' '); 95 } 96 } 97 cout << endl; 98 } 99 100 return 0; 101 } 102
|