백준(C, C++) 40

백준 14939: 불 끄기

https://www.acmicpc.net/problem/14939 14939번: 불 끄기 전구 100개가 10×10 정사각형 모양으로 늘어서 있다. 전구에 달린 스위치를 누르면 그 전구와 위, 아래, 왼쪽, 오른쪽에 있는 전구의 상태도 바뀐다. 전구 100개의 상태가 주어지면 모든 전구를 끄 www.acmicpc.net 코드 #include #include using namespace std; #define fastio() ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int ans[10], res = 1111111; void dfs(int y, int x, int before, int now, int then, int cnt) { if (x == 10) { if..

백준 1047: 울타리 [C/C++]

https://www.acmicpc.net/problem/1047 1047번: 울타리 첫째 줄에 N이 주어진다. N은 2보다 크거나 같고, 40보다 작거나 같은 자연수이다. 둘째 줄부터 N개의 줄에 각 나무가 심어져 있는 위치와 그 나무로 만들 수 있는 울타리의 길이가 순서대로 주어 www.acmicpc.net 코드 #include #include #include static const auto fastio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }(); using namespace std; typedef struct Tree { int y, x, v, n; } tr..

백준 22116: 창영이와 퇴근 [C/C++]

https://www.acmicpc.net/problem/22116 22116번: 창영이와 퇴근 A1,1에서 AN,N까지, 경로상의 최대 경사의 최솟값을 출력한다. www.acmicpc.net #include static const auto fastio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }(); using namespace std; int n; int grid[1111][1111]; int visited[1111][1111]; int dy[4] = {0, 1, 0, -1}; int dx[4] = {1, 0, -1, 0}; int dp[1111][1111]; in..

백준 4342: 유클리드 게임 [C/C++]

https://www.acmicpc.net/problem/4342 4342번: 유클리드 게임 유클리드 게임은 두 명이서 하는 게임이고, 자연수 2개로 시작한다. 동혁이와 동규는 유클리드 게임을 하려고 한다. 동혁이가 먼저 시작한다. 동혁이는 큰 수를 작은 수의 배수만큼 뺀다. 이때, www.acmicpc.net 코드 #include #define endl "\n" #define ll long long static const auto fastio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; }(); using namespace std; ll GCD(ll a, ll b){ i..

백준 1863: 스카이라인 쉬운거 [C/C++]

https://www.acmicpc.net/problem/1863 1863번: 스카이라인 쉬운거 첫째 줄에 n이 주어진다. (1 ≤ n ≤ 50,000) 다음 n개의 줄에는 왼쪽부터 스카이라인을 보아 갈 때 스카이라인의 고도가 바뀌는 지점의 좌표 x와 y가 주어진다. (1 ≤ x ≤ 1,000,000. 0 ≤ y ≤ 500,000) 첫 www.acmicpc.net #include #include using namespace std; int n, t, ans; int m[55555]; static const auto fastio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 0; ..

백준 2624: 동전 바꿔주기 [C/C++]

https://www.acmicpc.net/problem/2624 2624번: 동전 바꿔주기 명보네 동네 가게의 현금 출납기에는 k 가지 동전이 각각 n1, n2, … , nk개 씩 들어있다. 가게 주인은 명보에게 T원의 지폐를 동전으로 바꿔 주려고 한다. 이때, 동전 교환 방법은 여러 가지가 있을 www.acmicpc.net 코드 #include using namespace std; int n, k; int c[111], m[111]; int dp[11111]; int main() { cin >> n >> k; for (int i = 0; i > c[i] >> m[i]; dp[0] = 1; for (int i = 0; i = 0; j--) { if (dp[j]) { for (int l = 1; l

백준 1052: 물병 [C/C++]

https://www.acmicpc.net/problem/1052 1052번: 물병 지민이는 N개의 물병을 가지고 있다. 각 물병에는 물을 무한대로 부을 수 있다. 처음에 모든 물병에는 물이 1리터씩 들어있다. 지민이는 이 물병을 또 다른 장소로 옮기려고 한다. 지민이는 한 번 www.acmicpc.net 코드 Z#include #include #include using namespace std; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int N, K; cin >> N >> K; K--; vector arr = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384,..

백준 1041: 주사위

https://www.acmicpc.net/problem/1041 1041번: 주사위 첫째 줄에 N이 주어진다. 둘째 줄에 주사위에 쓰여 있는 수가 주어진다. 위의 그림에서 A, B, C, D, E, F에 쓰여 있는 수가 차례대로 주어진다. N은 1,000,000보다 작거나 같은 자연수이고, 쓰여 있는 수 www.acmicpc.net 코드 #include #include #define ll unsigned long long using namespace std; int N; ll a, b, c, d, e, f; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin >> N; cin >> a >> b >> c >> d >> e >> f; ll m..

백준 23253: 자료구조는 정말 최고야

https://www.acmicpc.net/problem/23253 23253번: 자료구조는 정말 최고야 위 그림처럼 책이 쌓여 있으므로, 첫 번째 더미 - 두 번째 더미 - 첫 번째 더미 - 두 번째 더미 순으로 꺼내면 책 번호순으로 나열할 수 있다. www.acmicpc.net #include #include #include using namespace std; int m, n, k, t; int lis[200000]; priority_queue pq; int main() { scanf("%d %d", &n, &m); for (int i = 0; i = 0; j--) { scanf("%d", &t); pq.push(make_pair(-t, make_pair(i, j))); } } while (!pq...

백준 23832: 서로소 그래프

https://www.acmicpc.net/problem/23832 23832번: 서로소 그래프 우석이는 심심할 때마다 그래프를 그린다. 우석이는 매달 새로운 그래프를 그리는데, 이번 달에는 서로소 그래프를 그린다. 서로소 그래프는 $1$부터 $N$까지의 번호를 가진 $N$ 개의 정점으로 이 www.acmicpc.net 코드 #include using namespace std; #define ull unsigned long long int ull Power(__int128 x, __int128 y, __int128 mod) { x %= mod; ull ret = 1; while(y > 0) { if(y%2 == 1) ret = (ret*x)%mod; x = (x*x)%mod; y /= 2; } retur..