2026天梯赛普及赛P-8 从前有座山 - 大数据版
·

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class Main {
static int[][] b;
static int[] a;
static int[] cnt;
static boolean[] flag;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
while (n-- > 0) {
solve(br);
}
}
private static void solve(BufferedReader br) throws IOException {
// 初始化数据结构
Map<Integer, Integer> mp = new HashMap<>(); // 映射数值到唯一索引
a = new int[10010]; // 存储输入序列
b = new int[10010][2]; // 存储每个数值对应的后两位
flag = new boolean[10010]; // 标记是否后两位不一致
cnt = new int[10010]; // 统计每个数值的有效出现次数
int idx = 0; // 序列索引
int mpi = 0; // 映射索引
// 读取输入序列(以-1结束)
String[] nums = br.readLine().split(" ");
for (String numStr : nums) {
int x = Integer.parseInt(numStr);
if (x == -1) break;
a[++idx] = x;
// 为数值分配唯一映射索引
if (!mp.containsKey(x)) {
mp.put(x, ++mpi);
}
}
// 标记最后两个数为无效(避免越界)
if (idx >= 2) {
int last1 = mp.get(a[idx]);
int last2 = mp.get(a[idx - 1]);
flag[last1] = true;
flag[last2] = true;
}
// 第一步:检查每个数值的后两位是否一致
for (int i = 1; i <= idx - 2; i++) { // 确保i+2不越界
int x = mp.get(a[i]);
if (flag[x]) continue; // 已标记为无效,跳过
// 首次出现,记录后两位
if (b[x][0] == 0 || b[x][1] == 0) {
b[x][0] = a[i + 1];
b[x][1] = a[i + 2];
} else {
// 后两位不一致,标记为无效
if (b[x][0] != a[i + 1] || b[x][1] != a[i + 2]) {
flag[x] = true;
}
}
}
// 第二步:找最早出现且次数≥2的有效数值
for (int i = 1; i <= idx - 2; i++) {
int x = mp.get(a[i]);
if (flag[x]) continue; // 无效数值,跳过
cnt[x]++;
// 满足条件,输出结果并返回
if (cnt[x] >= 2) {
System.out.printf("%d %d %d\n", a[i], a[i + 1], a[i + 2]);
return;
}
}
// 无满足条件的三元组
System.out.println("NONE");
}
}
更多推荐
所有评论(0)