河南省第九屆ACM Prototypes analyze

題目描述
ALpha Ceiling Manufacturers (ACM) is analyzing the p roperties of its new series of Incredibly
Collapse-Proof Ceilings (ICPCs). An ICPC consists of n layers of material, each with a different
value of collapse resistance (measured as a positive integer). The analysis ACM wants to run will
take the collapse-resistance values of the layers, store them in a binary search tree, and check
whether the shape of this tree in any way correlates with the quality of the whole construction.
Because, well, why should it not?
To be precise, ACM takes the collapse-resistance values for the layers, ordered from the top layer
to the bottom layer, and inserts them one-by-one into a tree. The rules for inserting a value v are:
• If the tree is empty, make v the root of the tree.
• If the tree is not empty, compare v with the root of the tree. If v is smaller, insert v into the left
subtree of the root, otherwise insert v into the right subtree.
ACM has a set of ceiling prototypes it wants to analyze by trying to collapse them. It wants to take
each group of ceiling prototypes that have trees of the same shape and analyze them together.
For example , assume ACM is considering five ceiling prototypes with three layers each, as
described by Sample Input 1 and shown in Figure C.1. Notice that the first prototype’s top layer
has collapseresistance value 2, the middle layer has value 7, and the bottom layer has value 1. The
second prototype has layers with collapse-resistance values of 3, 1, and 4 – and yet these two
prototypes induce the same tree shape, so ACM will analyze them together.
Given a set of prototypes, your task is to determine how many different tree shapes they induce.

輸入
The first line of the input contains one integers T, which is the nember of test cases (1<=T<=8).
Each test case specifies:

  • Line 1: two integers n (1 ≤ n ≤ 50), which is the number of ceiling prototypes to analyze,
    and k (1 ≤ k ≤ 20), which is the number of layers in each of the prototypes.
    *T he next n lines describe the ceiling prototypes. Each of these lines contains k distinct
    integers ( between 1 and 10 6 , inclusive ) , which are the collapse-resistance values of the
    layers in a ceiling prototype, ordered from top to bottom.

輸出
For each test case generate a single line containing a single integer that is the number of different tree
shapes.

樣例輸入 Copy
1
5 3
2 7 1
1 5 9
3 1 4
2 6 5
9 7 3
樣例輸出 Copy
4

package ACM2016;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class G {
	public static int k;
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		k=sc.nextInt();
		int ans[]=new int [k];
		sc.nextLine();
		for(int i=0;i<k;i++)
		{
			int n=sc.nextInt();
			int m=sc.nextInt();
			sc.nextLine();
			int sz[][]=new int [n][m];
			for(int j=0;j<n;j++)
			{
				for(int l=0;l<m;l++)
				{
					sz[j][l]=sc.nextInt();
					
				}
				sc.nextLine();
			}
			ans[i]=check(sz);			
		}
		for(int i=0;i<k;i++)
		{
			System.out.println(ans[i]);
		}
	}
	public static int check(int sz[][])
	{
		int wz[][]=new int [sz.length][sz[0].length];
		for(int i=0;i<sz.length;i++)//遍歷每一行
		{
			wz[i][0]=0;
			for(int j=1;j<sz[0].length;j++)//遍歷每一個
			{
				int min=9000000,max=0;
				int minxb=99999,maxxb=99999;
				for(int l=0;l<j;l++)//遍歷該個應該所在的位置
				{
					if(sz[i][l]>sz[i][j])
					{
						if(min>sz[i][l]) {
							min=sz[i][l];
							minxb=wz[i][l];
						}
					}
					else
					{
						if(max<sz[i][l])
						{
							max=sz[i][l];
							maxxb=wz[i][l];
						}
					}
				}
				if(min==9000000) //新值是最大的
				{
					wz[i][j]=2*maxxb+2;
				}
				else if(max==0)  //新值是最小的
				{
					wz[i][j]=2*minxb+1;
				}
				else {
					if(minxb>maxxb)
					{
						wz[i][j]=2*minxb+1;
					}
					else
					{
						wz[i][j]=2*maxxb+2;
					}
				}
			}
		}
		HashSet<String> hs=new HashSet<String>();
		for(int i=0;i<sz.length;i++)
		{
			Arrays.sort(wz[i]);
			String str="";
			for(int j=0;j<sz[0].length;j++)
			{
				str+=wz[i][j];
			}
			hs.add(str);
		}
		return hs.size();
	}
}













發佈了60 篇原創文章 · 獲贊 15 · 訪問量 5657
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章