QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#332146#1375. TripTikishmeal#WA 1ms3772kbC++232.9kb2024-02-19 10:26:202024-02-19 10:26:21

Judging History

你现在查看的是最新测评结果

  • [2024-02-19 10:26:21]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3772kb
  • [2024-02-19 10:26:20]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

int k;
map<int,int> weight; // coords, weight

struct RMQ {
	vector<vector<vector<int>>> jmp;
	vector<int> join(vector<int> &a, vector<int> &b) {
		vector<int> ret; ret.reserve(k);

		auto insertt = [&](int i) {
			if (!count(ret.begin(),ret.end(),i)) ret.emplace_back(i);
		};

		auto aa = a.begin(), bb = b.begin();
		while (aa != a.end() and bb != b.end() and ret.size() < k) {
			if (weight[*aa] > weight[*bb]) insertt(*aa), aa++;
			else insertt(*bb), bb++;
		}
		while (aa != a.end() and ret.size() < k) insertt(*aa), aa++;
		while (bb != b.end() and ret.size() < k) insertt(*bb), bb++;
		while (ret.size() and ret.back() == 0) ret.pop_back();
		return ret;
	}
	RMQ(const vector<vector<int>>& V) : jmp(1, V) {
		for (int pw = 1, k = 1; pw * 2 <= V.size(); pw *= 2, ++k) {
			jmp.emplace_back(V.size() - pw * 2 + 1);
			for (int j = 0; j < jmp[k].size(); j++) {
				jmp[k][j] = join(jmp[k-1][j], jmp[k-1][j+pw]);
			}
		}
	}
	vector<int> query(int a, int b) {
		assert(a <= b);
		if (a == b) return vector<int>();
		int dep = 31 - __builtin_clz(b - a);
		return join(jmp[dep][a], jmp[dep][b-(1<<dep)]);
	}
};


int main() {
	cin.tie(0)->ios_base::sync_with_stdio(0);

	int n; cin >> n >> k;
	vector<vector<int>> v(n+1);
	vector<int> output(n+1); // ind to output order
	map<int,int> inds; inds[0]; weight[0] = -1; // coord, index
	for (int i = 0; i < n; i++) {
		int x; cin >> x;
		weight[x] = i;
		inds[x];
	}
	{
		int i = 0;
		for (auto &[j, k] : inds) {
			k = i;
			v[i].emplace_back(j);
			output[i] = weight[j];
			i++;
		}
	}
	RMQ rmq(v); // coords

	vector vis(n+1, vector(30, -1));
	priority_queue<tuple<int,int,int>> q; // dist, ind, radius
	q.emplace(-0, inds[0],0), vis[inds[0]][0] = 0;
	while (q.size()) {
		auto [d,ind,r] = q.top(); q.pop();
		d = -d;
		if (d > vis[ind][r]) continue;
		for (int j = 0; j < r; j++) {
			if (vis[ind][j] != -1 and vis[ind][j] <= d+r-j) continue;
			q.emplace(-(d+r-j),ind,j);
			vis[ind][j] = d+r-j;
		}
		for (int j = r+1; j < 30; j++) {
			if (vis[ind][j] != -1 and vis[ind][j] <= d+j-r) continue;
			q.emplace(-(d+j-r),ind,j);
			vis[ind][j] = d+j-r;
		}
		auto it = inds.upper_bound(v[ind][0]+(1<<r));
		for (int j : rmq.query(inds.lower_bound(v[ind][0]-(1<<r))->second, it == inds.end() ? n+1 : it->second)) {
			if (vis[inds[j]][r] != -1 and vis[inds[j]][r] <= d+1) continue;
			q.emplace(-d-1,inds[j],r);
			vis[inds[j]][r] = d+1;
		}
	}

	vector<int> ans(n);
	for (int i = 0; i < n+1; i++) {
		if (output[i] == -1) continue;
		int mn = INT_MAX;
		int mx = 29;
		while (mx >= 0) {
			auto it = inds.upper_bound(v[i][0]+(1<<mx));
			auto vv = rmq.query(inds.lower_bound(v[i][0]-(1<<mx))->second, it == inds.end() ? n+1 : it->second);
			if (count(vv.begin(),vv.end(),v[i][0])) break;
			mx--;
		}
		for (int j = 0; j <= mx; j++) if (vis[i][j] != -1) mn = min(mn, vis[i][j]);
		if (mn == INT_MAX) mn = -1;
		ans[output[i]] = mn;
	}

	for (int i : ans) cout << i << '\n';
}

详细

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 3772kb

input:

4 2
-2
-1
-3
2

output:

-1
1
3
2

result:

wrong answer 1st lines differ - expected: '3', found: '-1'