This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
import java.util.*;
import java.io.*;
class segment_tree// "segment tree"
{
int n;
int[]a;
segment_tree(int n)
{
this.n = n;
a = new int[n];
}
void update(int i,int j)
{
a[i] = j;
}
int query(int i,int j)
{
int ans= 0;
for(int k = i;k<j + 1;k++)ans += a[k];
return ans;
}
}
public class Main
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a= new int[n];
for(int i = 0;i<n;i++) {a[i] = sc.nextInt();a[i]--;}
int[]dp = new int[n + 1];
for(int i= 0;i<n+1;i++)dp[i] = 1000000000;
dp[n] = 0;
for(int i= n;i>=0;i--)
{
int val = dp[i];
ArrayList<Integer>b = new ArrayList<Integer>();
for(int y:a)if(y<i)b.add(y);
int[]idx = new int[i];
for(int j = 0;j<i;j++)idx[b.get(j)] = j;
segment_tree sum = new segment_tree(i);
for(int j= i -1;j>=0;j--)
{
val += i - idx[j] - 1;
val -=sum.query(0,idx[j]);
sum.update(idx[j],1);
dp[j] = Math.min(dp[j],val);
}
}
System.out.println(dp[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... |