题解:P10300 [THUWC 2020] 工资分配
Hey3_smallZ · · 题解
首先不难打出暴力代码,荣获
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q, k;
struct node {
int who;
vector<int> b;
}t[100005];
int main () {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q >> k;
for (int i = 1; i <= n; i++) {
cin >> t[i].who;
t[i].b.resize(k + 5);
for (int j = 1; j <= k; j++)
cin >> t[i].b[j];
}
while (q--) {
vector<int> a(k + 5);
for (int i = 1; i <= k; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
if (t[i].b[t[i].who] > a[t[i].who])
a = t[i].b;
for (int i = 1; i <= k; i++)
cout << a[i] << ' ';
cout << '\n';
}
return 0;
}
尝试优化,发现我们 TLE 的原因是枚举
我们定义一个
但是我们怎么找
但是又有问题出现了:我们又怎么快速找到
为什么 ST 表会有一个员工编号
最后算一下时间复杂度:
AC 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 5, LOG = 17;
int n, q, k;
ll st[25][N][LOG];
int up[LOG][N];
struct node {
int who;
vector<int> b;
}t[N];
int find (int id, int s, int x) {
int l = s, r = n - 1;
while (l <= r) {
int mid = (l + r) >> 1, go = __lg(mid - l + 1);
if (max(st[id][l][go], st[id][mid - (1 << go) + 1][go]) <= x)
l = mid + 1;
else
r = mid - 1;
}
return l;
}
void init () {
for (int id = 0; id < k; id++) {
for (int i = 0; i < n; i++)
if (t[i].who == id)
st[id][i][0] = t[i].b[id];
for (int j = 1; j < LOG; j++)
for (int i = 0; i + (1 << j) <= n; i++)
st[id][i][j] = max(st[id][i][j - 1], st[id][i + (1 << (j - 1))][j - 1]);
}
for (int i = 0; i < n; i++) {
int pos = n;
for (int id = 0; id < k; id++)
pos = min(pos, find(id, i + 1, t[i].b[id]));
up[0][i] = (pos == n ? i : pos);
}
for (int j = 1; j < LOG; j++)
for (int i = 0; i < n; i++)
up[j][i] = up[j - 1][up[j - 1][i]];
}
int main () {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> q >> k;
for (int i = 0; i < n; i++) {
cin >> t[i].who;
t[i].who--;
t[i].b.resize(k);
for (int j = 0; j < k; j++)
cin >> t[i].b[j];
}
init();
while (q--) {
vector<int> a(k);
for (int i = 0; i < k; i++)
cin >> a[i];
int pos = n;
for (int id = 0; id < k; id++)
pos = min(pos, find(id, 0, a[id]));
if (pos == n) {
for (int x : a)
cout << x << ' ';
cout << '\n';
continue;
}
for (int j = LOG - 1; j >= 0; j--)
if (up[j][pos] != pos)
pos = up[j][pos];
for (int x : t[pos].b)
cout << x << ' ';
cout << '\n';
}
return 0;
}
这是一位自宅警备员的第十篇题解,可以点个赞吗求求了