Skip to content

Commit 48e6966

Browse files
authored
명환 숙제(04132021) (#61)
* 명환 백준 숙제 * 명환 책 숙제 * 2667 수정
1 parent fe58e95 commit 48e6966

File tree

5 files changed

+314
-0
lines changed

5 files changed

+314
-0
lines changed

mhkim/baekjoon/11724.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file 11724.cpp
3+
* @brief 연결 요소의 개수
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* BFS, 인접리스트
7+
* DFS로 풀 수 있지 않을까?
8+
*/
9+
#include <iostream>
10+
#include <vector>
11+
#include <queue>
12+
13+
using namespace std;
14+
15+
#define N 1001
16+
17+
vector<int> g[N];
18+
bool visited[N];
19+
20+
void bfs(int start)
21+
{
22+
visited[start] = true;
23+
queue<int> q;
24+
q.push(start);
25+
26+
while (!q.empty())
27+
{
28+
int p = q.front();
29+
q.pop();
30+
31+
for (int i = 0; i < g[p].size(); i++)
32+
{
33+
if (!visited[g[p][i]])
34+
{
35+
visited[g[p][i]] = true;
36+
q.push(g[p][i]);
37+
}
38+
}
39+
}
40+
}
41+
42+
int main()
43+
{
44+
ios_base::sync_with_stdio(false);
45+
cin.tie(nullptr);
46+
47+
int n, m;
48+
cin >> n >> m;
49+
50+
for (int i = 1; i <= m; i++)
51+
{
52+
int u, v;
53+
cin >> u >> v;
54+
g[u].push_back(v);
55+
g[v].push_back(u);
56+
}
57+
58+
int ans = 0;
59+
for (int i = 1; i <= n; i++)
60+
{
61+
if (!visited[i])
62+
{
63+
bfs(i);
64+
ans++;
65+
}
66+
}
67+
68+
cout << ans << '\n';
69+
70+
return 0;
71+
}

mhkim/baekjoon/2667.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @file 2667.cpp
3+
* @brief 단지번호붙이기
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* DFS
7+
* 음료수 얼려먹기 문제랑 비슷
8+
*
9+
* 백준 테스트 케이스 맞왜틀? -> 입출력 주석제거해서 통과
10+
*/
11+
#include <iostream>
12+
#include <vector>
13+
#include <algorithm>
14+
#include <utility>
15+
16+
using namespace std;
17+
18+
int n, cnt;
19+
int map[26][26];
20+
bool visited[26][26] = {
21+
false,
22+
};
23+
vector<int> cnts;
24+
pair<int, int> dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
25+
26+
void dfs(int x, int y)
27+
{
28+
cnt++;
29+
visited[x][y] = true;
30+
31+
for (int i = 0; i < 4; i++)
32+
{
33+
int newX = x + dir[i].first;
34+
int newY = y + dir[i].second;
35+
36+
if (newX >= 1 && newX <= n && newY >= 1 && newY <= n)
37+
{
38+
if (!visited[newX][newY] && map[newX][newY] == 1)
39+
{
40+
dfs(newX, newY);
41+
}
42+
}
43+
}
44+
}
45+
46+
int main()
47+
{
48+
// ios_base::sync_with_stdio(false);
49+
// cin.tie(nullptr);
50+
51+
cin >> n;
52+
for (int i = 1; i <= n; i++)
53+
{
54+
for (int j = 1; j <= n; j++)
55+
{
56+
scanf("%1d", &map[i][j]);
57+
}
58+
}
59+
60+
for (int i = 1; i <= n; i++)
61+
{
62+
for (int j = 1; j <= n; j++)
63+
{
64+
if (!visited[i][j] && map[i][j] == 1)
65+
{
66+
cnt = 0;
67+
dfs(i, j);
68+
cnts.push_back(cnt);
69+
}
70+
}
71+
}
72+
73+
sort(cnts.begin(), cnts.end());
74+
75+
cout << cnts.size() << '\n';
76+
for (int i = 0; i < cnts.size(); i++)
77+
{
78+
cout << cnts[i] << '\n';
79+
}
80+
81+
return 0;
82+
}

mhkim/greedy-4.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @file greedy-4.cpp
3+
* @brief 만들 수 없는 금액
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* 아이디어 생각하기 어려움
7+
*/
8+
#include <iostream>
9+
10+
using namespace std;
11+
12+
int coins[1001];
13+
14+
int main()
15+
{
16+
ios_base::sync_with_stdio(false);
17+
cin.tie(nullptr);
18+
19+
int n;
20+
cin >> n;
21+
for (int i = 0; i < n; i++)
22+
{
23+
cin >> coins[i];
24+
}
25+
26+
sort(coins, coins + n);
27+
28+
int target = 1;
29+
// 아이디어!
30+
for (int i = 0; i < n; i++)
31+
{
32+
33+
if (target < coins[i])
34+
{
35+
break;
36+
}
37+
target += coins[i];
38+
}
39+
40+
cout << target << '\n';
41+
42+
return 0;
43+
}

mhkim/greedy-5.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @file greedy-5.cpp
3+
* @brief 볼링공 고르기
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*/
6+
#include <iostream>
7+
8+
using namespace std;
9+
10+
int balls[1000];
11+
12+
int main()
13+
{
14+
ios_base::sync_with_stdio(false);
15+
cin.tie(nullptr);
16+
17+
int n, m;
18+
cin >> n >> m;
19+
for (int i = 0; i < n; i++)
20+
{
21+
cin >> balls[i];
22+
}
23+
24+
sort(balls, balls + n);
25+
26+
int count = 0;
27+
for (int i = 0; i < n - 1; i++)
28+
{
29+
for (int j = i + 1; j < n; j++)
30+
{
31+
if (balls[i] != balls[j])
32+
{
33+
count++;
34+
}
35+
}
36+
}
37+
38+
cout << count << '\n';
39+
40+
return 0;
41+
}

mhkim/greedy-6.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* @file greedy-6.cpp
3+
* @brief 무지의 먹방 라이브
4+
* @author Sam Kim (samkim2626@gmail.com)
5+
*
6+
* 1. 처음에 tni 음식 시간, 음식 인덱스 pair로 만들어서 정렬함 -> 우선순위큐로
7+
* 2. 우선순위큐 top이 시간이 가장 적게 걸리는 음식 -> 계산해서 k에서 빼기, 우선순위큐에서 빼기
8+
* 3. 다음 반복에서 계산했을 때 k보다 크면 빼지 않고 다음에 먹어야 할 음식 인덱스 출력
9+
*/
10+
#include <iostream>
11+
#include <vector>
12+
#include <string>
13+
#include <algorithm>
14+
#include <numeric>
15+
#include <utility>
16+
#include <queue>
17+
18+
using namespace std;
19+
20+
int solution(vector<int> food_times, long long k)
21+
{
22+
int answer = 0;
23+
24+
long long sum = accumulate(food_times.begin(), food_times.end(), 0LL);
25+
if (sum < k)
26+
{
27+
return -1;
28+
}
29+
30+
// 정렬 전 time과 index를 저장
31+
priority_queue<pair<int, int>> tni;
32+
for (int i = 0; i < food_times.size(); i++)
33+
{
34+
tni.push(make_pair(-food_times[i], i + 1)); // 파이썬은 +
35+
}
36+
37+
int nFoods = food_times.size(); // 남은 음식의 개수
38+
int sumTime = 0; // 먹는데 걸린 총 시간
39+
while (1)
40+
{
41+
pair<int, int> top = tni.top();
42+
43+
if (sumTime + (-top.first) * nFoods > k)
44+
{
45+
answer = (k - sumTime) % nFoods break;
46+
}
47+
48+
sumTime += (-top.first) * nFoods;
49+
tni.pop();
50+
nFoods--;
51+
}
52+
53+
return answer;
54+
}
55+
56+
int main()
57+
{
58+
ios_base::sync_with_stdio(false);
59+
cin.tie(nullptr);
60+
61+
int n;
62+
cin >> n;
63+
vector<int> food_times;
64+
for (int i = 0; i < n; i++)
65+
{
66+
int time;
67+
cin >> time;
68+
food_times.push_back(time);
69+
}
70+
71+
long long k;
72+
cin >> k;
73+
74+
cout << solution(food_times, k) << '\n';
75+
76+
return 0;
77+
}

0 commit comments

Comments
 (0)