Algorithm 26

백준 6579번 [히스토그램에서 가장 큰 직사각형]

구간 전체를 한 번에 처리하기 어렵다.최소 높이를 기준으로 좌우로 나눌 수 있다.ㅡ> 분할 정복 시도vector arr;ll sol(int l, int r){ if(l==r) return arr[l]; int mid = (l+r)/2; ll lmax = sol(l,mid); ll rmax = sol(mid+1, r); ll ret = max(lmax, rmax); int b = mid; int e = mid+1; ll y = min(arr[b], arr[e]); ll pmax = 2*y; while(b>l || e arr[e+1]){ b--; } else{ e++; } y=min(y, min(arr[b], arr[e])); pmax = max(pmax, (e-b+1)*y); } ret =..

Algorithm/PS 2026.02.26

Segment Tree

Segment Tree 사용 조건1. 연산이 결합 법칙을 만족2. 연산에 항등원이 존재 - 리프노드에 원본 데이터를 입력vector tree;int main() { int n; cin >> n; int height = 0; while((int)pow(2, height) > tree[i]; } build(treeSize-1); //update(node + leftNodeIndex - 1, val); //query(start + leftNodeIndex - 1, end + leftNodeIndex - 1) return 0;} - build, query, update 함수를 연산에 따라 작성//구간합void build(int i){ while(i>1){ tree[i/2] += tree[i]; i--; }}..

Algorithm/PS 2026.02.26

백준 1948번 [임계경로]

위상정렬로 최장 경로를 구한 후 해당 경로 찾기그래프에서 역추적 + DP로 경로 구하기vector world[10001];vector reverse_world[10001];bool visited[10001] = {false,};int idx[10001] = {0,};int dis[10001] = {0,};int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, m; cin >> n >> m; for(int i=0;i> u >> v >> w; world[u].push_back({v, w}); reverse_world[v].push_back({u, w}); idx[v]++; } int start, end; cin >>..

Algorithm/PS 2026.02.21

백준 1516번 [게임 개발]

사이클 없는 그래프에서 선후관계를 따질 때 ㅡ> 위상정렬(먼저 해야한다, 의존 관계, 선행 작업...)vector v[501];int idx[501] = {0,};int work_time[501] = {0,};int final_time[501] = {0,};int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for(int i=1;i> t; work_time[i] = t; int b = 0; while(b!=-1){ cin >> b; if(b!=-1) { v[b].push_back(i); idx[i]++; } } } queue q; for(int i=1;i

Algorithm/PS 2026.02.21

백준 2250번 [물통]

그래프 데이터가 없는 bfs 문제. 문제 흐름에 따라 그래프를 역으로 그리는 방식.가능한 모든 상태를 탐색해야 할 때 ㅡ> 그래프 탐색 고려int sender[6] = {0, 0, 1, 1, 2, 2};int receiver[6] = {1, 2, 0, 2, 0, 1};bool visited[201][201];bool answer[201];int bottle[3];void bfs(){ queue q; q.push({0, 0}); visited[0][0] = true; answer[bottle[2]] = true; while(!q.empty()){ pii now = q.front(); q.pop(); int a = now.first; int b = now.second; int c = bottle[2..

Algorithm/PS 2026.02.19