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

贴Java代码。DP O(n*log n)

Posted by haqishen at 2015-01-04 13:39:44 on Problem 1065
import java.util.*;
public class Main{
	public static void main(String args[]){
		Scanner s = new Scanner(System.in);
		
		int T = s.nextInt();
		
		for(int t = 0; t < T; t++){
			int n = s.nextInt();
			stick sts[] = new stick[n];
			
			for(int i = 0; i < n; i++){
				sts[i] = new stick();
				sts[i].l = s.nextInt();
				sts[i].w = s.nextInt();
			}
			Arrays.sort(sts);
				
			int dp[] = new int[n+1];
			int len = 0;
			
			for(int i = 0; i < n; i++){
				
				int l = 0, r = len;
				
				while(l <= r){
					int temp = (l+r)/2;
					if(dp[temp] > sts[i].w){
						l = temp + 1;
					}else {
						r = temp - 1;
					}
				}
				dp[l] = sts[i].w;
				if(l == len)
					len++;
			
			}
			System.out.println(len);
		}
	}
}

class stick implements Comparable<stick>{
	int l,w;
	public int compareTo(stick o) {
		if(this.l == o.l)
			return this.w - o.w;
		else
			return this.l - o.l;		
	}	
}

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