跳到正文

题解

题解:SP3867 VBOSS - Who is The Boss

TLEZPB约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年7月7日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 2 分钟
题目
SP3867

版本

  1. v2当前
  2. v1

Who’s the boss?

题目传送门。

题意:在所有人中找比这个员工高的,就是他的上司(总裁也就是没有人比他/她高)。

思路:看完数据点后,发现可以暴力,用结构体存每个员工,然后用 O(n2)O(n^2) 的暴力预处理。

易错:输出时要 −1-1,因为自己不是自己的下属。

AC code:

C++
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int boss[10000005], cnt[10000005];

struct node {
	int id, money, height;
}a[300005];

bool cmp(node z, node pb) {
	return z.money < pb.money;
}

signed main() {
	int n, m, q;
	cin >> n;
	while(n--) {
		memset(boss, 0, sizeof(boss));//初始化
		cin >> m >> q;
		for(int i = 1; i <= m; i++) {//输入
			cin >> a[i].id >> a[i].money >> a[i].height;
			cnt[a[i].id] = 1;
		}
		sort(a + 1, a + m + 1, cmp);
		for(int i = 1; i <= m; i++) {//预处理
			for(int j = i + 1; j <= m; j++) {
				if(a[j].height >= a[i].height) {
					boss[a[i].id] = a[j].id;
					cnt[a[j].id] += cnt[a[i].id];
					break;
				}
			}
		}
		while(q--) {
			int op;
			cin >> op;
			cout << boss[op] << ' ' << cnt[op] - 1 << endl;
		}
	}
}