fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1042-B (Div. 3) (C++) Alternating Series
최초 업로드: 2025-08-10 18:07:24
최근 수정 시간: 2025-08-30 14:06:52
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 18
# B. Alternating Series [문제 링크](https://codeforces.com/contest/2131/problem/B) ## Problem Statement You are given an integer $n$. Call an array $a$ of length $n$ **good** if: - For all $1 \le i < n$, $a_i \cdot a_{i+1} < 0$ (i.e., the product of adjacent elements is negative). - For all subarrays (∗) with length at least $2$, the sum of all elements in the subarray is positive (†). Additionally, we say a good array $a$ of length $n$ is **better** than another good array $b$ of length $n$ if $[\,|a_1|,\ |a_2|,\ \ldots,\ |a_n|\,]$ is lexicographically smaller (‡) than $[\,|b_1|,\ |b_2|,\ \ldots,\ |b_n|\,]$. Note that $|z|$ denotes the [absolute value](https://en.wikipedia.org/wiki/Absolute_value) of integer $z$. Output a good array of length $n$ such that it is better than every other good array of length $n$. ## Input The first line contains an integer $t$ ($1 \leq t \leq 500$) — the number of test cases. The single line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of your array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. ## Output For each test case, output $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \leq a_i \leq 10^9$), the elements of your array on a new line. ## Footnotes (∗) An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. (†) An integer $x$ is positive if $x > 0$. (‡) A sequence $a$ is lexicographically smaller than a sequence $b$ if and only if one of the following holds: - $a$ is a prefix of $b$, but $a \ne b$; or - in the first position where $a$ and $b$ differ, the sequence $a$ has a smaller element than the corresponding element in $b$. ## 풀이 #### 홀수번째인 경우 -1, 마지막이 아닌 짝수번째는 3, 마지막인 짝수번째는 2를 출력하면 됩니다. ``` c++ #include<bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int n; cin >> n; for(int i=0;i<n;i++) { if(i%2==0) cout << "-1 "; else if(i<n-1) cout << "3 "; else cout << "2 "; } cout << '\n'; } } ```