Uva 11039 - Building designing

原创 2013年12月02日 01:18:19

题目链接:Uva 11039 - Building designing

题目大意,有n个绝对值个不相同的非0整数,选出尽量多的数,排成一个序列,使得正负号交替排列,且绝对值递增。求最长序列长度。


import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {

	/**
	 * @param args
	 */
	static Integer[] blue=null;
	static Integer[] red=null;
	static class DescendCompare implements Comparator
	{

		@Override
		public int compare(Object arg0, Object arg1) {
			// TODO Auto-generated method stub
			int a1=(Integer)arg0;
			int a2=(Integer)arg1;
			if(a1>a2) return -1;
			else 
			if(a1<a2) return 1;
			return 0;
		}
		
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in=new Scanner(System.in);
		int t=in.nextInt();
		int n;
		while((t--)!=0)
		{
			int index1=0,index2=0;
			int flag=0;
			n=in.nextInt();
			blue=new Integer[n];
			red=new Integer[n];
			for(int i=0;i<n;i++)
			{
				int temp=in.nextInt();
				if(temp>0)
				  blue[index1++]=temp;
				else
				  red[index2++]=temp;
			}
			DescendCompare comp=new DescendCompare();
			Arrays.sort(blue, 0, index1, comp);
			Arrays.sort(red,0,index2);
			int ans=1;
			if(Math.abs(red[0])>=blue[0])
			{
				flag=1;
			}
			else 
			{	
				flag=0;
			}
			int i=0,j=0;
			while(i<index1&&j<index2)
			{
				if(flag==1)
				{
					while(i<index1&&(blue[i]>=Math.abs(red[j])))
						i++;
					if(i<index1)
					 ans++;
					flag=0;
				}else
				{
					while(j<index2&&(Math.abs(red[j])>=blue[i]))
						j++;
					if(j<index2)
					  ans++;
					flag=1;
				}
			}
			System.out.println(ans);
		}
	}

}


相关文章推荐

UVA - 11039 Building designing

题目大意:搭建楼层,下一层比上一层大且颜色交替(即正负交替),问最多能搭几层解题思路:正负数分别存于两个数组,轮流在两个数组取合适的数。模拟过程好复杂而且 WA 好几次。#include #inclu...

UVA 11039 Building designing

题面:     An architect wants to design a very high building. The building will consist of some floors...

UVa 11039 Building designing (模拟)

11039 - Building designing Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=c...

uva 11039 Building designing (排序)

uva 11039 Building designing An architect wants to design a very high building. The building ...

UVA_11039 Building Designing

题目请点我 题解: 这道题不难,简单的模拟吧。但是收获是定义优先队列的优先级。同学还有一种思路是只放入一个数组中,重载sort比较函数后输出,也很棒。 代码实现:#include #inclu...

uva11039 Building designing

题目大意:给出一个n个数列,将数列按绝对值递增的方式排完续后,求相邻元素符号不同的最大序列长度。 思路:俺绝对值排序,n将减去相邻元素相同的数目为所求答案 /* @author : liuwe...

UVA 11039

Building designing  An architect wants to design a very high building. The building will ...
  • zkzxmzk
  • zkzxmzk
  • 2013年08月03日 15:05
  • 504

Designing the Application Architecture - Building Windows Azure Service Part5: Worker Role Backgroun

http://blogs.msdn.com/b/morebits/archive/2011/01/01/building-windows-azure-service-part5-worker-role...

Designing and Building Parallel Programs

  • 2017年10月15日 23:16
  • 6.52MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Uva 11039 - Building designing
举报原因:
原因补充:

(最多只允许输入30个字)