Codeforces Round 858 (Div. 2) 1806 C - Sequence Maste Solution
Problem Link : https://codeforces.com/contest/1806/problem/C
Solution in C++:
- /// La ilaha illellahu muhammadur rasulullah
- ///******Bismillahir-Rahmanir-Rahim******///
- /// Abul Hasnat Tonmoy
- /// Department of CSE,23rd batch
- /// Islamic University,Bangladesh
- ///**********ALLAH IS ALMIGHTY************///
- #include <bits/stdc++.h>
- using namespace std;
- int32_t main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- int test;
- cin >> test;
- while (test--) {
- long long n;
- cin >> n;
- long long m = 2 * n;
- long long temp = 0, ar[m + 9], ans = 0;
- for (int i = 1; i <= m; i++) {
- cin >> ar[i];
- ans += abs(ar[i]);
- }
- sort(ar + 1, ar + m + 1);
- if (m == 2) {
- ans = min(ans, abs(ar[1] - ar[2]));
- cout << ans << endl;
- } else if (m == 4) {
- temp = 0;
- for (int i = 1; i <= m; i++) {
- temp += abs(2 - ar[i]);
- }
- ans = min(temp, ans);
- temp = 0;
- for (int i = 1; i < m; i++) {
- temp += abs(-1 - ar[i]);
- }
- temp += abs(2 - ar[m]);
- ans = min(temp, ans);
- cout << ans << endl;
- } else if (m % 4 == 0) {
- std::vector<int> v;
- for (int i = 1; i < m; i++) {
- v.push_back(-1);
- }
- v.push_back(n);
- temp = 0;
- for (int i = 1; i <= m; i++) {
- temp += abs(v[i - 1] - ar[i]);
- }
- ans = min(temp, ans);
- cout << ans << endl;
- } else {
- cout << ans << endl;
- }
- }
- return 0;
- }
No comments