새로 추가된 문제라 그런지 풀이가 없어서 혹시나 하고 물어봤더니 풀렸다;
GPT가 제대로 푼 첫 알고리즘 문제.. 뭐지
코드 설명은 아래와 같다.
예제 검증도 해준다.
코드의 동작 방식을 이해하는 데 도움이 된다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N, K;
cin >> N >> K;
vector<int> A(N);
for (int i = 0; i < N; i++) {
cin >> A[i];
}
int g = gcd(N, K);
for (int start = 0; start < g; start++) {
vector<int> group;
for (int i = start; i < N; i += g) {
group.push_back(A[i]);
}
sort(group.begin(), group.end());
int expected = start;
for (int num : group) {
if (num != expected) {
cout << "NO\n";
return 0;
}
expected += g;
}
}
cout << "YES\n";
return 0;
}