Untitled
Answer 1: #include <vector> int CalLargestBlock(int id_ignore){ vector<int> arr; for(int i = 0; i < N; i++) { if(id_ignore == ID[i]) { continue; } arr.push_back(ID[i]); } int max_block_size = 0; int cur_size = 1; for (int i = 1; i < arr.size(); i++){ if (arr[i] == arr[i - 1]) cur_size++; else cur_size = 1; if (max_block_size < cur_size) { max_block_size = cur_size; } } return max_block_size; } Answer 2: for(int i = 0 ; i < N; i++) { int sum = 0; for(int j = 0; j < N; j++) { sum += Box[i][j]; } rsum.push_back(sum); maxi = max(sum, maxi); } for(int i = 0 ; i < N; i++) { int sum = 0; for(int j = 0; j < N; j++) { sum += Box[j][i]; } csum.push_back(sum); maxi = max(sum, maxi); } for(int i = 0; i< N; i++) { int diff = maxi - rsum[i]; ans += diff; } Answer 4: int ComputeTime(void) { int s = ConvertInt(start_time) * 60 + ConvertInt(start_time + 3); int e = ConvertInt(end_time) * 60 + ConvertInt(end_time + 3); if (e < s) e += 1440; return (e - s); } int Solve(void) { int p; int t = ComputeTime(); if (t < 30) return 500; p = 500 + ((int)((t - 30 + 9) / 10)) * 300; return min(p, 30000); } Answer 5: #include <iostream> #include <vector> #include <cstring> using namespace std; #define MAXN 100 int N; char map[MAXN][MAXN]; // Grid to store the land owner information bool visited[MAXN][MAXN]; // To mark visited cells int dz[] = {-1, 1, 0, 0}; // Directions for DFS (up, down, left, right) int dx[] = {0, 0, -1, 1}; void dfs(int x, int y, char owner) { visited[x][y] = true; for (int i = 0; i < 4; ++i) { int nx = x + dx[i], ny = y + dz[i]; if (nx >= 0 && ny >= 0 && nx < N && ny < N && !visited[nx][ny] && map[nx][ny] == owner) { dfs(nx, ny, owner); } } } void InputData() { cin >> N; for (int i = 0; i < N; ++i) { cin >> map[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); InputData(); // Count zones and grids for each owner int zoneCountR = 0, zoneCountG = 0, zoneCountB = 0; int gridCountR = 0, gridCountG = 0, gridCountB = 0; memset(visited, false, sizeof(visited)); // Traverse the grid to find zones for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { if (!visited[i][j]) { char owner = map[i][j]; // Start a new DFS to mark the entire zone dfs(i, j, owner); if (owner == 'R') { zoneCountR++; } else if (owner == 'G') { zoneCountG++; } else if (owner == 'B') { zoneCountB++; } } if (map[i][j] == 'R') gridCountR++; else if (map[i][j] == 'G') gridCountG++; else if (map[i][j] == 'B') gridCountB++; } } // We have the counts for each owner (R, G, B) // Now decide who has the largest number of zones or grids if necessary char ans; int areacnt; // First compare the number of zones if (zoneCountR > zoneCountG && zoneCountR > zoneCountB) { ans = 'R'; areacnt = zoneCountR; } else if (zoneCountG > zoneCountR && zoneCountG > zoneCountB) { ans = 'G'; areacnt = zoneCountG; } else if (zoneCountB > zoneCountR && zoneCountB > zoneCountG) { ans = 'B'; areacnt = zoneCountB; } // If the number of zones is the same, compare the grid counts else if (zoneCountR == zoneCountG && zoneCountR > zoneCountB) { if (gridCountR > gridCountG) { ans = 'R'; areacnt = zoneCountR; } else if (gridCountG > gridCountR) { ans = 'G'; areacnt = zoneCountG; } else { ans = 'R'; // Tie-breaking by lexicographical order areacnt = zoneCountR; } } else if (zoneCountG == zoneCountB && zoneCountG > zoneCountR) { if (gridCountG > gridCountB) { ans = 'G'; areacnt = zoneCountG; } else if (gridCountB > gridCountG) { ans = 'B'; areacnt = zoneCountB; } else { ans = 'G'; // Tie-breaking by lexicographical order areacnt = zoneCountG; } } else if (zoneCountR == zoneCountB && zoneCountR > zoneCountG) { if (gridCountR > gridCountB) { ans = 'R'; areacnt = zoneCountR; } else if (gridCountB > gridCountR) { ans = 'B'; areacnt = zoneCountB; } else { ans = 'R'; // Tie-breaking by lexicographical order areacnt = zoneCountR; } } else if (zoneCountR == zoneCountG && zoneCountR == zoneCountB) { // If all the zone counts are the same, tie-breaking by grids if (gridCountR > gridCountG && gridCountR > gridCountB) { ans = 'R'; areacnt = zoneCountR; } else if (gridCountG > gridCountR && gridCountG > gridCountB) { ans = 'G'; areacnt = zoneCountG; } else if (gridCountB > gridCountR && gridCountB > gridCountG) { ans = 'B'; areacnt = zoneCountB; } // If the grids are also equal, use lexicographical order else { ans = 'R'; // R < G < B areacnt = zoneCountR; } } // Output the result cout << ans << " " << areacnt << "\n"; return 0; } Answer 7: int dr[8] = { -1, 1, 0, 0, 1, 1, -1, -1 }; int dc[8] = { 0, 0, -1, 1, 1, -1, -1, 1 }; void Touch(int r, int c) { int V; if (A[r][c] == 0) V = 1; else V = 0; A[r][c] = V; for (int k = 0; k < 8; k++) { int nr = r; int nc = c; int flag = 0, bomb = 0; for (;;) { nr = nr + dr[k]; nc = nc + dc[k]; if (nr < 0 || nr >= H || nc < 0 || nc >= W) break; if (A[nr][nc] == 2) { bomb = 1; } if (A[nr][nc] == V) { flag = 1; break; } } if (flag == 1 && bomb == 0) { nr = r; nc = c; for (;;) { nr = nr + dr[k]; nc = nc + dc[k]; if (A[nr][nc] == V) { break; } A[nr][nc] = V; } } if (flag == 1 && bomb == 1) { nr = r; nc = c; for (;;) { nr = nr + dr[k]; nc = nc + dc[k]; if (nr < 0 || nr >= H || nc < 0 || nc >= W) break; A[nr][nc] = V; } } } } Answer 8: #include <bits/stdc++.h> using namespace std; int N;//선수의 인원수 number of playersnumber of players long long T;//시간 time long long P[100000 + 10];//선수 초기 위치 player initial position long long S[100000 + 10];//선수 속도 player speed int group_first[100000 + 10];//각 그룹의 선두선수 leader of each group void InputData() { cin >> N >> T; for (int i = 0; i < N; i++) { cin >> P[i] >> S[i]; } } int main() { int ans = -1; InputData();//입력 Input //코드를 작성하세요 Write the code vector <long long> temp(N); for (int i = 0; i < N; i++){ temp[i] = P[i]; } for (int i = N - 1; i >= 0; i--){ if (i == N - 1) temp[i] = temp[i] + S[i] * T; else { long long x = temp[i] + S[i] * T; if (x >= temp[i + 1]) { temp[i] = temp[i + 1]; } else { temp[i] = x; } } } long long x = 1, count = 1; group_first[0] = N; for(int i = N-1; i > 0; i--){ if(temp[i] != temp[i - 1]){ count++; group_first[x++] = i; } } ans = count; //출력 Output cout << ans << endl; for (int i = 0; i < ans; i++) cout << group_first[i] << " "; return 0; } Answer 10: int di1[8] = { -1,-1,0,1,1,1,0,-1 }; int dj1[8] = { 0,1,1,1,0,-1,-1,-1 }; int di2[8] = { 1,1,0,-1,-1,-1,0,1 }; int dj2[8] = { 0,-1,-1,-1,0,1,1,1 }; int Solve(void) { int count = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (a[i][j] == X2) { for (int k = 0; k < 8; k++) { int ni1 = i + di1[k]; int nj1 = j + dj1[k]; int ni2 = i + di2[k]; int nj2 = j + dj2[k]; if ((ni1 < 0 || ni1 >= N) || (nj1 < 0 || nj1 >= N) || (ni2 < 0 || ni2 >= N) || (nj2 < 0 || nj2 >= N)) continue; if ((a[ni1][nj1] == X1 && a[ni2][nj2] == X3) || (a[ni1][nj1] == X3 && a[ni2][nj2] == X1)) { count++; } } } } } return count / 2; } Answer 11: #include <iostream> #include <bits/stdc++.h> using namespace std; int S, E1, E2; int ans1, ans2; // Function to input data void InputData() { cin >> S >> E1 >> E2; } // Function to precompute the number of factors for each number from 1 to 9999 void countFactors(vector<int>& factorCounts) { for (int i = 1; i < 10000; ++i) { for (int j = i; j < 10000; j += i) { factorCounts[j]++; } } } // Function to find the minimum number of moves from start to end int FindMoves(int start, int end, const vector<int>& factorCounts) { queue<pair<int, int>> q; vector<bool> visited(10000, false); q.push({start, 0}); visited[start] = true; while (!q.empty()) { int current = q.front().first; int moves = q.front().second; q.pop(); if (current == end) { return moves; } string currentStr = to_string(current); for (int i = 0; i < 4; ++i) { for (char d = '0'; d <= '9'; ++d) { if (currentStr[i] == d) continue; string nextStr = currentStr; nextStr[i] = d; int next = stoi(nextStr); if (next >= 1000 && next <= 9999 && !visited[next] && abs(factorCounts[current] - factorCounts[next]) <= 1) { visited[next] = true; q.push({next, moves + 1}); } } } } return -1; // If no path is found } int main() { InputData(); // Input the start and end numbers vector<int> factorCounts(10000, 0); countFactors(factorCounts); // Precompute factor counts for numbers 1 to 9999 // Find the minimum moves for both E1 and E2 from S cout << FindMoves(S, E1, factorCounts) << '\n'; cout << FindMoves(S, E2, factorCounts) << '\n'; return 0; } Answer 13: #define ValR R / 2 #define ValC C / 2 #define SWAP(a, b) {int temp = a;a = b;b = temp;} void Rotate(int sr, int sc, int er, int ec) { int a = Mat[sr][sc]; if(ValR <= sr || ValC <= sc){ return; } for (int r = sr + 1; r <= er; r++) { SWAP(Mat[r][sc], a); } for (int c = sc + 1; c <= ec; c++) { SWAP(Mat[er][c], a); } for (int r = er - 1; r >= sr; r--) { SWAP(Mat[r][ec], a); } for (int c = ec - 1; c >= sc; c--) { SWAP(Mat[sr][c], a); } }
Leave a Comment