Minimum Operations to Make Array Equal and Valid Swap Sequence Analysis

Problem A: Array Equalization Strategy

The optimal approach involves transforming all elements to match the most freqeunt value in the array. Initial attempts with incorrect assumptions led to multiple failed submissions.


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void solve() {
    int testCases;
    cin >> testCases;
    while(testCases--) {
        int size;
        cin >> size;
        vector<int> elements(size);
        for(int i = 0; i < size; i++) {
            cin >> elements[i];
        }
        
        int currentCount = 1;
        int maxCount = 1;
        for(int i = 1; i < size; i++) {
            if(elements[i] == elements[i-1]) {
                currentCount++;
            } else {
                currentCount = 1;
            }
            maxCount = max(maxCount, currentCount);
        }
        cout << size - maxCount << endl;
    }
}

int main() {
    solve();
    return 0;
}

Problem B: Valid Adjacent Swap Sequence

Each element in a sorted sequence can be swapped at most once. To transform an unsorted sequence into sorted order using adjacent swaps, we can attempt to swap all valid inversion pairs and verify the final sorted state.


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void process() {
    int testCases;
    cin >> testCases;
    while(testCases--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        
        for(int i = 1; i < n; i++) {
            if(arr[i-1] - arr[i] == 1) {
                swap(arr[i-1], arr[i]);
            }
        }
        
        if(is_sorted(arr.begin(), arr.end())) {
            cout << "YES" << endl;
        } else {
            cout << "NO" << endl;
        }
    }
}

int main() {
    process();
    return 0;
}

Problem C: Square Distance Sequence Construction

For even counts, pairs can be placed consecutively. For odd counts, at least one element must appear an odd number of times. The key challenge is placing three identical elements with square distances between them.

Let positions be p, p+X, p+X+Y where X, Y, and X+Y are perfect squares. We need x² + y² = z² with minimal z=5 (x=3, y=4). The minimal length for three identical elements is 27 positions.


#include <iostream>
using namespace std;

void generateSequence() {
    int testCases;
    cin >> testCases;
    while(testCases--) {
        int n;
        cin >> n;
        if(n % 2 == 1) {
            if(n < 27) {
                cout << -1 << endl;
            } else {
                cout << "1 2 2 3 3 4 4 5 5 1 6 6 7 7 8 8 9 9 10 10 12 11 11 1 12 ";
                int remaining = (n - 27) / 2;
                for(int i = 1; i <= remaining; i++) {
                    cout << i + 14 << " " << i + 14 << " ";
                }
                cout << endl;
            }
        } else {
            for(int i = 1; i <= n/2; i++) {
                cout << i << " " << i << " ";
            }
            cout << endl;
        }
    }
}

int main() {
    generateSequence();
    return 0;
}

Problem D: Connected Component Optimization

Using union-find to manage connected components while tracking maximum and minimum values within each component. Components can be merged when current maximum exceeds previous minimum values.


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class UnionFind {
private:
    vector<int> parent;
    vector<int> maxVal;
    vector<int> minVal;
    
public:
    UnionFind(int n, vector<int>& arr) {
        parent.resize(n);
        maxVal.resize(n);
        minVal.resize(n);
        for(int i = 0; i < n; i++) {
            parent[i] = i;
            maxVal[i] = arr[i];
            minVal[i] = arr[i];
        }
    }
    
    int find(int x) {
        if(parent[x] != x) {
            parent[x] = find(parent[x]);
        }
        return parent[x];
    }
    
    void unite(int x, int y) {
        int rootX = find(x);
        int rootY = find(y);
        if(rootX != rootY) {
            maxVal[rootX] = max(maxVal[rootX], maxVal[rootY]);
            minVal[rootX] = min(minVal[rootX], minVal[rootY]);
            parent[rootY] = rootX;
        }
    }
    
    int getMax(int x) { return maxVal[find(x)]; }
    int getMin(int x) { return minVal[find(x)]; }
};

void solveProblem() {
    int testCases;
    cin >> testCases;
    while(testCases--) {
        int n;
        cin >> n;
        vector<int> arr(n);
        for(int i = 0; i < n; i++) {
            cin >> arr[i];
        }
        
        UnionFind uf(n, arr);
        vector<pair<int, int>> prefixMax(n);
        
        for(int i = 0; i < n; i++) {
            if(i == 0) {
                prefixMax[i] = {arr[i], i};
            } else {
                prefixMax[i] = max(prefixMax[i-1], {arr[i], i});
            }
            uf.unite(i, prefixMax[i].second);
        }
        
        int currentMin = 1e9;
        for(int i = n-1; i >= 0; i--) {
            if(uf.getMax(i) > currentMin) {
                uf.unite(i, i+1);
            }
            currentMin = min(currentMin, uf.getMin(i));
        }
        
        for(int i = 0; i < n; i++) {
            cout << uf.getMax(i) << " ";
        }
        cout << endl;
    }
}

int main() {
    solveProblem();
    return 0;
}

Tags: competitive-programming algorithm-optimization Union-Find sequence-construction array-manipulation

Posted on Fri, 11 Sep 2026 16:36:37 +0000 by 156418