优化后怎么还是70分?TLE
查看原帖
优化后怎么还是70分?TLE
173864
NaN_HQJ2007_NaN楼主2021/3/27 18:47
#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 5;
int n, h[N], goa[N][25][3], gob[N];
struct node {
	int d, hi, id;
	node(int d = 0, int hi = 0, int id = 0):d(d), hi(hi), id(id){}
}tmp[N];
set<int> s;
map<int, int> m;
int dis(int i, int j) {
	return abs(h[i] - h[j]);
}
bool cmp(node x, node y) {
	if (x.d != y.d) return x.d < y.d;
	else return x.hi < y.hi;
}
pair<int, int> run(int i, int x) {
	int ans1 = 0, ans2 = 0;
	for (int j = 20; j >= 0; --j) {
		if (goa[i][j][0] == 0 || ans1 + ans2 + goa[i][j][1] + goa[i][j][2] > x) continue;
		ans1 += goa[i][j][1];
		ans2 += goa[i][j][2];
		i = goa[i][j][0];
	}
	return make_pair(ans1, ans2);
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", &h[i]);
	for (int i = n; i >= 1; --i) {
		int tot = 0;
		set<int>::iterator it = lower_bound(s.begin(), s.end(), h[i]), it2;
		if (i != n) {
			if (it != s.end()) {
				int th = abs((*it) - h[i]), tid = m[*it], td = *it;
				tmp[++tot] = node(th, td, tid);
			}
			//it;
			it2 = it;
			++it2;
			if (it != s.end() && it2 != s.end()) {
				int th = abs((*it2) - h[i]), tid = m[*it2], td = *it2;
				tmp[++tot] = node(th, td, tid);
			}
			//it + 1;
			it2 = it;
			++it2;
			if (it != s.end() && it2 != s.end()) {
				++it2;
				if (it2 != s.end()) {
					int th = abs((*it2) - h[i]), tid = m[*it2], td = *it2;
					tmp[++tot] = node(th, td, tid);
				}
			}
			//it + 2;
			it2 = it;
			--it2;
			if (it != s.begin()) {
				int th = abs((*it2) - h[i]), tid = m[*it2], td = *it2;
				tmp[++tot] = node(th, td, tid);
			}
			//it - 1;
			it2 = it;
			--it2;
			if (it != s.begin() && it2 != s.begin()) {
				--it2;
				int th = abs((*it2) - h[i]), tid = m[*it2], td = *it2;
				tmp[++tot] = node(th, td, tid);
			}
			//it - 2;
			sort(tmp + 1, tmp + tot + 1, cmp);
			if (tot >= 1) gob[i] = tmp[1].id;
			if (tot >= 2){
				goa[i][0][0] = tmp[2].id;
				goa[i][0][1] = tmp[2].d;
			}
		}
		s.insert(h[i]);
		m[h[i]] = i;
	}
	for (int i = 1; i <= n; ++i) {
		goa[i][1][0] = gob[goa[i][0][0]];
		goa[i][1][1] = goa[i][0][1];
		goa[i][1][2] = dis(goa[i][0][0], gob[goa[i][0][0]]);
	}
	for (int j = 2; j <= 20; ++j) {
		for (int i = 1; i <= n; ++i) {
			goa[i][j][0] = goa[goa[i][j - 1][0]][j - 1][0];
			goa[i][j][1] = goa[i][j - 1][1] + goa[goa[i][j - 1][0]][j - 1][1];
			goa[i][j][2] = goa[i][j - 1][2] + goa[goa[i][j - 1][0]][j - 1][2];
		}
	}
	int x0, ans2;
	double ans1 = 1e18;
	scanf("%d", &x0);
	for (int i = 1; i <= n; ++i) {
		pair<int, int> p = run(i, x0);
		double t = (double)(p.first) / p.second;
		if (t < ans1) {
			ans1 = t;
			ans2 = i;
		}
	}
	printf("%d\n", ans2);
	int q;
	scanf("%d", &q);
	while (q--) {
		int s, x;
		scanf("%d%d", &s, &x);
		pair<int, int> p = run(s, x);
		printf("%d %d\n", p.first, p.second);
	}
	return 0;
}
2021/3/27 18:47
加载中...