public class solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); Map<Integer,List> map = new HashMap<>(); int before = -1; int result = 0; for(int i=0;i<n;i++){ int num = scanner.nextInt(); if (map.containsKey(num)){ map.get(num).add(i); if (map.get(num).size() >= m){ List<Integer> list = map.get(num); result += (n-i)*(list.get(0)-before); map.put(num,list); before = list.get(0); list.remove(0); } } else { List list = new LinkedList(); list.add(i); map.put(num,list); } } System.out.println(result); }
点赞 评论

相关推荐

牛客网
牛客网在线编程
牛客网题解
牛客企业服务