Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 누적 합
- 트리
- 재귀
- 유니티
- 알고리즘
- 유니온 파인드
- 정렬
- BFS
- 그리디 알고리즘
- 스택
- XR Interaction Toolkit
- 문자열
- c++
- 우선순위 큐
- 백준
- 자료구조
- 그래프
- Team Fortress 2
- 브루트포스
- 다이나믹 프로그래밍
- 수학
- DFS
- Unreal Engine 5
- 투 포인터
- ue5
- 백트래킹
- 구현
- 다익스트라
- VR
- 시뮬레이션
Archives
- Today
- Total
1일1알
백준 1021번 회전하는 큐 C++ 본문
2번 연산을 수행하던 3번 연산을 수행하던 수행하는 횟수만 다르고 결과는 같다. 둘중 아무거나로 연산한 뒤에
연산결과, 큐 용량 - 연산결과 중 작은 것을 고르면 된다.
그래서 굳이 덱으로 만들지 않고 큐로 2번 연산만 수행해서 풀었다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using ll = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m;
cin >> n >> m;
queue<int> q;
int ans = 0;
for (int i = 1; i <= n; i++) {
q.push(i);
}
for (int i = 0; i < m; i++) {
int num;
cin >> num;
int cnt = 0;
while (num != q.front()) {
int front = q.front();
q.pop();
q.push(front);
cnt++;
}
cnt = min(cnt, int(q.size()) - cnt);
ans += cnt;
q.pop();
}
cout << ans;
};
'알고리즘' 카테고리의 다른 글
백준 1715번 카드 정렬하기 C++ (0) | 2022.06.07 |
---|---|
백준 18258번 큐 2 C++ (0) | 2022.06.06 |
백준 1406번 에디터 C++ (0) | 2022.06.04 |
백준 10815번 숫자 카드 C++ (0) | 2022.06.03 |
백준 1158번 요세푸스 문제 C++ (0) | 2022.06.02 |