Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

This is a typical DP problem, but you should use leftist tree in this problem to reduce time complexity to O(nlogn)

Posted by Petr at 2005-10-24 18:37:33 on Problem 1738
In Reply To:这样做对不对?? Posted by:faen at 2005-10-24 18:36:11
> import java.util.*;
> import java.io.*;
> public class Main1738n
> {
> 	static int [] sum;
> 	public static void main(String [] args) throws Exception
> 	{
> 		Scanner cin=new Scanner(new FileInputStream("c:\\in.txt"));
> 		while(true)		
> 		{
> 			int n=cin.nextInt();
> 			if(n==0)break;
> 			int [] p=new int[n];
> 			int [][] q=new int[n][n];
> 			sum=new int[n];
> 			for(int i=0;i<n;i++)
> 				p[i]=cin.nextInt();
> 			sum[0]=p[0];
> 			for(int i=1;i<n;i++)
> 				sum[i]=p[i]+sum[i-1];
> 			int total=f(0,n-1,q,p);
> 			System.out.println(total);
> 		}
> 	}
> 
> 	public static int f(int i,int j,int [][] q,int [] p)
> 	{
> 		if(i==j)
> 			return 0;
> 		if(i+1==j)
> 			return p[i]+p[j];
> 		if(q[i][j]>0)
> 			return q[i][j];
> 		int min=Integer.MAX_VALUE;
> 		for(int k=i;k<j;k++)
> 		{
> 			int tmin=f(i,k,q,p)+f(k+1,j,q,p)+sum[j]-sum[i]+p[i];
> 			if(min>tmin)
> 				min=tmin;
> 		}
> 		
> 		return q[i][j]=min;
> 		
> 	}
> }
> 
> 
> 
> 
> 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator