1238.日志统计
小明维护着一个程序员论坛。现在他收集了一份”点赞”日志,日志共有 NN 行。
其中每一行的格式是:
ts id
表示在 tsts 时刻编号 idid 的帖子收到一个”赞”。
现在小明想统计有哪些帖子曾经是”热帖”。
如果一个帖子曾在任意一个长度为 DD 的时间段内收到不少于 KK 个赞,小明就认为这个帖子曾是”热帖”。
具体来说,如果存在某个时刻 TT 满足该帖在 [T,T+D)[T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 KK 个赞,该帖就曾是”热帖”。
给定日志,请你帮助小明统计出所有曾是”热帖”的帖子编号。
输入格式
第一行包含三个整数 N,D,KN,D,K。
以下 NN 行每行一条日志,包含两个整数 tsts 和 idid。
输出格式
按从小到大的顺序输出热帖 idid。
每个 idid 占一行。
数据范围
1≤K≤N≤1051≤K≤N≤105,
0≤ts,id≤1050≤ts,id≤105,
1≤D≤100001≤D≤10000
输入样例:
7 10 2
0 1
0 10
10 10
10 1
9 1
100 3
100 3
输出样例:
1
3
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
String[] input = br.readLine().split(" ");
int n = Integer.parseInt(input[0]);
int d = Integer.parseInt(input[1]);
int k = Integer.parseInt(input[2]);
pill[] p = new pill[n + 10];
int[] cnt = new int[100010];
boolean[] st = new boolean[100010];
for(int i = 0; i < n; i++) {
String[] s = br.readLine().split(" ");
p[i] = new pill(Integer.parseInt(s[0]),Integer.parseInt(s[1]));
}
//按 时间排序
Arrays.parallelSort(p,0,n);
for(int i = 0, j = 0; i < n; i++) {
int id = p[i].y;
cnt[id] ++;
//当 时间跨度 大于d时, 将前面的剪掉
while(p[i].x - p[j].x >= d) {
cnt[p[j].y] --;
j ++;
}
//判断是否 是热贴
if(cnt[id] >= k)st[id] = true;
}
for(int i = 0; i < 100010; i++)
if(st[i])bw.write(i + "\n");
bw.flush();
bw.close();
}
}
class pill implements Comparable<pill>{
int x,y;
pill(int x, int y){
this.x = x;
this.y = y;
}
@Override
public int compareTo(pill o) {
if(o.x == x)return y - o.y;
else return x - o.x;
}
}
1101.献给阿尔吉侬的花束
最简单的bfs模板
阿尔吉侬是一只聪明又慵懒的小白鼠,它最擅长的就是走各种各样的迷宫。
今天它要挑战一个非常大的迷宫,研究员们为了鼓励阿尔吉侬尽快到达终点,就在终点放了一块阿尔吉侬最喜欢的奶酪。
现在研究员们想知道,如果阿尔吉侬足够聪明,它最少需要多少时间就能吃到奶酪。
迷宫用一个 R×CR×C 的字符矩阵来表示。
字符 S 表示阿尔吉侬所在的位置,字符 E 表示奶酪所在的位置,字符 # 表示墙壁,字符 . 表示可以通行。
阿尔吉侬在 1 个单位时间内可以从当前的位置走到它上下左右四个方向上的任意一个位置,但不能走出地图边界。
输入格式
第一行是一个正整数 TT,表示一共有 TT 组数据。
每一组数据的第一行包含了两个用空格分开的正整数 RR 和 CC,表示地图是一个 R×CR×C 的矩阵。
接下来的 RR 行描述了地图的具体内容,每一行包含了 CC 个字符。字符含义如题目描述中所述。保证有且仅有一个 S 和 E。
输出格式
对于每一组数据,输出阿尔吉侬吃到奶酪的最少单位时间。
若阿尔吉侬无法吃到奶酪,则输出“oop!”(只输出引号里面的内容,不输出引号)。
每组数据的输出结果占一行。
数据范围
1<T≤101<T≤10,
2≤R,C≤2002≤R,C≤200
输入样例:
3
3 4
.S..
###.
..E.
3 4
.S..
.E..
....
3 4
.S..
####
..E.
输出样例:
5
1
oop!
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.Scanner;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(System.in);
static int n,m;
static int[] dx = {
-1,1,0,0},dy = {
0,0,1,-1};
public static void main(String[] args) {
int T = sc.nextInt();
while(T -- > 0) {
n = sc.nextInt();m = sc.nextInt();
char[][] maze = new char[n + 5][m + 5];
int start_x = 0,start_y = 0,end_x = 0,end_y = 0;
for(int i = 1; i <= n; i++ ) {
String input = sc.next();
// System.out.println(input);
for(int j = 1; j <= m; j++) {
char ch = input.charAt(j - 1);
maze[i][j] = ch;
if(ch == 'S') {
start_x = i;start_y = j;
}
if(ch == 'E') {
end_x = i; end_y = j;
}
}
}
int ans = bfs(start_x,start_y, end_x, end_y,maze);
if(ans == -1)System.out.println("oop!");
else System.out.println(ans);
}
}
private static int bfs(int start_x,int start_y,int end_x,int end_y,char