To solve the problem of determining the minimum number of operations required to sort the given array in non-decreasing order, we can use a dynamic programming approach with the help of a Binary Indexed Tree (Fenwick Tree). Below, I'll explain the step-by-step reasoning and then provide a C++ implementation.
Step-by-Step Reasoning:
-
Understanding the Operation:
- In each operation, you can choose a value
x and set all its occurrences in the array to 0.
- The goal is to make the array non-decreasing with the least number of such operations.
-
Key Observations:
- Unique Values: The number of unique values in the array plays a crucial role. If the array is already sorted, no operations are needed.
- First and Last Occurrences: For each unique value, determine the first and last positions where it appears in the array. This helps in understanding the order dependencies between values.
-
Dynamic Programming Approach:
- Sorting Unique Values: Sort all unique values in increasing order.
- DP Definition: Let
dp[i] represent the length of the longest valid subsequence ending with the i-th unique value.
- Transition: For each unique value, find the maximum
dp[j] such that the last position of j-th value is less than the first position of the i-th value. Then, set dp[i] = dp[j] + 1.
- Result: The maximum value in the DP array will give the length of the longest valid subsequence. The minimum number of operations required will then be the total number of unique values minus this maximum DP value.
-
Using Binary Indexed Tree:
- To efficiently compute the maximum
dp[j] for a given range, we use a Binary Indexed Tree (Fenwick Tree). This allows us to perform range queries and updates in logarithmic time.
C++ Implementation:
#include <bits/stdc++.h>
using namespace std;
// Binary Indexed Tree (Fenwick Tree) for Range Maximum Query
struct BIT {
int size;
vector<int> tree;
BIT(int n) : size(n), tree(n + 2, 0) {}
// Update the tree at position idx with value val (max)
void update(int idx, int val) {
while (idx <= size) {
tree[idx] = max(tree[idx], val);
idx += idx & (-idx);
}
}
// Query the maximum value in the range [1, idx]
int query(int idx) const {
int res = 0;
int i = const_cast<BIT*>(this)->size;
while (idx > 0) {
res = max(res, tree[idx]);
idx -= idx & (-idx);
}
return res;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
// Read the array
vector<int> a(n);
// Assuming input is given as a string without spaces
string s;
cin >> s;
for(int i=0;i<n;i++) a[i] = s[i]-'0';
// Collect unique values and sort them
vector<int> unique_vals;
unordered_set<int> seen;
for(auto x: a){
if(!seen.count(x)){
unique_vals.push_back(x);
seen.insert(x);
}
}
sort(unique_vals.begin(), unique_vals.end());
// Map each unique value to its first and last positions
// Assuming 1-based indexing
unordered_map<int, pair<int, int>> pos_map;
for(int i=0;i<n;i++){
int val = a[i];
if(pos_map.find(val)==pos_map.end()){
pos_map[val] = {i+1, i+1};
}
else{
pos_map[val].second = i+1;
}
}
// Sort unique values in increasing order
sort(unique_vals.begin(), unique_vals.end());
// Initialize BIT
BIT bit(n);
int max_dp =0;
for(auto val: unique_vals){
int fp = pos_map[val].first;
// Query BIT for max dp[j} where last_pos[j} < fp
int query_val = 0;
if(fp >1){
query_val = bit.query(fp-1);
}
int dp = query_val +1;
// Update BIT at last_pos[val} with dp
int lp = pos_map[val].second;
bit.update(lp, dp);
if(dp > max_dp) max_dp = dp;
}
// Number of unique values
int m = unique_vals.size();
// Answer is m - max_dp
cout << (m - max_dp) << "\n";
}
Explanation of Code:
-
Reading Input:
- The program first reads the array length
n and then reads the array itself. The array is assumed to be provided as a contiguous string of digits without spaces.
-
Processing Unique Values:
- It identifies all unique values in the array and sorts them in increasing order.
- For each unique value, it records the first and last positions where it appears in the array.
-
Dynamic Programming with BIT:
- It initializes a Binary Indexed Tree to keep track of the maximum
dp value for different positions.
- It iterates through each unique value in sorted order, updates the DP array by querying the BIT, and updates the BIT with the new
dp value.
-
Output:
- Finally, it calculates the result by subtracting the maximum
dp value from the total number of unique values and outputs it.
Example Walkthrough:
-
Sample Input:
3
332
- Unique values: [2, 3]
- After processing, the maximum DP value is 1.
- Minimum operations required: 2 - 1 = 1
-
Another Example:
4
2143
- Unique values: [1, 2, 3, 4]
- After processing, the maximum DP value is 2.
- Minimum operations required: 4 - 2 = 2
This approach ensures that the solution is efficient and works within the given constraints.