京东笔试题生日礼物

1.解题思路

题目链接:http://exercise.acmcoder.com/online/online_judge_ques?ques_id=3816&konwledgeId=41

首先对信封进行排序,然后求最长递增子序列即可


2.java代码

package jd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;



class Envelope
{
	public int w=0;
	public int h=0;
	
	public int index=0;
	 
	public Envelope(int w,int h,int index)
	{
		this.w=w;
		this.h=h;
		this.index=index;
	}
	
}

public class Packing
{
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		
		while(in.hasNext())
		{
			int n=in.nextInt();
			int w=in.nextInt();
			int h=in.nextInt();
			
			ArrayList envelopes=new ArrayList();
			for(int i=0;i()
			{
				public int compare(Envelope a,Envelope b)
				{
					if(a.w>b.w&&a.h>b.h)
					{
						return 1;
					}
					else if(a.wb.index)
					{
						return 1;
					}
					else
					{
						return 0;
					}
				}
			});
			
			ArrayList result=new ArrayList();
			for(int i=0;i temp=new ArrayList();
				temp.add(envelopes.get(i));
				int pos=i;
				for(int j=i+1;jenvelopes.get(pos).w&&envelopes.get(j).h>envelopes.get(pos).h)
					{
						temp.add(envelopes.get(j));
						pos=j;
					}
				}
				if(temp.size()>result.size())
				{
					result=temp;
				}
			}
			
			if(result.size()>0)
			{
				System.out.println(result.size());
				for(int i=0;i


你可能感兴趣的:(算法)