알고리즘/코드포스
codeforces #580 div2 C - Almost Eqaul (발상)
sun__
2019. 11. 13. 15:15
https://codeforces.com/contest/1206/problem/C
1 ~ 2*n 까지의 숫자를 나열하는데 연속된 n개의 부분합이 모두 같도록 나열하는 문제다.
n==3)
x y z x+1 y-1 z+1
n==5)
x y z w d x+1 y-1 z+1 w-1 d+1
...
위와 같은 패턴이면 된다.
위 패턴만 만족시키면 문제 조건을 만족하게 된다. 나는 추가로 구간의 합이 모두 같음도 생각해야 되는 줄 알아서 삽질을 좀 했다.
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n; cin >> n;
if (n % 2) {
cout << "YES\n";
int cnt = 2*n, curr = 1;
bool f = false;
while (cnt--) {
cout << (curr==0? 2*n : curr) << " ";
curr = (curr + (f ? 1 : 3)) % (2*n);
f = !f;
}
cout << '\n';
}
else
cout << "NO\n";
}