Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions mhkim/baekjoon/11724.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* @file 11724.cpp
* @brief 연결 요소의 개수
* @author Sam Kim (samkim2626@gmail.com)
*
* BFS, 인접리스트
* DFS로 풀 수 있지 않을까?
*/
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

#define N 1001

vector<int> g[N];
bool visited[N];

void bfs(int start)
{
visited[start] = true;
queue<int> q;
q.push(start);

while (!q.empty())
{
int p = q.front();
q.pop();

for (int i = 0; i < g[p].size(); i++)
{
if (!visited[g[p][i]])
{
visited[g[p][i]] = true;
q.push(g[p][i]);
}
}
}
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n, m;
cin >> n >> m;

for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}

int ans = 0;
for (int i = 1; i <= n; i++)
{
if (!visited[i])
{
bfs(i);
ans++;
}
}

cout << ans << '\n';

return 0;
}
82 changes: 82 additions & 0 deletions mhkim/baekjoon/2667.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @file 2667.cpp
* @brief 단지번호붙이기
* @author Sam Kim (samkim2626@gmail.com)
*
* DFS
* 음료수 얼려먹기 문제랑 비슷
*
* 백준 테스트 케이스 맞왜틀? -> 입출력 주석제거해서 통과
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int n, cnt;
int map[26][26];
bool visited[26][26] = {
false,
};
vector<int> cnts;
pair<int, int> dir[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

void dfs(int x, int y)
{
cnt++;
visited[x][y] = true;

for (int i = 0; i < 4; i++)
{
int newX = x + dir[i].first;
int newY = y + dir[i].second;

if (newX >= 1 && newX <= n && newY >= 1 && newY <= n)
{
if (!visited[newX][newY] && map[newX][newY] == 1)
{
dfs(newX, newY);
}
}
}
}

int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);

cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
scanf("%1d", &map[i][j]);
}
}

for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (!visited[i][j] && map[i][j] == 1)
{
cnt = 0;
dfs(i, j);
cnts.push_back(cnt);
}
}
}

sort(cnts.begin(), cnts.end());

cout << cnts.size() << '\n';
for (int i = 0; i < cnts.size(); i++)
{
cout << cnts[i] << '\n';
}

return 0;
}
43 changes: 43 additions & 0 deletions mhkim/greedy-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @file greedy-4.cpp
* @brief 만들 수 없는 금액
* @author Sam Kim (samkim2626@gmail.com)
*
* 아이디어 생각하기 어려움
*/
#include <iostream>

using namespace std;

int coins[1001];

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> coins[i];
}

sort(coins, coins + n);

int target = 1;
// 아이디어!
for (int i = 0; i < n; i++)
{

if (target < coins[i])
{
break;
}
target += coins[i];
}

cout << target << '\n';

return 0;
}
41 changes: 41 additions & 0 deletions mhkim/greedy-5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @file greedy-5.cpp
* @brief 볼링공 고르기
* @author Sam Kim (samkim2626@gmail.com)
*/
#include <iostream>

using namespace std;

int balls[1000];

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> balls[i];
}

sort(balls, balls + n);

int count = 0;
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (balls[i] != balls[j])
{
count++;
}
}
}

cout << count << '\n';

return 0;
}
77 changes: 77 additions & 0 deletions mhkim/greedy-6.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* @file greedy-6.cpp
* @brief 무지의 먹방 라이브
* @author Sam Kim (samkim2626@gmail.com)
*
* 1. 처음에 tni 음식 시간, 음식 인덱스 pair로 만들어서 정렬함 -> 우선순위큐로
* 2. 우선순위큐 top이 시간이 가장 적게 걸리는 음식 -> 계산해서 k에서 빼기, 우선순위큐에서 빼기
* 3. 다음 반복에서 계산했을 때 k보다 크면 빼지 않고 다음에 먹어야 할 음식 인덱스 출력
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <utility>
#include <queue>

using namespace std;

int solution(vector<int> food_times, long long k)
{
int answer = 0;

long long sum = accumulate(food_times.begin(), food_times.end(), 0LL);
if (sum < k)
{
return -1;
}

// 정렬 전 time과 index를 저장
priority_queue<pair<int, int>> tni;
for (int i = 0; i < food_times.size(); i++)
{
tni.push(make_pair(-food_times[i], i + 1)); // 파이썬은 +
}

int nFoods = food_times.size(); // 남은 음식의 개수
int sumTime = 0; // 먹는데 걸린 총 시간
while (1)
{
pair<int, int> top = tni.top();

if (sumTime + (-top.first) * nFoods > k)
{
answer = (k - sumTime) % nFoods break;
}

sumTime += (-top.first) * nFoods;
tni.pop();
nFoods--;
}

return answer;
}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);

int n;
cin >> n;
vector<int> food_times;
for (int i = 0; i < n; i++)
{
int time;
cin >> time;
food_times.push_back(time);
}

long long k;
cin >> k;

cout << solution(food_times, k) << '\n';

return 0;
}