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
2 changes: 1 addition & 1 deletion dohyeondol1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
| 5μ°¨μ‹œ | 2025.04.02 | DFS & BFS | [DFS와 BFS](https://www.acmicpc.net/problem/1260)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/18|
| 6μ°¨μ‹œ | 2025.04.05 | DP | [ν‰λ²”ν•œ λ°°λ‚­](https://www.acmicpc.net/problem/12865)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/22|
| 7μ°¨μ‹œ | 2025.04.08 | 트리 | [트리 순회](https://www.acmicpc.net/problem/1991)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/26|
---
| 8μ°¨μ‹œ | 2025.04.11 | 덱 | [νšŒμ „ν•˜λŠ” 큐](https://www.acmicpc.net/problem/1021)|https://github.com/AlgoLeadMe/AlgoLeadMe-13/pull/32|
42 changes: 42 additions & 0 deletions dohyeondol1/덱/8-dohyeondol1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include <vector>
#include <deque>
#include <algorithm>
using namespace std;

int main() {
cin.tie(0)->sync_with_stdio(0);
Copy link
Member

Choose a reason for hiding this comment

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

널 값을 λ„˜κ²¨μ€„ 땐 κ°€λŠ₯ν•˜λ©΄ nullptr을 μ‚¬μš©ν•΄μ£Όμ„Έμš©

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nullptr은 포인터λ₯Ό λͺ…ν™•νžˆ λ‚˜νƒ€λ‚΄λŠ”κ΅°μš”.. nullptr을 μ‚¬μš©ν•˜λŠ”κ±Έ μ§€ν–₯ν•΄μ•Όκ² λ„€μš₯

int N, M;
cin >> N >> M;

deque<int> dq;
for(int i = 1; i <= N; i++)
dq.push_back(i);
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

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

μ•„μ£Ό μ‚¬μ†Œν•œ 팁? 인데

#include <numeric>

deque<int> DQ(N);
iota(DQ.begin(), DQ.end(), 1);  // 1, 2, 3, ..., N - 1, N으둜 DQκ°€ μ±„μ›Œμ§

numeric 헀더에 iotaλΌλŠ” ν•¨μˆ˜κ°€ μžˆμŠ΅λ‹ˆλ‹€. 이거λ₯Ό μ‚¬μš©ν•˜λ©΄ λ„˜κ²¨μ£ΌλŠ” 숫자(μ—¬κΈ°μ„œλŠ” 1)λΆ€ν„° 1μ”© μ¦κ°€ν•˜λŠ” κ°’μœΌλ‘œ μ±„μ›Œμ€λ‹ˆλ‹€.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

였호....!!!!! μ‚¬μ†Œν•˜μ§€λ§Œ μ–Έμ  κ°€ 써먹을 팁!! κ°μ‚¬ν•©λ‹ˆλ‹€


vector<int> target(M);
for(int i = 0; i < M; i++)
cin >> target[i];
Comment on lines +16 to +18
Copy link
Member

Choose a reason for hiding this comment

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

사싀 이 λ¬Έμ œλŠ” μ–΄λ–€ target값을 μ‚¬μš©ν•  λ•Œ, λ‹€λ₯Έ target값은 μ•Œ ν•„μš”κ°€ μ—†κΈ° λ•Œλ¬Έμ— 배열에 미리 λ‹€ μ €μž₯해두지 μ•Šμ•„λ„ λ©λ‹ˆλ‹€.

while(M--)
{
  int target; cin >> target;
  int index = find(dq.begin(), dq.end(), target[i]) - dq.begin();

  ...
}

이런 μ‹μœΌλ‘œ μ‹€μ‹œκ°„μœΌλ‘œ target 값을 ν•˜λ‚˜μ”© 받을 λ•Œλ§ˆλ‹€ 연산을 μˆ˜ν–‰ν•˜λ„λ‘ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ„ λ¬΄λ°©ν•˜μ£  :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

κ·Έλ ‡λ„€μš”...; λ‚˜λ¦„ κΉŽλŠ”λ‹€κ³  κΉŽμ•˜λŠ”λ° 살짝 λͺ¨μžλžλ‚˜ λ΄…λ‹ˆλ‹€..
μ›λž˜λŠ” 값을 λ°”λ‘œ μ‚¬μš© κ°€λŠ₯ν•œ 경우 λ˜λ„λ‘μ΄λ©΄ while문을 μ“°λŠ” 편인데, μ΄λ²ˆμ—” μ•Œμ•„μ°¨λ¦¬μ§€ λͺ»ν–ˆλ„€μš”..


int count = 0;
for(int i = 0; i < M; i++) {
int index = find(dq.begin(), dq.end(), target[i]) - dq.begin();

if(index <= dq.size()/2) {
while(index--) {
dq.push_back(dq.front());
dq.pop_front();
count++;
}
} else{
int step = dq.size() - index;
while(step--) {
dq.push_front(dq.back());
dq.pop_back();
count++;
}
}
dq.pop_front();
}

cout << count << '\n';
}