Skip to content
Open
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
2 changes: 2 additions & 0 deletions Kaifeel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
| 5차시 | 2026.07.01 | <a href="https://jungol.co.kr/problem/2191">최소 편집 | <a href="https://github.com/AlgoLeadMe/AlgoLeadMe-16/pull/32"> #32|
| 6차시 | 2026.07.07 | <a href="https://www.codetree.ai/ko/trails/complete/curated-cards/challenge-tromino/description">트로미노 | <a href="https://github.com/AlgoLeadMe/AlgoLeadMe-16/pull/35"> #35|
| 7차시 | 2026.07.13 | <a href="https://web.archive.org/web/20260421150151/https://www.acmicpc.net/problem/17472">다리만들기 2 | <a href="https://github.com/AlgoLeadMe/AlgoLeadMe-16/pull/36"> #36|
| 8차시 | 2026.08.10| <a href="https://leetcode.com/problems/maximal-rectangle/description/">Maximal Rectangle | <a href="https://github.com/AlgoLeadMe/AlgoLeadMe-16/pull/37"> #37|
| 9차시 | 2026.08.16| <a href="https://atcoder.jp/contests/abc471/tasks/abc471_c">Cookies and Greedy Takahashi | <a href="https://github.com/AlgoLeadMe/AlgoLeadMe-16/pull/39"> #39|
60 changes: 60 additions & 0 deletions Kaifeel/구현/Cookies and Greedy Takahashi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;
typedef long long ll;

int main()
{
int n;
cin >> n;
vector<ll> plus;

vector<ll> minus;

while (n--)
{
ll num;
cin >> num;
if (num > 0)
plus.push_back(num);
else
minus.push_back(num);
}

sort(plus.begin(), plus.end());
sort(minus.rbegin(), minus.rend());

int i = 0, j = 0;
ll ans = 0;
ll cur = 0;

while (i != minus.size() || j != plus.size())
{

ll leftD = LLONG_MAX;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨습니다!
longlong_max를 사용하신 이유가
왼쪽이나 오른쪽에 더 이상 후보가 없는 경우 엄청 큰 값을 넣어 둬서 이후 거리 비교 때 반대쪽이 선택 되도록 하신 건가요??

ll rightD = LLONG_MAX;

if (i < minus.size())
leftD = abs(minus[i] - cur);
if (j < plus.size())
rightD = abs(plus[j] - cur);

if (leftD <= rightD)
{
ans += leftD;
cur = minus[i];
i++;
}
else
{
ans += rightD;
cur = plus[j];
j++;
}
}

cout << ans;
}