제출 #478902

#제출 시각아이디문제언어결과실행 시간메모리
478902arwaeystoamnegGroup Photo (JOI21_ho_t3)Java
64 / 100
5018 ms15828 KiB
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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...