fragment-header
fragment-markdown
홈
로그인
로그아웃
내 블로그
설정
로그인
Codeforces Round 1042-A (Div. 3) (C++) Lever
최초 업로드: 2025-08-10 18:34:29
최근 수정 시간: 2025-08-30 14:06:43
게시자: rlatjwls3333
카테고리: Codeforces
조회수: 31
# A. Lever [문제 링크](https://codeforces.com/contest/2131/problem/A) ## Problem Statement In Divergent Universe, The Lever iterates itself given two arrays $a$ and $b$ of length $n$. In each iteration, The Lever will do the following: 1. Choose a random index $i$ such that $a_i > b_i$. Then, decrease $a_i$ by $1$. If there does not exist such $i$, ignore this step. 2. Choose a random index $i$ such that $a_i < b_i$. Then, increase $a_i$ by $1$. If there does not exist such $i$, ignore this step. After each iteration, the Lever will check if step $1$ is ignored, and if so, it will end its iteration. You're given the two arrays. Find the number of iterations that the Lever does. It can be shown this number is fixed over all possibilities of random indices that The Lever can choose for each step. ## Input Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains one integer $n$ ($1 \le n \le 10$). The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10$). The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \le b_i \le 10$). ## Output For each test case, output one integer — the number of iterations that the Lever does. ## 풀이 #### 문제 지문대로 시뮬레이션하면 된다. ``` c++ #include<bits/stdc++.h> using namespace std; typedef vector<int> vi; int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while(t--) { int n; cin >> n; vi a(n), b(n); for(int i=0;i<n;i++) cin >> a[i]; for(int i=0;i<n;i++) cin >> b[i]; int cnt=1; while(true) { bool chk=false; for(int i=0;i<n;i++) { if(a[i]>b[i]) { a[i]--; chk=true; break; } } for(int i=0;i<n;i++) { if(a[i]<b[i]) { a[i]++; break; } } if(!chk) break; cnt++; } cout << cnt << '\n'; } } ```