diff --git a/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp new file mode 100644 index 0000000..6995c7d --- /dev/null +++ b/4. Combinatorial search/APSS_ALLERGY_minkyu.cpp @@ -0,0 +1,78 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; +int T, n, m; +int MAX_DEPTH; +// nameIndex[name] : table에서 해당 이름을 가진 사람의 행 번호 저장 +map nameIndex; +// foodEater[i] : i번째 음식을 먹을 수 있는 사람들의 리스트 +matrix foodEater; +// eaterFood[i] : i번째 사람이 먹을 수 있는 음식 리스트 +matrix eaterFood; +// 현재 상태 +vector edible; + +int search(int start) { + return distance(edible.begin(), find(edible.begin(), edible.end(), 0)); +} + +void select(int food) { + for (int saram: foodEater[food]) { + edible[saram]++; + } +} + +void unselect(int food) { + for (int saram: foodEater[food]) { + edible[saram]--; + } +} + +// start 번 음식부터 시작 +void solve(int start, int num) { + if (num >= MAX_DEPTH) return; + if (find(edible.begin(), edible.end(), 0) == edible.end()) { + MAX_DEPTH = num; + return; + } + int inedible = search(start); + for (int food: eaterFood[inedible]) { + select(food); + solve(food+1, num+1); + unselect(food); + } +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> T; + while(T--) { + MAX_DEPTH = 21; + cin >> n >> m; + foodEater = matrix(m); + eaterFood = matrix(n); + edible = vector(n, 0); + for (int i = 0; i < n; i++) { + string name; + cin >> name; + nameIndex[name] = i; + } + for (int i = 0; i < m; i++) { + int num; + cin >> num; + while(num--) { + string name; + cin >> name; + int person = nameIndex[name]; + foodEater[i].push_back(person); + eaterFood[person].push_back(i); + } + } + solve(0, 0); + cout << MAX_DEPTH << endl; + } +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" new file mode 100644 index 0000000..258c862 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_ARTIC_minkyu.cpp" @@ -0,0 +1,70 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; + +struct Point { + double x, y; +}; + +int C, n; +vector base; +matrix dist; +vector visited; +vector decisioned; + +bool decision(int here, double gap) { + decisioned[here] = true; + visited[here] = true; + for (int i = 0; i < n; i++) { + if (dist[here][i] < gap) { + visited[i] = true; + } + } + if (find(visited.begin(), visited.end(), 0) != visited.end()) { + for (int i = 0; i < n; i++) { + if (visited[i] && !decisioned[i]) return decision(i, gap); + } + return false; + } + return true; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cout << fixed; + cout.precision(2); + cin >> C; + while(C--) { + cin >> n; + base = vector(); + dist = matrix(n, vector(n, 0)); + for (int i = 0; i < n; i++) { + double x, y; + cin >> x >> y; + base.push_back(Point{x, y}); + } + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + if (i == j) continue; + double gap = pow(pow(abs(base[i].x-base[j].x), 2) + pow(abs(base[i].y-base[j].y), 2), 0.5); + dist[i][j] = gap; + dist[j][i] = gap; + } + } + + double lo = 0, hi = 2e6+1; + double mid; + for (int i = 0; i < 100; i++) { + visited = vector(n, 0); + decisioned = vector(n, 0); + mid = (lo + hi) / 2.0; + if (decision(0, mid)) hi = mid; + else lo = mid; + } + cout << mid << endl; + } +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" new file mode 100644 index 0000000..f92033b --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/APSS_DARPA_minkyu.cpp" @@ -0,0 +1,44 @@ +#include +#define endl '\n' + +using namespace std; + +int C, N, M; +vector settable; + +bool decision(int cameras, double gap) { + double limit = -1; + int installed = 0; + for (int i = 0; i < settable.size(); i++) { + if (limit <= settable[i]) { + installed++; + limit = settable[i] + gap; + } + } + return installed >= cameras; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> C; + cout << fixed; + cout.precision(2); + while (C--) { + cin >> N >> M; + settable = vector(M); + for (int i = 0; i < M; i++) { + cin >> settable[i]; + } + cout << endl; + double lo = 0, hi = 241; + double mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2.0; + if(decision(N, mid)) lo = mid; + else hi = mid; + } + cout << lo << endl; + } +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" new file mode 100644 index 0000000..3a1f75e --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1477_minkyu.cpp" @@ -0,0 +1,49 @@ +#include +#define endl '\n' + +using namespace std; + +int N, M, L; +vector station; +vector distances; + +// 모든 간격이 low 이상으로 가능? +bool decision(double low) { + int howmany = 0; + for (int d: distances) { + howmany += ceil(d / low)-1; + } + return howmany <= M; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M >> L; + if (N == 0) { + cout << ceil(L/(double)(M+1)) << endl; + return 0; + } + station = vector(N); + distances = vector(N+1); + for (int i = 0; i < N; i++) cin >> station[i]; + sort(station.begin(), station.end()); + distances[0] = station[0]; + for (int i = 1; i < N; i++) { + distances[i] = station[i] - station[i-1]; + } + distances[N] = L - station[N-1]; + + double lo = -1, hi = 2001; + double mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + if (decision(mid)) hi = mid; + else lo = mid; + } + + // 반드시 올림 해야됨 + cout << ceil(mid) << endl; +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" new file mode 100644 index 0000000..4d2ce2b --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1561_minkyu.cpp" @@ -0,0 +1,54 @@ +#include +#define endl '\n' + +using namespace std; + +int N, M; +vector rides; + +bool decision(long long n) { + long long ret = 0; + for (int i = 1; i <= M; i++) { + ret += ceil(n / (double)rides[i]); + } + return ret >= N; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M; + rides = vector(M+1); + for (int i = 1; i <= M; i++) cin >> rides[i]; + + if (M >= N) { + cout << N; + return 0; + } + + // 아래 for문이 끝나면 모든 아이들이 빠짐없이 놀이기구를 타는데 hi분이 걸리는 것을 알 수 있음 + long long lo = 0; + long long hi = 1e15; + long long mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + if (decision(mid)) hi = mid; + else lo = mid; + } + + // 이제 hi분을 기준으로 가장 마지막에 탄 놀이기구가 무엇인지 찾으면 됨 + long long child = 0; + for (int i = 1; i <= M; i++) { + child += ceil(mid / (double)rides[i]); + } + for (int i = 1; i <= M; i++) { + if (mid % rides[i] == 0) { + if (++child == N) { + cout << i << endl; + return 0; + } + } + } +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" new file mode 100644 index 0000000..187a7d1 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_1939_minkyu.cpp" @@ -0,0 +1,51 @@ +#include +#define endl '\n' + +using namespace std; +using matrix = vector >; + +int N, M; +int A, B, C; +matrix bridge; +int f1, f2; + +bool DFS(int start, int weight, vector& visited) { + if (start == f2) return true; + visited[start] = true; + + bool ret = false; + for (pair next: bridge[start]) { + if (!visited[next.first] && next.second >= weight) ret = DFS(next.first, weight, visited); + if (ret) return true; + } + + return false; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N >> M; + bridge = matrix(N+1); + + for (int i = 0; i < M; i++) { + cin >> A >> B >> C; + int& temp = bridge[A][B]; + temp = max(temp, C); + bridge[B][A] = temp; + } + cin >> f1 >> f2; + + + int lo = -1, hi = 1e9+1; + int mid; + for (int i = 0; i < 100; i++) { + mid = (lo + hi) / 2; + vector visited (N+1, false); + if(DFS(f1, mid, visited)) lo = mid; + else hi = mid; + } + cout << mid << endl; +} \ No newline at end of file diff --git "a/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" new file mode 100644 index 0000000..e584669 --- /dev/null +++ "b/5. \354\265\234\354\240\201\355\231\224 \352\262\260\354\240\225 \353\254\270\354\240\234/BJ_2470_minkyu.cpp" @@ -0,0 +1,51 @@ +#include +#define endl '\n' + +using namespace std; + +int N; +vector liquid; + +bool decision(int sum) { + + return true; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> N; + liquid = vector(N); + for (int i = 0; i < N; i++) cin >> liquid[i]; + + sort(liquid.begin(), liquid.end()); + + vector::iterator i = liquid.begin(), j = liquid.end()-1; + vector::iterator left = liquid.begin(), right = liquid.end()-1; + int gap = *i + *j; + int mingap = abs(gap); + while (i < j-1) { + if (gap > 0) { + j--; + gap = *i + *j; + if (mingap > abs(gap)) { + mingap = abs(gap); + left = i; + right = j; + } + } + else if (gap < 0) { + i++; + gap = *i + *j; + if (mingap > abs(gap)) { + mingap = abs(gap); + left = i; + right = j; + } + } + else break; + } + cout << *left << ' ' << *right << endl; +} \ No newline at end of file diff --git a/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp b/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp new file mode 100644 index 0000000..b837a02 --- /dev/null +++ b/6. BFS, DFS/APSS_DICTIONARY_minkyu.cpp @@ -0,0 +1,90 @@ +#include +#define endl '\n' + +using namespace std; + +int C, N; +vector > G; +vector inorder; +list result; +int numAlpha; + +int pop(list& q) { + int ret = q.back(); + q.pop_back(); + return ret; +} + +bool toplogical() { + list q; + for (int i = 0; i < 26; i++) { + if (inorder[i] == 0) q.push_back(i); + } + while(q.size() > 0) { + int here = pop(q); + for (int there: G[here]) { + if(--inorder[there] == 0) q.push_back(there); + } + result.push_back(here+'a'); + --numAlpha; + } + return numAlpha == 0; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + + cin >> C; + while (C--) { + G = vector >(26); + inorder = vector(26, -1); + result.clear(); + numAlpha = 0; + + cin >> N; + string before; + string now; + cin >> before; + + while (--N) { + cin >> now; + int size = min(before.size(), now.size()); + for (int i = 0; i < size; i++) { + if (before[i] != now[i]) { + int from = before[i] - 'a'; + int to = now[i] - 'a'; + G[from].push_back(to); + if (inorder[from] < 0) inorder[from] = 0; + if (inorder[to] < 0) { + inorder[to] = 1; + } + else ++inorder[to]; + break; + } + } + before = now; + } + + for (int i = 0; i < 26; ++i) { + if (inorder[i] < 0) { + result.push_back(i+'a'); + continue; + } + ++numAlpha; + } + + bool valid = toplogical(); + + if (valid) { + for (char c: result) cout << c; + cout << endl; + continue; + } else { + cout << "INVALID HYPOTHESIS" << endl; + continue; + } + + } +} \ No newline at end of file diff --git a/6. BFS, DFS/BJ_2021_minkyu.cpp b/6. BFS, DFS/BJ_2021_minkyu.cpp new file mode 100644 index 0000000..f4fc1fb --- /dev/null +++ b/6. BFS, DFS/BJ_2021_minkyu.cpp @@ -0,0 +1,76 @@ +#include +#define endl '\n' + +using namespace std; +int N, L, src, dst; +vector > hosun; +vector > jiha; +vector hosunSeen; +vector jihaSeen; + +int pop(vector& q) { + int ret = q.back(); + q.pop_back(); + return ret; +} + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> N >> L; + hosun = vector >(L+1); + hosunSeen = vector(L+1, false); + jiha = vector >(N+1); + jihaSeen = vector(N+1, false); + + for (int i = 0; i < L; ++i) { + int j; + cin >> j; + while(j > 0) { + hosun[i].push_back(j); + jiha[j].push_back(i); + cin >> j; + } + } + + cin >> src >> dst; + + int here = src; + vector q1; + vector q2; + int minimumTransfer = 0; + + for(int e: jiha[here]) { + q1.push_back(e); + } + + for (; ; ++minimumTransfer) { + if (q1.empty()) { + cout << -1 << endl; + return 0; + } + while(!q1.empty()) { + int hosun_num = pop(q1); + if (hosunSeen[hosun_num]) continue; + for (int jiha_num: hosun[hosun_num]) { + if (jihaSeen[jiha_num]) continue; + if (jiha_num == dst) goto FINISH; + q2.push_back(jiha_num); + } + hosunSeen[hosun_num] = true; + } + while(!q2.empty()) { + here = pop(q2); + if (jihaSeen[here]) continue; + for(int e: jiha[here]) { + if (hosunSeen[e]) continue; + q1.push_back(e); + } + jihaSeen[here] = true; + } + } + FINISH: + cout << minimumTransfer << endl; + return 0; +} \ No newline at end of file diff --git a/6. BFS, DFS/BJ_3584_minkyu.cpp b/6. BFS, DFS/BJ_3584_minkyu.cpp new file mode 100644 index 0000000..d06ea76 --- /dev/null +++ b/6. BFS, DFS/BJ_3584_minkyu.cpp @@ -0,0 +1,48 @@ +#include +#define endl '\n' + +using namespace std; + +int T, N; +vector parentOf; +vector seen; + +int main() { + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + cout.tie(nullptr); + cin >> T; + while(T--) { + cin>> N; + parentOf = vector(N+1, 0); + seen = vector(N+1, false); + int parent, child; + for (int i = 1; i < N; ++i) { + cin >> parent >> child; + parentOf[child] = parent; + } + int A, B; + cin >> A >> B; + parent = A; + while (parent) { + if (parent == B) { + cout << parent << endl; + goto FINISH; + } + seen[parent] = true; + parent = parentOf[parent]; + } + + parent = B; + while(parent) { + if (seen[parent]) { + cout << parent << endl; + goto FINISH; + } + parent = parentOf[parent]; + } + + FINISH: + continue; + } +} \ No newline at end of file