作为一个手串艺人,有金主向你订购了一条包含n个杂色串珠的手串——每个串珠要么无色,要么涂了若干种颜色。为了使手串的色彩看起来不那么单调,金主要求,手串上的任意一种颜色(不包含无色),在任意连续的m个串珠里至多出现一次(注意这里手串是一个环形)。手串上的颜色一共有c种。现在按顺时针序告诉你n个串珠的手串上,每个串珠用所包含的颜色分别有哪些。请你判断该手串上有多少种颜色不符合要求。即询问有多少种颜色在任意连续m个串珠中出现了至少两次。
作为一个手串艺人,有金主向你订购了一条包含n个杂色串珠的手串——每个串珠要么无色,要么涂了若干种颜色。为了使手串的色彩看起来不那么单调,金主要求,手串上的任意一种颜色(不包含无色),在任意连续的m个串珠里至多出现一次(注意这里手串是一个环形)。手串上的颜色一共有c种。现在按顺时针序告诉你n个串珠的手串上,每个串珠用所包含的颜色分别有哪些。请你判断该手串上有多少种颜色不符合要求。即询问有多少种颜色在任意连续m个串珠中出现了至少两次。
第一行输入n,m,c三个数,用空格隔开。(1 <= n <= 10000, 1 <= m <= 1000, 1 <= c <= 50) 接下来n行每行的第一个数num_i(0 <= num_i <= c)表示第i颗珠子有多少种颜色。接下来依次读入num_i个数字,每个数字x表示第i颗柱子上包含第x种颜色(1 <= x <= c)
一个非负整数,表示该手链上有多少种颜色不符需求。
5 2 3 3 1 2 3 0 2 2 3 1 2 1 3
2
第一种颜色出现在第1颗串珠,与规则无冲突。 第二种颜色分别出现在第 1,3,4颗串珠,第3颗与第4颗串珠相邻,所以不合要求。 第三种颜色分别出现在第1,3,5颗串珠,第5颗串珠的下一个是第1颗,所以不合要求。 总计有2种颜色的分布是有问题的。 这里第2颗串珠是透明的。
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(), m = in.nextInt(), c = in.nextInt(); List<List<Integer>> ls = new ArrayList<>(); for (int i = 0; i < n; ++i) { List<Integer> lss = new ArrayList<>(); int x = in.nextInt(); for (int j = 0; j < x; ++j) lss.add(in.nextInt()); ls.add(lss); } int[] last = new int[c + 1]; Arrays.fill(last, Integer.MIN_VALUE / 2); Set<Integer> s = new HashSet<>(); for (int i = 0; i < ls.size() * 2; ++i) { List<Integer> colors = ls.get(i % ls.size()); for (int color: colors) { if (i - last[color] + 1 <= m) s.add(color); last[color] = i; } } System.out.println(s.size()); } }
import java.util.Scanner; import java.util.ArrayList; public class 手环_暴力炸 { public static void main (String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); int c = sc.nextInt(); int[][] table= new int[n][c]; ArrayList<Integer> wrong = new ArrayList<Integer>(); int res = 0; for (int j=0;j<n;j++){ int x = sc.nextInt(); for(int k=0;k<x;k++){ table[j][k] = sc.nextInt(); } } for (int i = 0;i<n;i++){ ArrayList<Integer> colors = new ArrayList<Integer>(); for (int t=0;t<m;t++){ int num = i+t;//一简化就必定爆 if (num>=n){ num -= n; } for (int k=0;k<table[num].length;k++){ if (table[num][k] != 0 && wrong.contains(table[num][k])== false ){ if (colors.contains(table[num][k])){ wrong.add(table[num][k]); res++; }else{ colors.add(table[num][k]); } } } } } System.out.println(res); } }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 珠子数量 int n = scanner.nextInt(); // 测试范围 int m = scanner.nextInt(); // 颜色总数 int c = scanner.nextInt(); // 将每个颜色和颜色的范围存入map中 Map<Integer, List<Integer>> colorMap = new HashMap<Integer, List<Integer>>(); for (int i = 0; i < n; i++) { int count = scanner.nextInt(); if (count == 0) { continue; } for (int j = 0; j < count; j++) { int color = scanner.nextInt(); if (colorMap.containsKey(color)) { colorMap.get(color).add(i + 1); } else { List<Integer> indexList = new ArrayList<Integer>(); indexList.add(i + 1); colorMap.put(color, indexList); } } } // 按照条件计算错误的颜色数量 int result = 0; for (Entry<Integer, List<Integer>> colorEntry : colorMap.entrySet()) { // 无色 if (colorEntry.getKey() == 0) { continue; } List<Integer> colorIndexArray = colorEntry.getValue(); if (colorIndexArray.get(0) + n - colorIndexArray.get(colorIndexArray.size() - 1) < m) { result++; continue; } for (int i = 0; i < colorIndexArray.size() - 1; i++) { if (colorIndexArray.get(i + 1) - colorIndexArray.get(i) < m) { result++; break; } } } System.out.println(result); } }
import java.util.Scanner; //统计出每种颜色出现的行数;如果相邻两个行数之差小于m //或者最大的行数与最小的行数之差大于n-m,说明这种颜色不合要求 public class Main{ public static void main(String[] args){ Scanner input = new Scanner(System.in); while(input.hasNext()) { int n = input.nextInt(); int m = input.nextInt(); int c = input.nextInt(); int[][] color = new int[n][]; int[] num = new int[n]; for(int i = 0; i < n; i++) { num[i] = input.nextInt(); color[i] = new int[num[i]]; for(int j = 0; j < num[i]; j++) { color[i][j] = input.nextInt(); } } System.out.println(Solution(n, m, c, color)); } input.close(); } public static int Solution(int n, int m, int c, int[][] color){ int sum = 0; int[][] row = new int[c][n]; int[] k = new int[c]; for(int i = 0; i < n; i++) { for(int j = 0; j < color[i].length; j++) { row[color[i][j]-1][k[color[i][j]-1]] = i; k[color[i][j]-1]++; } } for(int i = 0; i < c; i++) { if(row[i][k[i]-1] - row[i][0] > n - m) { sum++; }else { for(int j = 1; j < k[i]; j++) { if(row[i][j] - row[i][j-1] < m) { sum++; break; } } } } return sum; } }