Skip to content

Commit

Permalink
2024-05-27 포도주 시음
Browse files Browse the repository at this point in the history
  • Loading branch information
InSange committed May 26, 2024
1 parent 5385745 commit 2ddf2f3
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
5 changes: 3 additions & 2 deletions InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
| 9차시 | 2024.04.12 | 힙 | [Top K Frequent Words](https://leetcode.com/submissions/detail/1180988760/) | [#9](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 10차시 | 2024.04.30 | 스택 | [오아시스 재결합](https://www.acmicpc.net/problem/3015) | [#10](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/40)]
| 11차시 | 2024.05.03 | DFS | [숫자고르기](https://www.acmicpc.net/problem/2668) | [#11](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/41]
| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 13차시 | 2024.05.22 | BFS | [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
| 12차시 | 2024.05.08 | DP | [전깃줄](https://www.acmicpc.net/problem/2565) | [#12](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/46)]
| 13차시 | 2024.05.22 | BFS | [특정 거리의 도시 찾기](https://www.acmicpc.net/problem/18352) | [#13](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/53)]
| 14차시 | 2024.05.27 | 투 포인터 | [포도주 시음](https://www.acmicpc.net/problem/31589) | [#14](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/28)]
=======
---
57 changes: 57 additions & 0 deletions InSange/투포인터/31589.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int N, K, l, r, lastDrink;
vector<int> wine;
bool isLeft = false;
long long answer = 0;

void Solve()
{
cin >> N >> K;

wine.assign(N, 0);

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

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

l = 0;
r = N - 1;
answer = 0;
lastDrink = 0;

while (K--)
{
if (isLeft == true) // ¸À ¾ø´Â °Å
{
lastDrink = wine[l];
l++;
}
else// if(isLeft == false) // ¸À ÀÖ´Â °Å
{
answer += wine[r] - lastDrink;
r--;
}

isLeft = (isLeft + 1) & 1;
}

cout << answer;
}

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

Solve();

return 0;
}

0 comments on commit 2ddf2f3

Please sign in to comment.