JavaB log statistics of the 9th Blue Bridge Cup

Keywords: Java less

JavaB log statistics of the 9th Blue Bridge Cup

Title: log statistics

Xiaoming maintains a programmer forum. Now he has collected a "like" log with N lines. The format of each line is:

ts id

Indicates that the post with id number at ts time received a "like".

Now Xiaoming wants to count which posts were "hot posts". If a post has received at least K compliments in any time period with a length of D, Xiao Ming thinks that the post was a "hot post".

Specifically, if there is a time when t satisfies that the post receives no less than K compliments in the [T, t + D] period (note that it is left closed and right open), the post has been a "hot post".

Given the log, please help Xiaoming count all the post numbers that used to be "hot posts".

[input format]
The first line contains three integers, N, D, and K.
The following N lines contain two integers ts and id.

For 50% of data, 1 < = k < = n < = 1000
For 100% data, 1 < = k < = n < = 100000 0 < = TS < = 100000 0 < = ID < = 100000

[output format]
Output the hot post id in the order from small to large. One line per id.

[input example]
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

[sample output]
1
3

Resource conventions:
Peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

Law 1:
Train of thought:
Store the time when each post is liked. Take the rule to judge.

Reference link

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * 
 * @description TODO
 * @author frontier
 * @time 2019 6:11:53 PM, March 10, 2010
 *
 */

public class programming8Log statistics {
	static int n, d, k;
	static int time, id;
	static int l, r;
	// TreeMap sorted by id from small to large
	static Map<Integer, ArrayList<Integer>> map = new TreeMap<Integer, ArrayList<Integer>>();

	public static void main(String args[]) throws IOException {
		Scanner in = new Scanner(new File("src/JavaB/s9/8.txt"));
		n = in.nextInt();
		d = in.nextInt();
		k = in.nextInt();
		for (int i = 1; i <= n; ++i) {
			time = in.nextInt();
			id = in.nextInt();
			if (map.containsKey(id)) {
				map.get(id).add(time);
			} else {
				ArrayList<Integer> list2 = new ArrayList<Integer>();
				list2.add(time);
				map.put(id, list2);
			}
		}

		ArrayList<Entry<Integer, ArrayList<Integer>>> list = new ArrayList<Map.Entry<Integer, ArrayList<Integer>>>(
				map.entrySet());

		for (int i = 0; i < list.size(); ++i) {
			Entry<Integer, ArrayList<Integer>> entry = list.get(i);
			ArrayList<Integer> a = entry.getValue();
			Collections.sort(a);
			System.out.println(a);
			l = r = 0;

			while (l <= r && r < a.size()) {
				if (r - l + 1 >= k) {
					if (a.get(r) - a.get(l) < d) {
						System.out.println(entry.getKey());
						break;
					} else
						l++;
				} else {
					r++;
				}
			}
		}
	}
}

Posted by jcoones on Fri, 22 Nov 2019 09:43:35 -0800