HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)

简介: Problem Description As the increase of population, the living space for people is becoming smaller and smaller.

Problem Description
As the increase of population, the living space for people is becoming smaller and smaller. In MagicStar the problem is much worse. Dr. Mathematica is trying to save land by clustering buildings and then we call the set of buildings MagicBuilding. Now we can treat the buildings as a square of size d, and the height doesn’t matter. Buildings of d1,d2,d3….dn can be clustered into one MagicBuilding if they satisfy di != dj(i != j).
Given a series of buildings size , you need to calculate the minimal numbers of MagicBuildings that can be made. Note that one building can also be considered as a MagicBuilding.
Suppose there are five buildings : 1, 2, 2, 3, 3. We make three MagicBuildings (1,3), (2,3), (2) .And we can also make two MagicBuilding :(1,2,3), (2,3). There is at least two MagicBuildings obviously.

Input
The first line of the input is a single number t, indicating the number of test cases.
Each test case starts by n (1≤n≤10^4) in a line indicating the number of buildings. Next n positive numbers (less than 2^31) will be the size of the buildings.

Output
For each test case , output a number perline, meaning the minimal number of the MagicBuilding that can be made.

Sample Input
2
1
2
5
1 2 2 3 3

Sample Output
1
2

其实说了这么多,就是找出现次数最多的那个数。

练习了下Map的使用。也可以不用Map的。

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main{

    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        int t =sc.nextInt();
        while(t-->0){
            Map<Integer,Integer> map = new TreeMap<Integer, Integer>();
            int n=sc.nextInt();
            int m[] = new int[n];
            for(int i=0;i<n;i++){
                m[i] = sc.nextInt();
                if(map.get(m[i])==null){
                    map.put(m[i], 1);
                }else{
                    map.put(m[i], map.get(m[i])+1);
                }
            }
            int max=0;
            for(int i=0;i<n;i++){
                if(map.get(m[i])>max){
                    max=map.get(m[i]);
                }
            }
            System.out.println(max);
        }
    }
}
目录
相关文章
|
8月前
|
Java
hdoj 1753 (Java)
刚刚开始用Java,代码难免不够简洁。
19 1
|
Java
HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)
HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)
84 0
|
Java
HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)
HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)
128 0
|
机器学习/深度学习 Java C语言
HDOJ(HDU) 2137 circumgyrate the string(此题用Java-AC不过!坑)
HDOJ(HDU) 2137 circumgyrate the string(此题用Java-AC不过!坑)
87 0
|
Java
HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)
HDOJ(HDU) 2133 What day is it(认识下Java的Calendar类---日期类)
71 0
|
Java
HDOJ 1042 N!(大数阶乘JAVA)
HDOJ 1042 N!(大数阶乘JAVA)
94 0
|
Java
HDOJ/HDU 2203 亲和串(简单的判断~Java的indexOf()方法秒)
Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现,现在长大了却不知道怎么去判断亲和串了,于是他只好又再一次来请教聪明且乐于助人的你来解决这个问题。
1037 0
|
Java
HDOJ 1042 N!(大数阶乘JAVA)
Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in one line, process to the end of file.
1003 0
|
2天前
|
安全 算法 Java
Java中的并发编程技术:解锁高效多线程应用的秘密
Java作为一种广泛应用的编程语言,其并发编程技术一直备受关注。本文将深入探讨Java中的并发编程,从基本概念到高级技巧,帮助读者更好地理解并发编程的本质,并学会如何在多线程环境中构建高效可靠的应用程序。
|
2天前
|
Java
Java中多线程的常见实现方式
Java中多线程的常见实现方式
11 2