fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1049-A (Div. 2) Shift Sort
최초 업로드: 2025-09-09 16:46:45
최근 수정 시간: 2025-09-09 16:49:28
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 37
# A. Shift Sort [문제 링크](https://codeforces.com/contest/2140/problem/A) ## Problem Statement You are given a binary string<sup>*</sup> $s$ of length $n$ and you are allowed to perform the following operation any number of times (including zero): - Choose $3$ indices $1 \le i < j < k \le n$ and right shift or left shift the values on $s_i, s_j, s_k$ cyclically. For the binary string `110110`, if we choose $i=1$, $j=2$, $k=3$ and perform a right shift cyclically, the string becomes `011110`; if we choose $i=4$, $j=5$, $k=6$ and perform a left shift cyclically, the string becomes `110101`. Determine the minimum number of operations required to sort the given binary string. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($3 \le n \le 100$) — the length of the string. The second line contains a binary string $s$ of length $n$. ## Output For each test case, output a single integer — the minimum number of operations required to sort the given binary string. ## Footnotes A binary string* is a string that consists only of the characters `0` and `1`. ## 풀이 맨 뒤에서부터 1이 채워지지 않은 만큼 하나씩 비트 이동을 하면 된다. ``` 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; string s; cin >> n >> s; int cnt=0; for(int i=0;i<n;i++) if(s[i]=='1') cnt++; int need=0; for(int i=n-1;i>=n-cnt;i--) { if(s[i]=='0') need++; } cout << need << '\n'; } } ```