diff --git a/201902654/10026.cpp b/201902654/10026.cpp new file mode 100644 index 0000000..18cf87b --- /dev/null +++ b/201902654/10026.cpp @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +char board[101][101]; +bool is_visit[101][101]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N; + int normal_result = 0; + int abnormal_result = 0; + queue > Q; + cin >> N; + for (int i=0;i> line; + for (int j=0;j top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && !is_visit[ny][nx] && board[ny][nx] == board[top.Y][top.X]) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + } + } + } + for (int i=0;i top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && !is_visit[ny][nx] && (board[ny][nx] == board[top.Y][top.X] || (board[ny][nx] == 'R' && board[top.Y][top.X] == 'G') || (board[ny][nx] == 'G' && board[top.Y][top.X] == 'R'))) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + } + } + } + cout << normal_result << ' ' << abnormal_result; + return 0; +} diff --git a/201902654/1012.cpp b/201902654/1012.cpp new file mode 100644 index 0000000..d84aa5d --- /dev/null +++ b/201902654/1012.cpp @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[51][51]; +int is_visit[51][51]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int T; + cin >> T; + for (int i=0;i > Q; + cin >> M >> N >> K; + for (int y=0;y> x >> y; + board[y][x] = 1; + } + for (int y=0;y top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N && is_visit[ny][nx] == false && board[ny][nx] == 1) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + } + } + } + cout << result << '\n'; + } + + return 0; +} diff --git a/201902654/1021.cpp b/201902654/1021.cpp new file mode 100644 index 0000000..d7511ac --- /dev/null +++ b/201902654/1021.cpp @@ -0,0 +1,61 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M; + int result = 0; + deque D; + cin >> N >> M; + for (int i=0;i> value; + int front_count = 0; + int back_count = 0; + while (D.front() != value) { + D.push_back(D.front()); + D.pop_front(); + front_count++; + } + for (int i=0;i back_count) { + for (int i=0;i + +using namespace std; +#define X first +#define Y second +int order; +int N, r, c; + +int record(int N, int x, int y) { + int half = pow(2, N-1); + if (N == 0) { + return 0; + } + if (x < half && y < half) return record(N-1, x, y); + if (x >= half && y < half) return half * half * 1 + record(N-1, x-half, y); + if (x < half && y >= half) return half * half * 2 + record(N-1, x, y-half); + if (x >= half && y >= half) return half * half * 3 + record(N-1, x-half, y-half); +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> r >> c; + + cout << record(N, c, r); + + + return 0; +} diff --git a/201902654/10773.cpp b/201902654/10773.cpp new file mode 100644 index 0000000..7d1a2d0 --- /dev/null +++ b/201902654/10773.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int K; + long result = 0; + stack stack; + cin >> K; + + for (int i=0;i> input; + + if (input == 0) { + stack.pop(); + } else { + stack.push(input); + } + } + + while (!stack.empty()) { + result = result + stack.top(); + stack.pop(); + } + + cout << result; + + return 0; +} diff --git a/201902654/10799.cpp b/201902654/10799.cpp new file mode 100644 index 0000000..f5c504c --- /dev/null +++ b/201902654/10799.cpp @@ -0,0 +1,32 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + stack S; + long result = 0; + cin >> line; + for (int i=0;i +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N; + cin >> N; + int count[201]; + fill(count, count+201, 0); + for (int i=0;i> val; + count[val + 100]++; + } + int find; + cin >> find; + cout << count[find+100]; + + return 0; +} diff --git a/201902654/10808.cpp b/201902654/10808.cpp new file mode 100644 index 0000000..40e4dc1 --- /dev/null +++ b/201902654/10808.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +using namespace std; + +int main(void) { + string s; + getline(cin, s); + vector alpha(26, 0); + + for (int i=0;i +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N; + stack stack; + cin >> N; + for (int i=0;i> command; + if (command == "push") { + int val; + cin >> val; + stack.push(val); + } else if (command == "pop") { + if (stack.empty()) { + cout << -1 << '\n'; + } else { + cout << stack.top() << '\n'; + stack.pop(); + } + } else if (command == "size") { + cout << stack.size() << '\n'; + } else if (command == "empty") { + if (stack.empty()) { + cout << 1 << '\n'; + } else { + cout << 0 << '\n'; + } + } else { + if (stack.empty()) { + cout << -1 << '\n'; + } else { + cout << stack.top() << '\n'; + } + } + } + + return 0; +} diff --git a/201902654/10845.cpp b/201902654/10845.cpp new file mode 100644 index 0000000..8069f29 --- /dev/null +++ b/201902654/10845.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + queue Q; + int N; + cin >> N; + for (int i=0;i> command; + if (command == "push") { + int value; + cin >> value; + + Q.push(value); + } else { + if (command == "pop") { + cout << ((Q.size() == 0) ? -1 : Q.front()); + if (Q.size() != 0) { + Q.pop(); + } + } else if (command == "size") { + cout << Q.size(); + } else if (command == "empty") { + cout << ((Q.size() == 0) ? 1 : 0); + } else if (command == "front") { + cout << ((Q.size() == 0) ? -1 : Q.front()); + } else if (command == "back") { + cout << ((Q.size() == 0) ? -1 : Q.back()); + } + cout << '\n'; + } + } + + return 0; +} diff --git a/201902654/10866.cpp b/201902654/10866.cpp new file mode 100644 index 0000000..f1daf88 --- /dev/null +++ b/201902654/10866.cpp @@ -0,0 +1,47 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + deque D; + int N; + cin >> N; + for (int i=0;i> command; + if (command == "push_front") { + int value; + cin >> value; + D.push_front(value); + } else if (command == "push_back") { + int value; + cin >> value; + D.push_back(value); + } else if (command == "pop_front") { + cout << ((D.size() == 0) ? -1 : D.front()) << '\n'; + if (D.size() != 0) { + D.pop_front(); + } + } else if (command == "pop_back") { + cout << ((D.size() == 0) ? -1 : D.back()) << '\n'; + if (D.size() != 0) { + D.pop_back(); + } + } else if (command == "size") { + cout << D.size() << '\n'; + } else if (command == "empty") { + cout << ((D.size() == 0) ? 1 : 0) << '\n'; + } else if (command == "front") { + cout << ((D.size() == 0) ? -1 : D.front()) << '\n'; + } else if (command == "back") { + cout << ((D.size() == 0) ? -1 : D.back()) << '\n'; + } + } + + return 0; +} diff --git a/201902654/10871.cpp b/201902654/10871.cpp new file mode 100644 index 0000000..86fa64f --- /dev/null +++ b/201902654/10871.cpp @@ -0,0 +1,18 @@ +#include +#include + +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + int N, X; + cin >> N >> X; + for(int i=0;i> num; + if (num < X) { + cout << num << ' '; + } + } +} diff --git a/201902654/11003.cpp b/201902654/11003.cpp new file mode 100644 index 0000000..5578db4 --- /dev/null +++ b/201902654/11003.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + deque > D; + int N, L; + cin >> N >> L; + int arr[N]; + for (int i=0;i> arr[i]; + } + D.push_back(make_pair(arr[0], L)); + for (int i=0;i= arr[i]) { + pop_sum += D.back().second; + D.pop_back(); + } + D.push_back(make_pair(arr[i], pop_sum + 1)); + } + pair front = D.front(); + if (front.second == 1) { + D.pop_front(); + } else { + D.pop_front(); + D.push_front(make_pair(front.first, front.second - 1)); + } + cout << front.first << ' '; + } + + return 0; +} diff --git a/201902654/1158.cpp b/201902654/1158.cpp new file mode 100644 index 0000000..e269c67 --- /dev/null +++ b/201902654/1158.cpp @@ -0,0 +1,41 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N, K; + cin >> N >> K; + list L,result; + auto cursor = L.begin(); + for (int i=0;i::iterator it = result.begin();it != result.end();it++) { + cout << *it; + if (it != --result.end()) { + cout << ", "; + } + } + cout << '>'; + + return 0; +} diff --git a/201902654/11729.cpp b/201902654/11729.cpp new file mode 100644 index 0000000..d98d7e5 --- /dev/null +++ b/201902654/11729.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +vector > stacks; +int result = 0; + +void move(int n, int from, int to, int middle, bool is_print) { + if (n == stacks[from].top()) { + result++; + stacks[to].push(stacks[from].top()); + stacks[from].pop(); + if (is_print) { + cout << from + 1 << ' ' << to + 1 << '\n'; + } + } else { + move(n-1, from, middle, to, is_print); + result++; + stacks[to].push(stacks[from].top()); + stacks[from].pop(); + if (is_print) { + cout << from + 1 << ' ' << to + 1 << '\n'; + } + move(n-1, middle, to, from, is_print); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + stacks.push_back(stack()); + stacks.push_back(stack()); + stacks.push_back(stack()); + int N; + cin >> N; + for (int i=N;i>0;i--) { + stacks[0].push(i); + } + move(N, 0, 2, 1, false); + for (int i=N;i>0;i--) { + stacks[0].push(i); + } + stacks[2] = stack(); + cout << result << '\n'; + move(N, 0, 2, 1, true); + + return 0; +} diff --git a/201902654/13549.cpp b/201902654/13549.cpp new file mode 100644 index 0000000..97fe0fa --- /dev/null +++ b/201902654/13549.cpp @@ -0,0 +1,41 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int N, K; +int min_time[200001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + fill(min_time, min_time + 200001, __INT_MAX__); + cin >> N >> K; + queue > Q; + min_time[N] = 0; + Q.push(make_pair(N, 0)); + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + if (top.X - 1 >= 0 && min_time[top.X - 1] > top.Y + 1) { + min_time[top.X - 1] = top.Y + 1; + Q.push(make_pair(top.X - 1, top.Y + 1)); + } + if (top.X + 1 < 200001 && min_time[top.X + 1] > top.Y + 1) { + min_time[top.X + 1] = top.Y + 1; + Q.push(make_pair(top.X + 1, top.Y + 1)); + } + if (top.X * 2 < 200001 && min_time[top.X * 2] > top.Y) { + min_time[top.X * 2] = top.Y; + Q.push(make_pair(top.X * 2, top.Y)); + } + } + cout << min_time[K]; + + return 0; +} diff --git a/201902654/1406.cpp b/201902654/1406.cpp new file mode 100644 index 0000000..2ba73df --- /dev/null +++ b/201902654/1406.cpp @@ -0,0 +1,45 @@ +#include +#include +#include + +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + + string N; + int M; + cin >> N; + list L; + for (char ch : N) { + L.push_back(ch); + } + auto cursor = L.end(); + cin >> M; + for (int i=0;i> op; + if (op == 'L') { + if (cursor != L.begin()) { + cursor--; + } + } else if (op == 'D') { + if (cursor != L.end()) { + cursor++; + } + } else if (op == 'B') { + if (cursor != L.begin()) { + cursor--; + cursor = L.erase(cursor); + } + } else if (op == 'P') { + char ch; + cin >> ch; + L.insert(cursor, ch); + } + } + for (char ch : L) { + cout << ch; + } +} diff --git a/201902654/14442.cpp b/201902654/14442.cpp new file mode 100644 index 0000000..1bb9a88 --- /dev/null +++ b/201902654/14442.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int map[1001][1001]; +int dist[11][1001][1001]; +int N, M, K; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + for (int i=0;i<11;i++) { + for (int j=0;j<1001;j++) { + fill(dist[i][j], dist[i][j] + 1001, -1); + } + } + cin >> N >> M >> K; + for (int i=0;i> line; + for (int j=0;j > Q; + dist[0][0][0] = 1; + Q.push(make_tuple(0, 0, 0)); + while (!Q.empty()) { + tuple top = Q.front(); Q.pop(); + int x = get<0>(top); + int y = get<1>(top); + int broken_wall_count = get<2>(top); + for (int i=0;i<4;i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N) { + bool is_wall = map[ny][nx] == 1; + int next_wall_count = is_wall ? broken_wall_count + 1 : broken_wall_count; + if (next_wall_count <= K && dist[next_wall_count][ny][nx] == -1) { + dist[next_wall_count][ny][nx] = dist[broken_wall_count][y][x] + 1; + Q.push(make_tuple(nx, ny, next_wall_count)); + } + } + } + } + int result = __INT_MAX__; + for (int i=0;i<=K;i++) { + if (dist[i][N-1][M-1] != -1) { + result = min(result, dist[i][N-1][M-1]); + } + // cout << dist[i][N-1][M-1]; + } + cout << ((result == __INT_MAX__) ? -1 : result); + + return 0; +} diff --git a/201902654/1475.cpp b/201902654/1475.cpp new file mode 100644 index 0000000..442142c --- /dev/null +++ b/201902654/1475.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + + string N; + vector arr(10, 0); + getline(cin, N); + for (char ch : N) { + ++arr[ch - '0']; + } + arr[6] = (arr[6] + arr[9] + 1) / 2; + arr[9] = 0; + int max = 0; + for (int iter : arr) { + if (max < iter) { + max = iter; + } + } + cout << max; +} diff --git a/201902654/1629.cpp b/201902654/1629.cpp new file mode 100644 index 0000000..a8ccf7c --- /dev/null +++ b/201902654/1629.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; + +long long POW(int a, int b, int c) { + if (b == 1) return a % c; + long long val = POW(a, b/2, c); + val = val * val % c; + if (b % 2 == 0) { + return val; + } else { + return val * a % c; + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int A, B, C; + cin >> A >> B >> C; + + cout << POW(A, B, C); + return 0; +} diff --git a/201902654/1644.cpp b/201902654/1644.cpp new file mode 100644 index 0000000..784f1cd --- /dev/null +++ b/201902654/1644.cpp @@ -0,0 +1,44 @@ +#include + +using namespace std; +int count_divider[4000001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + vector prime_numbers; + for (int i=2;i<=4000000;i++) { + for (int index=i;index<=4000000;index+=i) { + ++count_divider[index]; + } + } + for (int i=2;i<=4000000;i++) { + if (count_divider[i] == 1) { + prime_numbers.push_back(i); + } + } + int result = 0; + long long N; + cin >> N; + vector sub_sum; + sub_sum.push_back(0); + for (int i=1;i<=prime_numbers.size();i++) { + sub_sum.push_back(sub_sum[i-1] + prime_numbers[i-1]); + } + int end = 0; + int length = sub_sum.size(); + for (int start=0;start= N) { + if (sub_sum[end] - sub_sum[start] == N) { + result++; + } + break; + } + end++; + } + } + cout << result; + + return 0; +} diff --git a/201902654/1697.cpp b/201902654/1697.cpp new file mode 100644 index 0000000..67c54e8 --- /dev/null +++ b/201902654/1697.cpp @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int min_time[100001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + fill(min_time, min_time+100001, -1); + + int N, K; + queue Q; + cin >> N >> K; + min_time[N] = 0; + Q.push(N); + while (!Q.empty()) { + int top = Q.front(); Q.pop(); + if (top == K) { + break; + } + if (top - 1 >= 0 && min_time[top - 1] == -1) { + min_time[top - 1] = min_time[top] + 1; + Q.push(top - 1); + } + if (top + 1 < 100001 && min_time[top + 1] == -1) { + min_time[top + 1] = min_time[top] + 1; + Q.push(top + 1); + } + if (top * 2 < 100001 && min_time[top * 2] == -1) { + min_time[top * 2] = min_time[top] + 1; + Q.push(top * 2); + } + } + cout << min_time[K]; + + return 0; +} diff --git a/201902654/17298.cpp b/201902654/17298.cpp new file mode 100644 index 0000000..721d07b --- /dev/null +++ b/201902654/17298.cpp @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N; + cin >> N; + stack > stack; + int result[N]; + fill(result, result+N, -1); + for (int i=0;i> input; + while (!stack.empty() && stack.top().second < input) { + result[stack.top().first] = input; + stack.pop(); + } + stack.push(make_pair(i, input)); + } + for (int iter : result) { + cout << iter << ' '; + } + + return 0; +} diff --git a/201902654/17478.cpp b/201902654/17478.cpp new file mode 100644 index 0000000..bfd2a98 --- /dev/null +++ b/201902654/17478.cpp @@ -0,0 +1,38 @@ +#include + +using namespace std; +int N; + +void print_dash(int count) { + for (int i=0;i> N; + cout << "어느 한 컴퓨터공학과 학생이 유명한 교수님을 찾아가 물었다." << '\n'; + print(0, N); + return 0; +} diff --git a/201902654/1780.cpp b/201902654/1780.cpp new file mode 100644 index 0000000..6827f9b --- /dev/null +++ b/201902654/1780.cpp @@ -0,0 +1,51 @@ +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int counts[3]; +int board[2200][2200]; +int N; + +void recursion(int N, int init_x, int init_y) { + int comp = board[init_y][init_x]; + bool is_same = true; + for (int y = init_y; y < N + init_y; y++) { + for (int x = init_x; x < N + init_x; x++) { + if (board[y][x] != comp) is_same = false; + } + } + if (is_same) { + counts[comp + 1]++; + } else { + int w = N/3; + recursion(N/3, init_x, init_y); + recursion(N/3, init_x+w*1, init_y); + recursion(N/3, init_x+w*2, init_y); + recursion(N/3, init_x, init_y+w*1); + recursion(N/3, init_x+w*1, init_y+w*1); + recursion(N/3, init_x+w*2, init_y+w*1); + recursion(N/3, init_x, init_y+w*2); + recursion(N/3, init_x+w*1, init_y+w*2); + recursion(N/3, init_x+w*2, init_y+w*2); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int i=0;i> board[i][j]; + } + } + recursion(N, 0, 0); + cout << counts[0] << '\n'; + cout << counts[1] << '\n'; + cout << counts[2] << '\n'; + + return 0; +} diff --git a/201902654/1806.cpp b/201902654/1806.cpp new file mode 100644 index 0000000..ae24c9a --- /dev/null +++ b/201902654/1806.cpp @@ -0,0 +1,33 @@ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N, S; + cin >> N >> S; + int arr[N]; + for (int i=0;i> arr[i]; + } + int sub_sum[N+1]; + sub_sum[0] = 0; + for (int i=1;i<=N;i++) { + sub_sum[i] = sub_sum[i-1] + arr[i-1]; + } + int end = N; + int result = __INT_MAX__; + for (int start=N;start>=0;start--) { + while (end >= 0) { + if (sub_sum[start] - sub_sum[end] >= S) { + result = min(result, start - end); + break; + } + end--; + } + } + cout << ((result == __INT_MAX__) ? 0 : result); + + return 0; +} diff --git a/201902654/18258.cpp b/201902654/18258.cpp new file mode 100644 index 0000000..8069f29 --- /dev/null +++ b/201902654/18258.cpp @@ -0,0 +1,42 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + queue Q; + int N; + cin >> N; + for (int i=0;i> command; + if (command == "push") { + int value; + cin >> value; + + Q.push(value); + } else { + if (command == "pop") { + cout << ((Q.size() == 0) ? -1 : Q.front()); + if (Q.size() != 0) { + Q.pop(); + } + } else if (command == "size") { + cout << Q.size(); + } else if (command == "empty") { + cout << ((Q.size() == 0) ? 1 : 0); + } else if (command == "front") { + cout << ((Q.size() == 0) ? -1 : Q.front()); + } else if (command == "back") { + cout << ((Q.size() == 0) ? -1 : Q.back()); + } + cout << '\n'; + } + } + + return 0; +} diff --git a/201902654/1874.cpp b/201902654/1874.cpp new file mode 100644 index 0000000..65dca48 --- /dev/null +++ b/201902654/1874.cpp @@ -0,0 +1,45 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int max = 0; + stack stack; + int n; + cin >> n; + int arr[n]; + list result; + for (int i=0;i> input; + arr[i] = input; + } + for (int i=0;i max) { + for (int j=0;j +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second + +int board[501][501]; +bool is_visit[501][501]; +int dx[4] = {1, -1, 0, 0}; +int dy[4] = {0, 0, 1, -1}; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int n, m; + cin >> n >> m; + int count_result = 0; + int area_result = 0; + queue > Q; + for (int i=0;i> board[i][j]; + } + } + + for (int i=0;i top = Q.front(); Q.pop(); + for (int k=0;k<4;k++) { + int nx = top.X + dx[k]; + int ny = top.Y + dy[k]; + if (0 <= nx && nx < m && 0 <= ny && ny < n && board[ny][nx] == 1 && !is_visit[ny][nx]) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + area_result = max(area_result, area); + } + } + } + cout << count_result << '\n' << area_result; + + return 0; +} diff --git a/201902654/2146.cpp b/201902654/2146.cpp new file mode 100644 index 0000000..b1300d3 --- /dev/null +++ b/201902654/2146.cpp @@ -0,0 +1,94 @@ +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int N; +int board[101][101]; +int min_length[101][101]; +bool is_visit[101][101]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int y=0;y> board[y][x]; + } + } + int area = 1; + for (int y=0;y 0) { + queue > Q; + Q.push(make_pair(x, y)); + is_visit[y][x] = true; + board[y][x] = area; + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && !is_visit[ny][nx] && board[ny][nx] > 0) { + is_visit[ny][nx] = true; + board[ny][nx] = area; + Q.push(make_pair(nx, ny)); + } + } + } + + area++; + } + } + } + // cout << "#############" << '\n'; + // for (int y=0;y > Q; + Q.push(make_tuple(x, y, board[y][x], 0)); + min_length[y][x] = 0; + while (!Q.empty()) { + tuple top = Q.front(); Q.pop(); + int x = get<0>(top); + int y = get<1>(top); + int area = get<2>(top); + int length = get<3>(top); + for (int i=0;i<4;i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && length + 1 <= result) { + // cout << "^^" << '\n'; + if (board[ny][nx] == 0 && min_length[ny][nx] > length + 1) { + min_length[ny][nx] = length + 1; + Q.push(make_tuple(nx, ny, area, length + 1)); + } else if (board[ny][nx] != 0 && board[ny][nx] != area) { + result = min(result, length); + // cout << length << '\n'; + queue > empty; + Q.swap(empty); + } + } + } + } + } + } + } + cout << result; + + return 0; +} diff --git a/201902654/2164.cpp b/201902654/2164.cpp new file mode 100644 index 0000000..6ce1403 --- /dev/null +++ b/201902654/2164.cpp @@ -0,0 +1,25 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + queue Q; + int N; + cin >> N; + for (int i=0;i +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[101][101]; +int is_visit[101][101]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N, M; + int result = 1000000000; + queue > Q; // X Y val + cin >> N >> M; + for (int i=0;i> line; + for (int j=0;j top = Q.front(); Q.pop(); + if (get<0>(top) == M - 1 && get<1>(top) == N - 1) { + result = min(result, get<2>(top)); + } + for (int i=0;i<4;i++) { + int nx = get<0>(top) + dx[i]; + int ny = get<1>(top) + dy[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N && !is_visit[ny][nx] && board[ny][nx] == 1) { + is_visit[ny][nx] = true; + Q.push(make_tuple(nx, ny, get<2>(top) + 1)); + } + } + } + cout << result; + + return 0; +} diff --git a/201902654/2206.cpp b/201902654/2206.cpp new file mode 100644 index 0000000..52447b9 --- /dev/null +++ b/201902654/2206.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int map[1001][1001]; +int MAX_BREAKABLE_WALL = 1; +int dist[2][1001][1001]; +int N, M; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + for (int i=0;i<2;i++) { + for (int j=0;j<1001;j++) { + fill(dist[i][j], dist[i][j] + 1001, -1); + } + } + cin >> N >> M; + for (int i=0;i> line; + for (int j=0;j > Q; + dist[0][0][0] = 1; + Q.push(make_tuple(0, 0, 0)); + while (!Q.empty()) { + tuple top = Q.front(); Q.pop(); + int x = get<0>(top); + int y = get<1>(top); + int broken_wall_count = get<2>(top); + for (int i=0;i<4;i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N) { + bool is_wall = map[ny][nx] == 1; + int next_wall_count = is_wall ? broken_wall_count + 1 : broken_wall_count; + if (next_wall_count <= MAX_BREAKABLE_WALL && dist[next_wall_count][ny][nx] == -1) { + dist[next_wall_count][ny][nx] = dist[broken_wall_count][y][x] + 1; + Q.push(make_tuple(nx, ny, next_wall_count)); + } + } + } + } + int result = (dist[0][N-1][M-1] == -1 && dist[1][N-1][M-1] == -1) + ? -1 + : (dist[0][N-1][M-1] == -1 && dist[1][N-1][M-1] != -1) + ? dist[1][N-1][M-1] + : (dist[0][N-1][M-1] != -1 && dist[1][N-1][M-1] == -1) + ? dist[0][N-1][M-1] + : min(dist[0][N-1][M-1], dist[1][N-1][M-1]); + cout << result; + + return 0; +} diff --git a/201902654/2230.cpp b/201902654/2230.cpp new file mode 100644 index 0000000..86c6e6f --- /dev/null +++ b/201902654/2230.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N, M; + cin >> N >> M; + int arr[N]; + for (int i=0;i> arr[i]; + } + sort(arr, arr + N); + int end = 0; + int result = __INT_MAX__; + for (int start=0;start= M) { + result = min(result, abs(arr[start] - arr[end])); + break; + } + end++; + } + } + cout << result; + + return 0; +} diff --git a/201902654/2468.cpp b/201902654/2468.cpp new file mode 100644 index 0000000..6b71deb --- /dev/null +++ b/201902654/2468.cpp @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int heights[101][101]; +bool is_visit[101][101]; +int N; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int y=0;y> heights[y][x]; + } + } + int result = INT_MIN; + for (int rain=0;rain<101;rain++) { + int count = 0; + for (int i=0;i rain) { + count++; + queue > Q; + Q.push(make_pair(x, y)); + is_visit[y][x] = true; + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && !is_visit[ny][nx] && heights[ny][nx] > rain) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + } + } + } + result = max(result, count); + } + cout << result; + return 0; +} diff --git a/201902654/2493.cpp b/201902654/2493.cpp new file mode 100644 index 0000000..ca43850 --- /dev/null +++ b/201902654/2493.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N; + cin >> N; + int arr[N]; + int result[N]; + fill(result, result+N, 0); + stack > stack; + for (int i=0;i> arr[i]; + } + for (int i=N-1;i>=0;i--) { + while (!stack.empty() && stack.top().second < arr[i]) { + result[stack.top().first] = i + 1; + stack.pop(); + } + stack.push(make_pair(i, arr[i])); + } + + for (int iter : result) { + cout << iter << ' '; + } + return 0; +} diff --git a/201902654/2504.cpp b/201902654/2504.cpp new file mode 100644 index 0000000..b5380c9 --- /dev/null +++ b/201902654/2504.cpp @@ -0,0 +1,55 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + cin >> line; + stack S; + int temp = 1; + int result = 0; + bool is_error = false; + for (int i=0;i + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[301][301]; +bool is_visit[301][301]; +int N, M; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N >> M; + for (int y=0;y> board[y][x]; + } + } + int time = 0; + bool is_ice_exist = false; + while (true) { + time++; + int new_board[301][301]; + for (int y=0;y<301;y++) { + fill(new_board[y], new_board[y]+301, 0); + fill(is_visit[y], is_visit[y]+301, 0); + } + is_ice_exist = false; + for (int y=0;y 0) { + is_ice_exist = true; + int count = 0; + for (int i=0;i<4;i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (board[ny][nx] == 0) { + count++; + } + } + new_board[y][x] = max(0, board[y][x] - count); + } + } + } + for (int i=0;i<301;i++) { + for (int j=0;j<301;j++) { + board[i][j] = new_board[i][j]; + } + } + queue > Q; + int area = 0; + for (int y=0;y 0 && !is_visit[y][x]) { + area++; + Q.push(make_pair(x, y)); + is_visit[y][x] = true; + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (!is_visit[ny][nx] && board[ny][nx] != 0) { + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + } + } + } + if (area > 1) { + cout << time; + break; + } + if (is_ice_exist == false) { + break; + } + } + if (is_ice_exist == false) { + cout << 0; + } + + return 0; +} diff --git a/201902654/2577.cpp b/201902654/2577.cpp new file mode 100644 index 0000000..3fea25d --- /dev/null +++ b/201902654/2577.cpp @@ -0,0 +1,25 @@ +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int A, B, C; + cin >> A >> B >> C; + int result = A * B * C; + int count[10]; + fill(count, count + 10, 0); + string text; + text = to_string(result); + for (char ch : text) { + count[ch - '0']++; + } + for (int iter : count) { + cout << iter << '\n'; + } + + return 0; +} diff --git a/201902654/2583.cpp b/201902654/2583.cpp new file mode 100644 index 0000000..b315e02 --- /dev/null +++ b/201902654/2583.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[101][101]; +bool is_visit[101][101]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int count = 0; + vector arr; + int M, N, K; + queue > Q; + cin >> M >> N >> K; + for (int i=0;i> start_x >> start_y >> end_x >> end_y; + for (int y=start_y;y top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < M && board[ny][nx] == 0 && !is_visit[ny][nx]) { + is_visit[ny][nx] = true; + area++; + Q.push(make_pair(nx, ny)); + } + } + } + arr.push_back(area); + } + } + } + sort(arr.begin(), arr.end()); + cout << count << '\n'; + for (int iter : arr) { + cout << iter << ' '; + } + + return 0; +} diff --git a/201902654/2630.cpp b/201902654/2630.cpp new file mode 100644 index 0000000..da39ceb --- /dev/null +++ b/201902654/2630.cpp @@ -0,0 +1,42 @@ +#include + +using namespace std; + +int board[128][128]; +int N; +int counts[2]; + +void recursion(int N, int init_x, int init_y) { + int comp = board[init_y][init_x]; + bool is_same = true; + for (int y = init_y; y < N + init_y; y++) { + for (int x = init_x; x < N + init_x; x++) { + if (comp != board[y][x]) is_same = false; + } + } + if (is_same) { + counts[comp]++; + } else { + int w = N / 2; + recursion(N/2, init_x, init_y); + recursion(N/2, init_x + w, init_y); + recursion(N/2, init_x, init_y + w); + recursion(N/2, init_x + w, init_y + w); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> N; + for (int y=0;y> board[y][x]; + } + } + recursion(N, 0, 0); + cout << counts[0] << '\n'; + cout << counts[1] << '\n'; + + return 0; +} diff --git a/201902654/2667.cpp b/201902654/2667.cpp new file mode 100644 index 0000000..9c5416b --- /dev/null +++ b/201902654/2667.cpp @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int map[26][26]; +bool is_visit[26][26]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N; + cin >> N; + for (int i=0;i> line; + for (int j=0;j area_result; + for (int y=0;y > Q; + Q.push(make_pair(x, y)); + is_visit[y][x] = true; + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < N && 0 <= ny && ny < N && !is_visit[ny][nx] && map[ny][nx] == 1) { + area++; + is_visit[ny][nx] = true; + Q.push(make_pair(nx, ny)); + } + } + } + area_result.push_back(area); + } + } + } + sort(area_result.begin(), area_result.end()); + cout << count_result << '\n'; + for (int iter : area_result) { + cout << iter << '\n'; + } + + return 0; +} diff --git a/201902654/32068.cpp b/201902654/32068.cpp new file mode 100644 index 0000000..9d6bc62 --- /dev/null +++ b/201902654/32068.cpp @@ -0,0 +1,26 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int T; + cin >> T; + for (int i=0;i> L >> R >> S; + + int result = min((S - L) * 2 + 1, (R - S) * 2); + if (L == S || S == R) { + cout << 1 << '\n'; + } else { + cout << result << '\n'; + } + } + + return 0; +} diff --git a/201902654/3273.cpp b/201902654/3273.cpp new file mode 100644 index 0000000..33faef8 --- /dev/null +++ b/201902654/3273.cpp @@ -0,0 +1,34 @@ +#include +#include +#include + +using namespace std; + +int main(void) { + ios::sync_with_stdio(0); + cin.tie(0); + + int n; + cin >> n; + vector arr(2000001, 0); + for (int i=0;i> input; + ++arr[input]; + } + int x; + cin >> x; + + int result = 0; + if (x % 2 == 0) { + for (int i=1;i +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int N; + cin >> N; + int result = 0; + for (int i=0;i S; + string line; + cin >> line; + for (char ch : line) { + if (S.size() > 0 && S.top() == ch) { + S.pop(); + } else { + S.push(ch); + } + } + if (S.size() == 0) { + result++; + } + } + cout << result; + return 0; +} diff --git a/201902654/4179.cpp b/201902654/4179.cpp new file mode 100644 index 0000000..22c3c4b --- /dev/null +++ b/201902654/4179.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second + +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[1001][1001]; +int fire_time[1001][1001]; +int j_time[1001][1001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + for (int i=0;i<1001;i++) { + fill(fire_time[i], fire_time[i] + 1001, -1); + fill(j_time[i], j_time[i] + 1001, -1); + } + + int R, C; + int result = __INT_MAX__; + pair j_location, fire_location; + queue > Q; + cin >> R >> C; + for (int i=0;i> line; + for (int j=0;j top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < C && 0 <= ny && ny < R && board[ny][nx] != -1 && fire_time[ny][nx] == -1) { + fire_time[ny][nx] = fire_time[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + + Q.push(j_location); + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + if (top.X == 0 || top.X == C - 1 || top.Y == 0 || top.Y == R - 1) { + result = min(result, j_time[top.Y][top.X] + 1); + } + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < C && 0 <= ny && ny < R && board[ny][nx] != -1 && j_time[ny][nx] == -1 && (fire_time[ny][nx] == -1 || j_time[top.Y][top.X] + 1 < fire_time[ny][nx])) { + j_time[ny][nx] = j_time[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + + if (result == __INT_MAX__) { + cout << "IMPOSSIBLE"; + } else { + cout << result; + } + + return 0; +} + diff --git a/201902654/4949.cpp b/201902654/4949.cpp new file mode 100644 index 0000000..9cb0e6a --- /dev/null +++ b/201902654/4949.cpp @@ -0,0 +1,46 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + string line; + while (true) { + stack S; + bool is_error = false; + getline(cin, line); + if (line == ".") { + break; + } + for (char ch : line) { + if (ch == '(') { + S.push('('); + } + if (ch == ')') { + if (S.size() == 0 || S.top() != '(') { + is_error = true; + break; + } else { + S.pop(); + } + } + if (ch == '[') { + S.push('['); + } + if (ch == ']') { + if (S.size() == 0 || S.top() != '[') { + is_error = true; + break; + } else { + S.pop(); + } + } + } + cout << ((S.size() == 0 && !is_error) ? "yes" : "no") << '\n'; + + } + return 0; +} diff --git a/201902654/5014.cpp b/201902654/5014.cpp new file mode 100644 index 0000000..8959cac --- /dev/null +++ b/201902654/5014.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int F, S, G, U, D; +int button_count[1000001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> F >> S >> G >> U >> D; + fill(button_count, button_count+F+1, -1); + queue Q; + button_count[S] = 0; + Q.push(S); + while (!Q.empty()) { + int top = Q.front(); Q.pop(); + if (top + U <= F && button_count[top + U] == -1) { + button_count[top + U] = button_count[top] + 1; + Q.push(top + U); + } + if (top - D > 0 && button_count[top - D] == -1) { + button_count[top - D] = button_count[top] + 1; + Q.push(top - D); + } + } + if (button_count[G] == -1) { + cout << "use the stairs"; + } else { + cout << button_count[G]; + } + + return 0; +} diff --git a/201902654/5397.cpp b/201902654/5397.cpp new file mode 100644 index 0000000..5ee2306 --- /dev/null +++ b/201902654/5397.cpp @@ -0,0 +1,44 @@ +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int test_case; + cin >> test_case; + for (int i=0;i L; + auto cursor = L.end(); + cin >> input; + + for (char ch : input) { + if (ch == '<') { + if (cursor != L.begin()) { + cursor--; + } + } else if (ch == '>') { + if (cursor != L.end()) { + cursor++; + } + } else if (ch == '-') { + if (cursor != L.begin()) { + cursor--; + cursor = L.erase(cursor); + } + } else { + L.insert(cursor, ch); + } + } + + for (char ch : L) { + cout << ch; + } + cout << '\n'; + } + + return 0; +} diff --git a/201902654/5427.cpp b/201902654/5427.cpp new file mode 100644 index 0000000..84a9ce3 --- /dev/null +++ b/201902654/5427.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int board[1001][1001]; +int fire_time[1001][1001]; +int human_time[1001][1001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int T; + cin >> T; + for (int t=0;t human_position; + queue > Q; + int w, h; + cin >> w >> h; + for (int y=0;y> line; + for (int x=0;x top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < w && 0 <= ny && ny < h && board[ny][nx] == 0 && fire_time[ny][nx] == -1) { + fire_time[ny][nx] = fire_time[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + Q.push(human_position); + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + if (top.X == 0 || top.X == w - 1 || top.Y == 0 || top.Y == h - 1) { + result = human_time[top.Y][top.X] + 1; + break; + } + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < w && 0 <= ny && ny < h && board[ny][nx] == 0 && human_time[ny][nx] == -1 && (fire_time[ny][nx] == -1 || fire_time[ny][nx] > human_time[top.Y][top.X] + 1)) { + human_time[ny][nx] = human_time[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + if (result == -1) { + cout << "IMPOSSIBLE" << '\n'; + } else { + cout << result << '\n'; + } + } + + return 0; +} diff --git a/201902654/5430.cpp b/201902654/5430.cpp new file mode 100644 index 0000000..d380d71 --- /dev/null +++ b/201902654/5430.cpp @@ -0,0 +1,73 @@ +#include +#include +#include + +using namespace std; + +deque tokenize(string s, string del = ",") +{ + deque result; + int start, end = -1 * del.size(); + if (s.size() > 0) { + do { + start = end + del.size(); + end = s.find(del, start); + result.push_back(stoi(s.substr(start, end - start))); + } while (end != -1); + } + + return result; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int T; + cin >> T; + for (int i=0;i> commands; + cin >> n; + getline(cin >> ws, arr); + deque D = tokenize(arr.substr(1, arr.size() - 2), ","); + bool is_reversed = false; + bool is_error = false; + // cout << D.size() << '\n'; + for (char command : commands) { + if (command == 'R') { + is_reversed = !is_reversed; + } else { + if (D.size() == 0) { + cout << "error" << '\n'; + is_error = true; + break; + } + if (is_reversed) { + D.pop_back(); + } else { + D.pop_front(); + } + } + } + if (!is_error) { + cout << '['; + while (!D.empty()) { + if (is_reversed) { + cout << D.back(); + D.pop_back(); + } else { + cout << D.front(); + D.pop_front(); + } + if (!D.empty()) { + cout << ','; + } + } + cout << ']' << '\n'; + } + } + + return 0; +} diff --git a/201902654/6198.cpp b/201902654/6198.cpp new file mode 100644 index 0000000..b428257 --- /dev/null +++ b/201902654/6198.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int N; + cin >> N; + int arr[N+1], result[N]; + stack > stack; + fill(result, result + N, 0); + for (int i=0;i> arr[i]; + } + arr[N] = 1000000000; + for (int i=0;i +#include +#include +#include +#include +#include +#include + +using namespace std; + +int dx[6] = {0, 0, 1, -1, 0, 0}; +int dy[6] = {1, -1, 0, 0, 0, 0}; +int dz[6] = {0, 0, 0, 0, 1, -1}; +int L, R, C; +int building[31][31][31]; +int count_minutes[31][31][31]; +tuple s_position; +tuple e_position; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + while (true) { + cin >> L >> R >> C; + if (L == 0 && R == 0 && C == 0) { + break; + } + for (int z=0;z> line; + for (int x=0;x > Q; + Q.push(s_position); + count_minutes[get<2>(s_position)][get<1>(s_position)][get<0>(s_position)] = 0; + while (!Q.empty()) { + tuple top = Q.front(); Q.pop(); + for (int i=0;i<6;i++) { + int nx = get<0>(top) + dx[i]; + int ny = get<1>(top) + dy[i]; + int nz = get<2>(top) + dz[i]; + + if (0 <= nx && nx < C && 0 <= ny && ny < R && 0 <= nz && nz < L && building[nz][ny][nx] == 0 && count_minutes[nz][ny][nx] == -1) { + count_minutes[nz][ny][nx] = count_minutes[get<2>(top)][get<1>(top)][get<0>(top)] + 1; + Q.push(make_tuple(nx, ny, nz)); + } + } + } + if (count_minutes[get<2>(e_position)][get<1>(e_position)][get<0>(e_position)] == -1) { + cout << "Trapped!" << '\n'; + } else { + cout << "Escaped in " << count_minutes[get<2>(e_position)][get<1>(e_position)][get<0>(e_position)] << " minute(s)." << '\n'; + } + } + + return 0; +} diff --git a/201902654/7562.cpp b/201902654/7562.cpp new file mode 100644 index 0000000..6d48d73 --- /dev/null +++ b/201902654/7562.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1}; +int dy[8] = {-2, -1, 1, 2, 2, 1, -1, -2}; +int moving[301][301]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + int T; + cin >> T; + for (int t=0;t knight_position, dest_position; + queue > Q; + cin >> l; + cin >> x >> y; + knight_position = make_pair(x, y); + moving[y][x] = 0; + Q.push(make_pair(x, y)); + cin >> x >> y; + dest_position = make_pair(x, y); + + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + if (top == dest_position) { + break; + } + for (int i=0;i<8;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < l && 0 <= ny && ny < l && moving[ny][nx] == -1) { + moving[ny][nx] = moving[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + cout << moving[dest_position.Y][dest_position.X] << '\n'; + } + + return 0; +} diff --git a/201902654/7569.cpp b/201902654/7569.cpp new file mode 100644 index 0000000..c86a0a4 --- /dev/null +++ b/201902654/7569.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include + +using namespace std; + +int dx[6] = {0, 0, 1, -1, 0, 0}; +int dy[6] = {1, -1, 0, 0, 0, 0}; +int dz[6] = {0, 0, 0, 0, 1, -1}; +int days[101][101][101]; +int boards[101][101][101]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + for (int i=0;i<101;i++) { + for (int j=0;j<101;j++) { + fill(days[i][j], days[i][j]+101, -1); + } + } + + int M, N, H; + queue > Q; + cin >> M >> N >> H; + for (int h=0;h> boards[h][i][j]; + if (boards[h][i][j] == 1) { + Q.push(make_tuple(j, i, h)); + days[h][i][j] = 0; + } + } + } + } + while (!Q.empty()) { + tuple top = Q.front(); Q.pop(); + for (int i=0;i<6;i++) { + int nx = get<0>(top) + dx[i]; + int ny = get<1>(top) + dy[i]; + int nz = get<2>(top) + dz[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N && 0 <= nz && nz < H && boards[nz][ny][nx] != -1 && days[nz][ny][nx] == -1) { + days[nz][ny][nx] = days[get<2>(top)][get<1>(top)][get<0>(top)] + 1; + Q.push(make_tuple(nx, ny, nz)); + } + } + } + int result = 0; + for (int h=0;h +#include +#include +#include +#include +#include + +using namespace std; +#define X first +#define Y second +int dx[4] = {0, 0, 1, -1}; +int dy[4] = {1, -1, 0, 0}; +int days[1001][1001]; +int boards[1001][1001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + for (int i=0;i<1001;i++) { + fill(days[i], days[i]+1001, -1); + } + + int M, N; + queue > Q; + cin >> M >> N; + for (int i=0;i> boards[i][j]; + if (boards[i][j] == 1) { + Q.push(make_pair(j, i)); + days[i][j] = 0; + } + } + } + while (!Q.empty()) { + pair top = Q.front(); Q.pop(); + for (int i=0;i<4;i++) { + int nx = top.X + dx[i]; + int ny = top.Y + dy[i]; + if (0 <= nx && nx < M && 0 <= ny && ny < N && boards[ny][nx] != -1 && days[ny][nx] == -1) { + days[ny][nx] = days[top.Y][top.X] + 1; + Q.push(make_pair(nx, ny)); + } + } + } + int result = 0; + for (int i=0;i +#include +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + int T; + cin >> T; + for (int i=0;i S; + bool is_error = false; + string line; + cin >> line; + + for (char ch : line) { + if (ch == '(') { + S.push(ch); + } else { + if (S.size() == 0 || S.top() != '(') { + is_error = true; + break; + } else { + S.pop(); + } + } + } + cout << ((S.size() == 0 && !is_error) ? "YES" : "NO") << '\n'; + } + + return 0; +} diff --git a/201902654/9466.cpp b/201902654/9466.cpp new file mode 100644 index 0000000..dec77d9 --- /dev/null +++ b/201902654/9466.cpp @@ -0,0 +1,53 @@ +#include + +using namespace std; +int T; +int select_num[100001], connecting[100001]; +bool is_visit[100001]; + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + cin >> T; + for (int t=0;t Q; + cin >> n; + for (int i=0;i> select_num[i]; + } + for (int i=0;i