小易有一个古老的游戏机,上面有着经典的游戏俄罗斯方块。因为它比较古老,所以规则和一般的俄罗斯方块不同。
荧幕上一共有 n 列,每次都会有一个 1 x 1 的方块随机落下,在同一列中,后落下的方块会叠在先前的方块之上,当一整行方块都被占满时,这一行会被消去,并得到1分。
有一天,小易又开了一局游戏,当玩到第 m 个方块落下时他觉得太无聊就关掉了,小易希望你告诉他这局游戏他获得的分数。
第一行两个数 n, m
第二行 m 个数,c1, c2, ... , cm , ci 表示第 i 个方块落在第几列
其中 1 <= n, m <= 1000, 1 <= ci <= n
小易这局游戏获得的分数
3 9 1 1 2 2 2 3 1 2 3
2
package com.shengxi.niuke; import java.util.LinkedList; import java.util.Scanner; /** * 题目描述 * 小易有一个古老的游戏机,上面有着经典的游戏俄罗斯方块。因为它比较古老,所以规则和一般的俄罗斯方块不同。 * 荧幕上一共有 n 列,每次都会有一个 1 x 1 的方块随机落下,在同一列中, * 后落下的方块会叠在先前的方块之上,当一整行方块都被占满时,这一行会被消去,并得到1分。 * 有一天,小易又开了一局游戏,当玩到第 m 个方块落下时他觉得太无聊就关掉了, * 小易希望你告诉他这局游戏他获得的分数。 * 输入描述: * <p> * 第一行两个数 n, m * 第二行 m 个数,c1, c2, ... , cm , ci 表示第 i 个方块落在第几列 * 其中 1 <= n, m <= 1000, 1 <= ci <= n * <p> * 输出描述: * <p> * 小易这局游戏获得的分数 * <p> * 3 9 * 1 1 2 2 2 3 1 2 3 * <p> * <p> * 2 * <p> * 这里是一个无限多行的俄罗斯方块,所以本质问题是考虑如果避免超长 * * @author yan */ public class Day003 { /** * 列数n */ private static int numColumns; private static int[] lists; public static void main(String[] args) { scan(); int result = lists[0]; for (int list : lists) { result = Math.min(list, result); } System.out.println(result); } private static void scan() { Scanner scan = new Scanner(System.in); numColumns = scan.nextInt(); lists = new int[numColumns]; int numChessPieces = scan.nextInt(); for (int i = 0; i < numChessPieces; i++) { lists[scan.nextInt() - 1]++; } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] c = new int[m]; for(int i = 0;i < m;i++){ c[i] = sc.nextInt(); } HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0;i < m;i++){ map.put(c[i],map.getOrDefault(c[i],0)+1); } int count = Integer.MAX_VALUE; //for循环迭代要最小值,输出的初始值设为最大值即可 if(map.size() == n){ for(int i = 0;i < m-1;i++){ count = Math.min(count,map.get(c[i])); } }else{ count = 0; } System.out.println(count); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int x = 0; boolean isfull = false; int min = 0; Map<Integer,Integer> map = new HashMap<>(); for(int i = 0;i < m;i++){ x = sc.nextInt(); if(map.get(x) == null){ map.put(x,1); }else{ int temp = map.get(x) + 1; map.put(x,temp); } } for(int j = 1;j <= n;j++){ if(!map.containsKey(j)){ isfull = false; break; } isfull = true; } if(isfull){ Collection<Integer> values = map.values(); min = Collections.min(values); } System.out.println(min); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int m=sc.nextInt(); int[] a=new int[n]; for(int i=0;i<m;i++){ int t=sc.nextInt(); a[t-1]++; } Arrays.sort(a); System.out.print(a[0]); } }
import java.util.*; //思路就是存储每个方块出现的次数,最后求次数最小的方块,即为得分数。 public class Main{ public static void main(String[] args){ Map<Integer, Integer> map = new HashMap<>(); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); for(int i = 0; i < m; i++){ int temp = sc.nextInt(); if(map.containsKey(temp)){ map.put(temp, map.get(temp) + 1); }else{ map.put(temp, 1); } } //判断每个元素出现的次数,次数最小的就是得分 int score = Integer.MAX_VALUE; if(map.size() == n){ //保证n列上只出现n个不同的方块 for(Integer key : map.keySet()){ score = Math.min(score, map.get(key)); } }else{ score = 0; } System.out.print(score); } }
import java.util.*; public class Main { static class Element { int pos, time; public Element(int pos, int time) { this.pos = pos; this.time = time; } } public static void main(String[] args) { // write your code here Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); PriorityQueue<Element> pq = new PriorityQueue<>(new Comparator<Element>() { @Override public int compare(Element o1, Element o2) { if(o1.time == o2.time) { return 0; } return o1.time > o2.time ? 1 : -1; } }); Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < m; i++) { int pos = sc.nextInt(); if(map.containsKey(pos)) { map.put(pos, map.get(pos) + 1); } else { map.put(pos,1); } } if(map.size() < n) { System.out.println(0); return; } for(Map.Entry<Integer, Integer> entry : map.entrySet()) { pq.offer(new Element(entry.getKey(), entry.getValue())); } System.out.println(pq.peek().time); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int[] arrm = new int[m]; int[] arrn = new int[n]; for(int i=0;i<m;i++){ arrm[i]=sc.nextInt(); } for(int i=0;i<m;i++){ for(int j=0;j<n;j++){ if(arrm[i]==j+1) arrn[j]+=1; } } int minp = arrn[0]; for(int i=0;i<n;i++){ minp=minp<arrn[i]?minp:arrn[i]; } System.out.println(minp); } }
import java.util.Scanner; import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int res[] = new int[n+1];//将荧幕宽设置为一个数组 for(int i=0;i<m;i++) res[sc.nextInt()]++;//落在哪一列就加1 Arrays.sort(res); System.out.println(res[1]);//找到最矮的那列 } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int score = 0; int[][] p = new int[m][n];//用于存放位置 int[] ci = new int[m]; for (int i = 0; i < m; i++) { ci[i] = sc.nextInt(); } int j = 0; int k = 0; for (int i = 0; i < m; i++) { while(p[j][ci[i]-1]!=0){ j++; } p[j][ci[i]-1] = 1; j = 0; if(isFull(p[k])){ score++; downMove(p); } } System.out.println(score); } public static boolean isFull(int[] s){ for(int i=0;i<s.length;i++){ if(s[i]==0){ return false; } } return true; } public static void downMove(int[][] p){ for(int a=0;a<p.length-1;a++){ //每一列下移 for(int b=0;b<p[0].length;b++){ p[a][b] = p[a+1][b]; if(a+1==p.length-1){ p[a+1][b] = 0; } } } } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < m; i++) { int num = sc.nextInt(); if (!map.containsKey(num)) { map.put(num, 1); } else { map.put(num, map.get(num) + 1); } } int min = Integer.MAX_VALUE; if (map.size() == n) { for (Map.Entry<Integer, Integer> entry : map.entrySet()) { if (entry.getValue() < min) { min = entry.getValue(); } } } else { min = 0; } System.out.println(min); } }