-
Problem - C - Codeforces
codeforces.com
풀이
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include <bits/stdc++.h>#define endl "\n"#define ooop(i, n) for(int i = 0; i < n; i++)#define loop(i, n) for(int i = 1; i <= n; i++)#define all(v) (v).begin(), (v).end()using namespace std;typedef long long ll;typedef pair<int, int> pi;typedef pair<ll, ll> pl;int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int t;cin >> t;while(t--){int n;cin >> n;string a, b;cin >> a >> b;int diff_cnt = 0;int diff_one = 0;int same_one = 0;ooop(i, n){if(a[i] != b[i]){if(a[i] == '1') diff_one++;diff_cnt++;}else{if(a[i] == '1') same_one++;}}int same_cnt = n - diff_cnt;int diff_zero = diff_cnt - diff_one;int same_zero = same_cnt - same_one;int flag_diff = -1;if(diff_cnt % 2 == 0 && 0 <= diff_one - diff_zero && diff_one - diff_zero <= 1) flag_diff = diff_cnt;int flag_same = -1;if(same_cnt % 2 == 1 && 0 <= same_one - same_zero && same_one - same_zero <= 1) flag_same = same_cnt;if(max(flag_diff, flag_same) == -1) cout << -1 << endl;else{if(flag_diff == -1) cout << flag_same << endl;else if(flag_same == -1) cout << flag_diff << endl;else{cout << min(flag_same, flag_diff) << endl;}}}return 0;}cs'코드포스' 카테고리의 다른 글
1303-C, *1600 (0) 2021.12.26 1303-B, *1400 (0) 2021.12.26 1615-B (0) 2021.12.25 1615-A (0) 2021.12.25 1301-B, *1500 (0) 2021.12.24