알고리즘
백준 1021번 회전하는 큐 C++
영춘권의달인
2022. 6. 5. 17:32
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;
};