第九届蓝桥杯 JavaB 日志统计

第九届蓝桥杯 JavaB 日志统计


标题:日志统计

小明维护着一个程序员论坛。现在他收集了一份"点赞"日志,日志共有N行。其中每一行的格式是:

ts id

表示在ts时刻编号id的帖子收到一个"赞"。

现在小明想统计有哪些帖子曾经是"热帖"。如果一个帖子曾在任意一个长度为D的时间段内收到不少于K个赞,小明就认为这个帖子曾是"热帖"。

具体来说,如果存在某个时刻T满足该帖在[T, T+D)这段时间内(注意是左闭右开区间)收到不少于K个赞,该帖就曾是"热帖"。

给定日志,请你帮助小明统计出所有曾是"热帖"的帖子编号。

【输入格式】
第一行包含三个整数N、D和K。
以下N行每行一条日志,包含两个整数ts和id。

对于50%的数据,1 <= K <= N <= 1000
对于100%的数据,1 <= K <= N <= 100000 0 <= ts <= 100000 0 <= id <= 100000

【输出格式】
按从小到大的顺序输出热帖id。每个id一行。

【输入样例】
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3

扫描二维码关注公众号,回复: 6024901 查看本文章

【输出样例】
1
3

资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 1000ms


法一:
思路:
存储每个帖子被点赞时间。尺取法判断。

参考链接


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年3月10日 下午6:11:53
 *
 */

public class 编程8日志统计 {
	static int n, d, k;
	static int time, id;
	static int l, r;
	// TreeMap按id从小到大排序
	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++;
				}
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_36477987/article/details/89542261