1762B - Make Array Good - CodeForces Solution


constructive algorithms implementation number theory sortings *1100

Please click on ads to support us..

Python Code:

sequence = [1]
while sequence[-1] < 10**18:
	sequence.append(sequence[-1]*2)

sequence.pop()

for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))


	print(n)
	for i in range(n):
		print(i+1, min([j for j in sequence if a[i] <= j]) - a[i])

C++ Code:

#include <bits/stdc++.h>
using namespace std;

// was doing
int main()
{
    long long int t;
    cin >> t;

    while (t--)
    {
        long long int n;
        cin >> n;

        long long int arr[n];
        for (long long int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }

        vector<pair<long long int, long long int>> v;

        for (int i = 0; i < n; i++)
        {
            v.push_back(make_pair(arr[i], i + 1));
        }

        sort(v.begin(), v.end());

        cout << n - 1 << endl;

        for (int i = 1; i < n; i++)
        {
            if (v[i].first % v[i - 1].first == 0)
            {
                cout << v[i].second << " " << v[i - 1].first << endl;
                v[i].first = v[i].first + v[i - 1].first;
            }
            else
            {
                int p = v[i].first / v[i - 1].first;
                p++;

                cout << v[i].second << " " << (p * v[i - 1].first - v[i].first) << endl;
                v[i].first = p * v[i - 1].first;
            }
        }

        /*long long int mini = INT_MAX;

        for (long long int i = 0; i < n; i++)
        {
            mini = min(arr[i], mini);
        }

        cout << n << endl;
        for (long long int i = 0; i < n; i++)
        {

            long long int k = arr[i] / mini;
            if (arr[i] % mini == 0)
            {
                if (k % 2 == 1)
                {
                    cout << (i + 1) << " " << mini << endl;
                }
                else
                {
                    cout << (i + 1) << " " << 2 * mini << endl;
                }
            }
            else
            {
                if (k % 2 == 0)
                {
                    cout << (i + 1) << " " << ((k + 2) * mini - arr[i]) << endl;
                }
                else
                {
                    cout << (i + 1) << " " << (k + 1) * mini - arr[i] << endl;
                }
            }
        }*/
    }
}


Comments

Submit
0 Comments
More Questions

119A - Epic Game
703A - Mishka and Game
1504C - Balance the Bits
988A - Diverse Team
1312B - Bogosort
1616B - Mirror in the String
1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It