-
Notifications
You must be signed in to change notification settings - Fork 1
8-dohyeondol1 #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8-dohyeondol1 #32
Conversation
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dequeμ μλ°©ν₯ μ μΆλ ₯μ΄ κ°λ₯νλ€λ κ² μΈμλ, queueμ λ¬λ¦¬ iterator κΈ°λ° μ°μ°μ μ§μν©λλ€(μΌλ° queueλ find κ°μ λ©μλλ₯Ό μ¬μ©ν μ μμ΅λλ€!), κ·Έλμ [] μ°μ°μλ₯Ό ν΅ν΄ νΉμ μΈλ±μ€μ μμλ λ°λ‘ μ»μ μ μλ€λ νΉμ§μ΄ μμ£ .
κ·Έλ λ€λ³΄λ μ λ μ΄μ§κ°ν΄μ queueλ³΄λ€ dequeμ μ νΈνκ² λλ€μ :)
#include <iostream>
#include <queue>
#include <numeric>
#include <algorithm>
using namespace std;
int main()
{
int N, M; cin >> N >> M;
deque<int> DQ(N);
iota(DQ.begin(), DQ.end(), 1);
auto LeftShift = [&DQ]()
{
DQ.emplace_back(DQ.front());
DQ.pop_front();
};
auto RightShift = [&DQ]()
{
DQ.emplace_front(DQ.back());
DQ.pop_back();
};
int Answer = 0;
while(M--)
{
int Target; cin >> Target;
int Index = find(DQ.begin(), DQ.end(), Target) - DQ.begin();
if(Index <= DQ.size() / 2)
{
for(; DQ.front() != Target; Answer++)
{
LeftShift();
}
}
else
{
for(; DQ.front() != Target; Answer++)
{
RightShift();
}
}
DQ.pop_front();
}
cout << Answer;
return 0;
}
dohyeondol1/λ±/8-dohyeondol1.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ κ°μ λκ²¨μ€ λ κ°λ₯νλ©΄ nullptrμ μ¬μ©ν΄μ£ΌμΈμ©
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nullptrμ ν¬μΈν°λ₯Ό λͺ
νν λνλ΄λκ΅°μ.. nullptrμ μ¬μ©νλκ±Έ μ§ν₯ν΄μΌκ² λ€μ₯
dohyeondol1/λ±/8-dohyeondol1.cpp
Outdated
There was a problem hiding this comment.
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μ© μ¦κ°νλ κ°μΌλ‘ μ±μμ€λλ€.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€νΈ....!!!!! μ¬μνμ§λ§ μΈμ κ° μ¨λ¨Ήμ ν!! κ°μ¬ν©λλ€
dohyeondol1/λ±/8-dohyeondol1.cpp
Outdated
There was a problem hiding this comment.
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 κ°μ νλμ© λ°μ λλ§λ€ μ°μ°μ μννλλ‘ μ½λλ₯Ό μμ±ν΄λ 무방νμ£ :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
κ·Έλ λ€μ...; λλ¦ κΉλλ€κ³ κΉμλλ° μ΄μ§ λͺ¨μλλ λ΄
λλ€..
μλλ κ°μ λ°λ‘ μ¬μ© κ°λ₯ν κ²½μ° λλλ‘μ΄λ©΄ whileλ¬Έμ μ°λ νΈμΈλ°, μ΄λ²μ μμμ°¨λ¦¬μ§ λͺ»νλ€μ..
caucsejunseo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ±μ μ²μ 보μ§λ§ νλ₯Ό μ΄μ μ 곡λΆνμ΄μ μ½κ² μ νμ΅λλ€! λ¬Έμ κ° μ²μ μ νμ λ νκΈ° μ’μ λ¬Έμ μΈ κ±° κ°μμ. cμΈμ΄λ‘ λ±μ ν λ² κ΅¬νν΄ λ΄€μ΄λ€.
C μ½λ
#define MAX 1000 // λ± μ΅λ ν¬κΈ°
typedef struct {
int data[MAX];
int front;
int back;
} Deque;
// λ± μ΄κΈ°ν
void init(Deque* dq) {
dq->front = dq->back = 0;
}
// λ±μ΄ λΉμλμ§ νμΈ
int isEmpty(Deque* dq) {
return dq->front == dq->back;
}
// λ±μ΄ κ°λ μ°Όλμ§ νμΈ
int isFull(Deque* dq) {
return (dq->back + 1) % MAX == dq->front;
}
// μμ μ½μ
void push_front(Deque* dq, int x) {
if (isFull(dq)) {
printf("Deque is full!\n");
return;
}
dq->front = (dq->front - 1 + MAX) % MAX;
dq->data[dq->front] = x;
}
// λ€μ μ½μ
void push_back(Deque* dq, int x) {
if (isFull(dq)) {
printf("Deque is full!\n");
return;
}
dq->data[dq->back] = x;
dq->back = (dq->back + 1) % MAX;
}
// μμμ μ κ±°
int pop_front(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return -1;
}
int value = dq->data[dq->front];
dq->front = (dq->front + 1) % MAX;
return value;
}
// λ€μμ μ κ±°
int pop_back(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return -1;
}
dq->back = (dq->back - 1 + MAX) % MAX;
return dq->data[dq->back];
}
// μμ κ° νμΈ
int front(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return -1;
}
return dq->data[dq->front];
}
// λ€μ κ° νμΈ
int back(Deque* dq) {
if (isEmpty(dq)) {
printf("Deque is empty!\n");
return -1;
}
return dq->data[(dq->back - 1 + MAX) % MAX];
}
|
νμ΄μ¬
from collections import deque
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
targets = list(map(int, input().split()))
dq = deque(range(1, N+1))
ops = 0
for t in targets:
idx = dq.index(t)
if idx <= len(dq) - idx:
dq.rotate(-idx)
ops += idx
else:
dq.rotate(len(dq) - idx)
ops += len(dq) - idx
dq.popleft()
print(ops) |
|
@caucsejunseo |
hadongun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€μ°.. νμ΄μ¬μλ νλ₯Ό νμ μν¬ μ μλ rotateλΌλ κ² μλκ΅°μ!
μκ°λ³΄λ€ μ½μ§ μλ€μ.. ! μ°μ° νμλ₯Ό μ΄λ»κ² ꡬν΄μΌ ν κΉ.. μ λν΄μ μκ°μ΄ μ λμ§ μμ
μ½κ°μ μ§νΌν°μ λμμ λ°μλ€μ₯
νμ΄λ€
from collections import deque
n, m = map(int, input().split())
targets = list(map(int, input().split()))
rotationq = deque()
for i in range(1, n + 1):
rotationq.append(i)
total_moves = 0
for target in targets:
idx = rotationq.index(target)
if idx <= len(rotationq) // 2:
rotationq.rotate(-idx)
total_moves += idx
else:
rotationq.rotate(len(rotationq) - idx)
total_moves += len(rotationq) - idx
rotationq.popleft()
print(total_moves)|
@hadongun μ λ κ°μΈμ μΌλ‘ λ¬Έμ λ₯Ό νλ€κ° μμ λͺ¨λ₯΄κ² λ€ μΆμΌλ©΄ λ€λ₯Έ μ¬λλ€μ λΈλ‘κ·Έλ₯Ό μ°Έκ³ νλ νΈμ
λλ€.. μ΄μ¨κ±°λ μ΄λ²μ λ°°μ μΌλ λ€μμ μ νλ©΄ λμ£ ! λΉ λΉ λΉ μ΄ν ! |
π λ¬Έμ λ§ν¬
βοΈ μμλ μκ°
40λΆ
β¨ μλ μ½λ
μλ°©ν₯ νλΌκ³ λ³Ό μ μλ 'λ±(deque)' μλ£κ΅¬μ‘°λ₯Ό ν΅ν΄ μ½κ² ν μ μλ λ¬Έμ μμ΅λλ€.
λ±μ λͺ°λλ€ νλλΌλ ν μμΌ μκ² λλΌκ΅¬μ..
μ£Όμ΄μ§ μ λ ₯ μμλλ‘ μλ₯Ό λ½μλ΄μΌ νκΈ° λλ¬Έμ,
μ°μ° νμλ‘ μΉμ§ μλ 1λ² μ°μ°μ μ΄μ©νλ΅μκ³ λ¬΄μμ μλ₯Ό λ½μλ΄κΈ°λ§ ν΄μλ λ΅μ λμΆν μ μμ΅λλ€.
μ£Όμ΄μ§ μκ° λ±μμ μ(μΌμͺ½), λ€(μ€λ₯Έμͺ½) μ€ μ΄λμ λ κ°κΉμ΄μ§ νλ³νκ³ ,
κ°κΉμ΄ μͺ½μΌλ‘ μλ₯Ό ν μΉΈμ© μ΄λμμΌ λμ리μμ λ½μλ΄μΌ ν©λλ€.
μ΄λμ κ°κΉμ΄μ§λ
λ± ν¬κΈ°μ μ λ°(μ€κ°μ§μ )κ³Ό, ν΄λΉ μκ° μμΉν μΈλ±μ€μ λμ λΉκ΅λ₯Ό ν΅ν΄ νλ³νμμ΅λλ€.
λ± ν¬κΈ°μ μ λ°λ³΄λ€ μΈλ±μ€κ° μλ€λ©΄ μ(μΌμͺ½)μ μμΉν κ²μ΄κ³ , μλλΌλ©΄ λ€(μ€λ₯Έμͺ½)μ μμΉν κ²μ΄κ² μ£ ?
μ΄μ λ§μΆ° μ°μ°νμλ₯Ό 1μ© μ¦κ°μμΌ μ΅μμ°μ°νμλ₯Ό λμΆνμμ΅λλ€.
μλ μ½λ
π μλ‘κ² μκ²λ λ΄μ©
λ±(Deque, double-ended queue)
λ κ°μ ν¬μΈν°λ₯Ό μ¬μ©νμ¬, μμͺ½μμ μμ μ μ½μ μ λ°μμν¬ μ μμ΅λλ€.
μνκΈ°κ°μ΄λΌ PR μ°κΈ°κ° μ‘°κΈ λΉ λ―νλ€μ...κ·Έλ¦Ό μλ£λ κ°λ₯ν 빨리 μΆκ°ν΄λ³΄λλ‘ νκ² μ΅λλ€..