VioletaBabel
1021번: 회전하는 큐 본문
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | #include<cstdio> #include<queue> using namespace std; int checkQueue(queue<int> *q, queue<int> *gacha); void reverseQueue(queue<int> *q); int main() { queue<int> q1, q2, gachaNumbers; int qSize, gachaCount, result = 0; scanf("%d %d", &qSize, &gachaCount); for (int i = 0; i < gachaCount; ++i) { int t; scanf("%d", &t); gachaNumbers.push(t); } q2.push(1); for (int i = 0; ++i <= qSize; q1.push(i)); for (int i = qSize; i > 1; q2.push(i--)); while (gachaCount--) { int qNum1 = checkQueue(&q1, &gachaNumbers); int qNum2 = checkQueue(&q2, &gachaNumbers); result += ((qNum1 < qNum2) ? qNum1 : qNum2); gachaNumbers.pop(); reverseQueue(&q2); } printf("%d", result); return 0; } int checkQueue(queue<int> *q, queue<int> *gacha) { int tempNum = 0; while (true) { if ((*q).front() == (*gacha).front()) { (*q).pop(); break; } ++tempNum; (*q).push((*q).front()); (*q).pop(); } return tempNum; } void reverseQueue(queue<int> *q) { for (int i = 0; i < (*q).size() - 1; ++i) { (*q).push((*q).front()); (*q).pop(); } } | cs |
무식하지만 큐 두개로 회전시킨 코드.
실제로 돌리면 안되겠구나.
그래서 데크로 다시 짬
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<deque> #include<cstdio> using namespace std; int main() { deque<int> left, right; int n, m, result = 0; scanf("%d %d", &n, &m); for (int i = 0; i++ < n; left.push_back(i), right.push_back(i)); for (int tmp; m--;) { scanf("%d", &tmp); int lRes = 0, rRes = 0; while (left.front() != tmp) { ++lRes; left.push_back(left.front()); left.pop_front(); } while (right.front() != tmp) { ++rRes; right.push_front(right.back()); right.pop_back(); } left.pop_front(); right.pop_front(); result += (lRes < rRes) ? lRes : rRes; } printf("%d", result); } | cs |
'백준 > 백준-C++' 카테고리의 다른 글
1212번: 8진수 2진수 (0) | 2018.12.06 |
---|---|
1145번: 적어도 대부분의 배수 (0) | 2018.12.06 |
1003번: 피보나치 함수 (0) | 2018.06.26 |
2217번: 로프 (0) | 2018.03.20 |
4641번: Doubles (0) | 2018.03.15 |
Comments