|
1 #include <iostream> 2 3 using namespace std; 4 5 class Square 6 { 7 private: 8 char x[10][10]; 9 10 public: 11 Square rotate(); 12 Square reflect(); 13 bool operator == (const Square &); 14 friend istream & operator >> (istream &, Square &); 15 friend ostream & operator << (ostream &, const Square &); 16 } ; 17 18 int n; 19 Square s, t; 20 21 int main() 22 { 23 freopen("transform.in", "r", stdin); 24 freopen("transform.out", "w", stdout); 25 26 cin >> n; 27 cin >> s >> t; 28 29 if (s.rotate() == t) { cout << 1 << endl; return 0; } 30 if (s.rotate().rotate() == t) { cout << 2 << endl; return 0; } 31 if (s.rotate().rotate().rotate() == t) { cout << 3 << endl; return 0; } 32 if (s.reflect() == t) { cout << 4 << endl; return 0; } 33 if (s.reflect().rotate() == t) { cout << 5 << endl; return 0; } 34 if (s.reflect().rotate().rotate() == t) { cout << 5 << endl; return 0; } 35 if (s.reflect().rotate().rotate().rotate() == t) { cout << 5 << endl; return 0; } 36 if (s == t) { cout << 6 << endl; return 0; } 37 cout << 7 << endl; return 0; 38 } 39 40 Square Square::rotate() 41 { 42 Square ns; //new square 43 for (int i = 0; i < n; i++) 44 for (int j = 0; j < n; j++) 45 ns.x[j][n - i - 1] = x[i][j]; 46 return ns; 47 } 48 49 Square Square::reflect() 50 { 51 Square ns; 52 for (int i = 0; i < n; i++) 53 for (int j = 0; j < n; j++) 54 ns.x[i][n - j - 1] = x[i][j]; 55 return ns; 56 } 57 58 bool Square::operator == (const Square & s) 59 { 60 for (int i = 0; i < n; i++) 61 for (int j = 0; j < n; j++) 62 if (x[i][j] != s.x[i][j]) 63 return false; 64 return true; 65 } 66 67 istream & operator >> (istream & is, Square & s) 68 { 69 for (int i = 0; i < n; i++) 70 for (int j = 0; j < n; j++) 71 cin >> s.x[i][j]; 72 return cin; 73 } 74 75 ostream & operator << (ostream & is, const Square & s) 76 { 77 for (int i = 0; i < n; i++) 78 { 79 for (int j = 0; j < n; j++) 80 cout << s.x[i][j]; 81 cout << endl; 82 } 83 return cout; 84 } 85
1 #include <iostream> 2 3 using namespace std; 4 5 struct Interval 6 { 7 int s, t; 8 9 bool operator < (const Interval & i) const 10 { 11 return s < i.s; 12 } 13 } interval[5000]; 14 15 int main() 16 { 17 freopen("milk2.in", "r", stdin); 18 freopen("milk2.out", "w", stdout); 19 20 int n; 21 22 cin >> n; 23 for (int i = 0; i < n; i++) 24 cin >> interval[i].s >> interval[i].t; 25 26 sort(interval, interval + n); 27 28 int p = 0; 29 30 bool toBeDeleted[5000] = { false }; 31 for (int i = 0; i < n - 1; i++) 32 for (int j = i + 1; interval[i].t > interval[j].t && j < n; j++) 33 toBeDeleted[j] = true; 34 for (int i = 0; i < n; i++) 35 if (toBeDeleted[i] == false) 36 { 37 interval[p].s = interval[i].s; 38 interval[p].t = interval[i].t; 39 p++; 40 } 41 42 n = p; 43 44 bool toBeUnited[5000] = { false }; 45 for (int i = 1; i < n; i++) 46 if (interval[i].s <= interval[i - 1].t) 47 toBeUnited[i] = true; 48 49 p = 0; 50 for (int i = 0; i < n; ) 51 { 52 int j = i; 53 while (toBeUnited[j + 1] == true) 54 j++; 55 if (i == j) 56 { 57 interval[p].s = interval[i].s; 58 interval[p].t = interval[i].t; 59 p++; 60 } 61 else 62 { 63 interval[p].s = interval[i].s; 64 interval[p].t = interval[j].t; 65 p++; 66 } 67 i = j + 1; 68 } 69 70 int longestContinuousTime = 0, longestIdleTime = 0; 71 for (int i = 0; i < p; i++) 72 longestContinuousTime >?= interval[i].t - interval[i].s; 73 for (int i = 0; i < p - 1; i++) 74 longestIdleTime >?= interval[i + 1].s - interval[i].t; 75 76 cout << longestContinuousTime << ' ' << longestIdleTime << endl; 77 78 return 0; 79 } 80
code 1
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 freopen("beads.in", "r", stdin); 8 freopen("beads.out", "w", stdout); 9 10 int n; 11 string s; 12 13 cin >> n; 14 cin >> s; 15 16 for (int i = 0; i < n - 1; i++) 17 s += s[i]; 18 19 int rec[1000] = { 0 }; 20 21 for (int i = 0; i < 2 * n - 1; i++) 22 { 23 int p = i + 1; char c = s[i]; 24 while (true) 25 { 26 if (p == 2 * n - 1) 27 break; 28 if (c == 'w') 29 { 30 if (s[p] != 'w') 31 c = s[p]; 32 } 33 else 34 { 35 if (s[p] != c && s[p] != 'w') 36 break; 37 } 38 p++; 39 } 40 rec[i] = p - i; 41 } 42 43 int ans = 0; 44 for (int i = 0; i < 2 * n - 2; i++) 45 ans >?= (rec[i] + rec[i + rec[i]]); 46 47 cout << (ans > n ? n : ans) << endl; 48 49 return 0; 50 } 51
code2
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 freopen("beads.in", "r", stdin); 8 freopen("beads.out", "w", stdout); 9 10 int n; 11 string s; 12 13 cin >> n; 14 cin >> s; 15 16 for (int i = 0; i < n - 1; i++) 17 s += s[i]; 18 19 int leftRed[1000] = { 0 }, rightRed[1000] = { 0 }; 20 int leftBlue[1000] = { 0 }, rightBlue[1000] = { 0 }; 21 22 if (s[0] == 'r') leftRed[0] = 1; 23 if (s[0] == 'b') leftBlue[0] = 1; 24 if (s[0] == 'w') leftRed[0] = leftBlue[0] = 1; 25 if (s[2 * n - 2] == 'r') rightRed[2 * n - 2] = 1; 26 if (s[2 * n - 2] == 'b') rightBlue[2 * n - 2] = 1; 27 if (s[2 * n - 2] == 'w') rightRed[2 * n - 2] = rightBlue[2 * n - 2] = 1; 28 29 for (int i = 1; i < 2 * n - 1; i++) 30 { 31 if (s[i] == 'r') 32 { 33 leftRed[i] = leftRed[i - 1] + 1; 34 leftBlue[i] = 0; 35 } 36 if (s[i] == 'b') 37 { 38 leftRed[i] = 0; 39 leftBlue[i] = leftBlue[i - 1] + 1; 40 } 41 if (s[i] == 'w') 42 { 43 leftRed[i] = leftRed[i - 1] + 1; 44 leftBlue[i] = leftBlue[i - 1] + 1; 45 } 46 } 47 48 for (int i = 2 * n - 3; i >= 0; i--) 49 { 50 if (s[i] == 'r') 51 { 52 rightRed[i] = rightRed[i + 1] + 1; 53 rightBlue[i] = 0; 54 } 55 if (s[i] == 'b') 56 { 57 rightRed[i] = 0; 58 rightBlue[i] = rightBlue[i + 1] + 1; 59 } 60 if (s[i] == 'w') 61 { 62 rightRed[i] = rightRed[i + 1] + 1; 63 rightBlue[i] = rightBlue[i + 1] + 1; 64 } 65 } 66 67 int ans = 0; 68 for (int i = 0; i < 2 * n - 2; i++) 69 { 70 int l = max(leftRed[i], leftBlue[i]); 71 int r = max(rightRed[i + 1], rightBlue[i + 1]); 72 ans >?= (l + r); 73 } 74 75 cout << (ans > n ? n : ans) << endl; 76 77 return 0; 78 } 79
1 #include <iostream> 2 3 using namespace std; 4 5 bool isLeapYear(int y) 6 { 7 return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0; 8 } 9 10 int theAmountOfDaysInTheMonth(int y, int m) 11 { 12 switch (m) 13 { 14 case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : 15 return 31; 16 case 4 : case 6 : case 9 : case 11 : 17 return 30; 18 case 2 : 19 return isLeapYear(y) ? 29 : 28; 20 } 21 } 22 23 int main() 24 { 25 freopen("friday.in", "r", stdin); 26 freopen("friday.out", "w", stdout); 27 28 int n; 29 30 cin >> n; 31 32 int y = 1900, m = 1, d = 1, dayOfWeek = 0, cnt[7] = { 0 }; 33 for (y = 1900; y <= 1900 + n - 1; y++) 34 { 35 for (m = 1; m <= 12; m++) 36 for (d = 1; d <= theAmountOfDaysInTheMonth(y, m); d++) 37 { 38 if (d == 13) 39 cnt[dayOfWeek]++; 40 dayOfWeek = (dayOfWeek + 1) % 7; 41 } 42 } 43 44 cout << cnt[5] << ' ' << cnt[6] << ' ' 45 << cnt[0] << ' ' << cnt[1] << ' ' 46 << cnt[2] << ' ' << cnt[3] << ' ' 47 << cnt[4] << endl; 48 49 return 0; 50 } 51
1 #include <iostream> 2 3 using namespace std; 4 5 class people 6 { 7 public: 8 string name; 9 int oriMoney; 10 int curMoney; 11 12 people() 13 { 14 oriMoney = curMoney = 0; 15 } 16 void present(people & p, int m) 17 { 18 p.curMoney += oriMoney / m; 19 curMoney -= oriMoney / m; 20 } 21 } pep[10]; 22 23 int main() 24 { 25 freopen("gift1.in", "r", stdin); 26 freopen("gift1.out", "w", stdout); 27 28 int n, m; 29 30 cin >> n; 31 for (int i = 0; i < n; i++) 32 cin >> pep[i].name; 33 34 string sname, tname; 35 int snum, tnum; 36 37 for (int i = 0; i < n; i++) 38 { 39 cin >> sname; 40 41 for (int j = 0; j < n; j++) 42 if (pep[j].name == sname) 43 { 44 snum = j; break; 45 } 46 47 cin >> pep[snum].oriMoney >> m; 48 49 for (int j = 0; j < m; j++) 50 { 51 cin >> tname; 52 for (int k = 0; k < n; k++) 53 if (pep[k].name == tname) 54 { 55 tnum = k; break; 56 } 57 pep[snum].present(pep[tnum], m); 58 } 59 } 60 61 for (int i = 0; i < n; i++) 62 cout << pep[i].name << ' ' << pep[i].curMoney << endl; 63 64 return 0; 65 } 66
1 #include <fstream> 2 3 using namespace std; 4 5 int main() 6 { 7 ifstream fin("ride.in"); 8 ofstream fout("ride.out"); 9 10 string cometName; 11 string groupName; 12 13 fin >> cometName; 14 fin >> groupName; 15 16 int cometNum = 1; 17 int groupNum = 1; 18 19 for (unsigned i = 0; i < cometName.size(); i++) 20 cometNum *= (cometName[i] - 'A' + 1); 21 for (unsigned i = 0; i < groupName.size(); i++) 22 groupNum *= (groupName[i] - 'A' + 1); 23 24 fout << (cometNum % 47 == groupNum % 47 ? "GO" : "STAY") << endl; 25 26 fin.close(); 27 fout.close(); 28 29 return 0; 30 } 31
1 /* Accepted 0.109 457 KB */ 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
1 /* Accepted 0.312 557 KB */ 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 }
1 /* Accepted 1334 C++ 00:00.00 840K */ 2 #include <iostream> 3 4 using namespace std; 5 6 char c[] = "0123456789ABCDEF"; 7 8 void convert(string & num, int s, int t) 9 { 10 if(s == t) 11 { 12 printf("%7s\n", num.c_str()); 13 return; 14 } 15 16 int n = 0; 17 for(int i = 0; i < num.size(); i++) 18 { 19 int m = 1; 20 for(int k = 1; k < num.size() - i; k++) 21 m *= s; 22 if(num[i] >= 'A' && num[i] <= 'F') 23 n += (num[i] - 'A' + 10) * m; 24 else 25 n += (num[i] - '0') * m; 26 } 27 28 string str; 29 while(n) 30 { 31 str += c[n % t]; 32 n /= t; 33 } 34 35 if(str.size() > 7) 36 printf("%7s\n", "ERROR"); 37 else 38 { 39 for(int i = 0; i < str.size() / 2; i++) 40 swap(str[i], str[str.size() - i - 1]); 41 printf("%7s\n", str.c_str()); 42 } 43 return; 44 45 cout << endl; 46 } 47 48 int main() 49 { 50 string num; 51 int s, t; 52 53 while(cin >> num >> s >> t) 54 convert(num, s, t); 55 56 return 0; 57 } 58
1 /* Accepted 588K 0MS G++ 1469B */ 2 #include <string> 3 #include <iostream> 4 5 using namespace std; 6 7 int cnt[10][10][10][10][10][10]; 8 9 int main() 10 { 11 char num[6]; 12 for(int i = 0; i < 6; i++) 13 { 14 cin >> num[i]; 15 num[i] -= '0'; 16 } 17 18 cnt[num[0]][num[1]][num[2]][num[3]][num[4]][num[5]] = 1; 19 20 while(true) 21 { 22 int n = 0; 23 for(int i = 1; i < 6 - 1; i++) 24 n = n * 10 + num[i]; 25 26 n = n * n; 27 28 char x[6]; 29 for(int i = 6 - 1; i >= 0; i--) 30 { 31 x[i] = n % 10; 32 n /= 10; 33 } 34 35 if(cnt[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]]) 36 { 37 int i = 0; 38 while(x[i] == 0 && i < 6) 39 i++; 40 if(i == 6) 41 cout << 0; 42 while(i < 6) 43 cout << int(x[i++]); 44 45 int a = cnt[num[0]][num[1]][num[2]][num[3]][num[4]][num[5]] + 1; 46 int b = cnt[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]]; 47 cout << ' ' << a - b << ' ' << a - 1 << endl; 48 break; 49 } 50 51 cnt[x[0]][x[1]][x[2]][x[3]][x[4]][x[5]] 52 = cnt[num[0]][num[1]][num[2]][num[3]][num[4]][num[5]] + 1; 53 54 for(int i = 0; i < 6; i++) 55 num[i] = x[i]; 56 } 57 58 return 0; 59 } 60
|