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

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);
        }
    }
}
时间: 2024-09-20 05:36:14

HDOJ(HDU) 2192 MagicBuilding(用Java的Map做了下)的相关文章

java 利用Map做缓存一个简单实例

有时候需要将一批数据缓存,并且按照最近最少使用的规则淘汰.这时候就应该想到LRUMap. LRUMap是一种尺寸固定的Map,当它的容量达到最大时采用最近最少使用的规则淘汰相应的元素.  代码如下 复制代码 LRUMap cache = newLRUMap(5);   cache.put("01", "news 1"); cache.put("02", "news 2"); cache.put("03",

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)

Problem Description In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four wor

HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)

Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book

JavaScript实现Java的Map、List功能

JavaScript实现Java的Map.List功能,如下代码: function HashMap(){      this.size=0;      this.map=new Object();  }    HashMap.prototype.put=function(key,value){      if(!this.map[key]){          this.size++;      }      this.map[key]=value;  };  HashMap.prototyp

Java:基于Map实现的频率统计代码

使用泛型T代表所要统计信息的类,应该有效的定义该类的equals()和hasCode(). statistics()方法进行关键字统计. getAllKeysStatistics()方法返回底层的Map,即所有的键-值对. getAllKeys()方法返回所有key组成的Set. getKeyStatistics()方法返回单个确定Key的统计信息. 测试例使用10000个随机整型数(0-9)来统计它们的产生频率. package com.zj.col; import java.util.Has

Java 遍历Map时 删除元素

package net.nie.test; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapTest { private static Map<Integer, String> map=new HashMap<Integer,String>(); /** 1.HashMap 类映射不保证顺序:某些映射可明确保证其顺序: TreeMap 类 *

JS自定义对象实现Java中Map对象功能的方法

这篇文章主要介绍了JS自定义对象实现Java中Map对象功能的方法,可实现类似Java中Map对象增删改查等功能,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了JS自定义对象实现Java中Map对象功能的方法.分享给大家供大家参考.具体分析如下: Java中有集合,Map等对象存储工具类,这些对象使用简易,但是在JavaScript中,你只能使用Array对象. 这里我创建一个自定义对象,这个对象内包含一个数组来存储数据,数据对象是一个Key,可以实际存储的内容! 这里Key

Java中Map和StringTokenizer的使用

    请编写一个文本统计程序,统计某个文本文件中单词出现的次数(忽略大小写).文本文件作为main方法的参数输入,输出如下格式: Hello:3 good:5 moon:1 上述输出表明了文本文件中hello单词出现了3次,good出现了5次,moon出现了1次     import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.StringTokenizer; publi