이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <algorithm>
using namespace std;
const int INF = 1000000000; // Large constant to represent infinity
// Function to find the minimum number of operations required to make the array non-decreasing
int minimumOperations(int arr[], int n) {
// Initialize the 2D array for dynamic programming
int dp[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = INF; // Initialize all entries to infinity
}
dp[i][i] = 0; // Set the base case for single elements
}
// Use a nested loop to fill in the rest of the array
for (int len = 2; len <= n; len++) {
for (int i = 0; i < n - len + 1; i++) {
int j = i + len - 1;
// Find the minimum number of operations needed to merge the left and right subarrays
for (int k = i; k < j; k++) {
dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j]);
}
// If the current subarray is not already non-decreasing, perform an operation
if (arr[j] < arr[j-1]) {
dp[i][j]++;
}
}
}
// Return the minimum number of operations needed for the entire array
return dp[0][n-1];
}
int main() {
// Read the array from the user
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
// Print the result
cout << minimumOperations(arr, n) << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |