95pts WA #8 求条
查看原帖
95pts WA #8 求条
730195
Little_Cabbage楼主2024/8/7 21:56
#include <bits/stdc++.h>
#define ull unsigned int
//#define int int
#define ll __int128
#define ldb long double
#define db double
#define bl bool
#define endl '\n'
#define PII pair<int, int>
#define p_q priority_queue
#define n_m unordered_map
#define il inline
#define re register
#define ve vector
#define bs bitset
#define ri register int

using namespace std;

//#define fre 1
#define IOS 1
//#define multitest 1

const int N = 1e5 + 10;
const int inf = 1e18;
const int eps = 1e-18;

int n, h[N];
int x0;
int m;

int st[N][19][3];
int sa[N][19][3];
int sb[N][19][3];

int nexta, nextb;

int ans;
ldb Max = inf;

struct Set {
	int id;
	int data;
	bool operator < (const Set &b) const {
		return data < b.data;
	}
};
multiset<Set> q;

il pair<int, int> Get_ans(int p, int x) {
	int cna = 0, cnb = 0;
	int now = p;
	for (ri i = 18; i >= 0; i -- ) {
//		cout << i << "    hh\n";
		if (st[now][i][0] && cna + cnb + sa[now][i][0] + sb[now][i][0] <= x) {
			cna += sa[now][i][0];
			cnb += sb[now][i][0];
			now = st[now][i][0];
		}
	}
	if (st[now][0][0] && sa[now][0][0] + cna + cnb <= x) cna += sa[now][0][0];
//	cout << cna << " " << cnb << endl;
	return make_pair(cna, cnb);
}

il void Init() {
	cin >> n;
	for (ri i = 1; i <= n; i ++ ) {
		cin >> h[i];
	}
	cin >> x0 >> m;
}

il void Solve() {
	Set a, Aa;
	a.id = 0;
	a.data = 2e9;
	q.insert(a);
	a.data = 2e9 - 1;
	q.insert(a);
	a.id = n + 1;
	a.data = -2e9;
	q.insert(a);
	a.data = -2e9 + 1;
	q.insert(a);
	h[0] = 2e9, h[n + 1] = -2e9;
	for (ri i = n; i > 0; i -- ) {
		Aa.id = i;
		Aa.data = h[i];
		q.insert(Aa);
		multiset<Set>::iterator it = q.lower_bound(Aa);
		int q1, h1;
		int qd, hd;
		++it;
		h1 = (*it).id;
		hd = (*it).data;
		-- it;
		-- it;
		q1 = (*it).id;
		qd = (*it).data;
		++ it;
		if (abs(hd - h[i]) >= abs(qd - h[i])) {
			nextb = q1;
			-- it;
			-- it;
			if (abs(hd - h[i]) > abs((*it).data - h[i])) nexta = (*it).id;
			else nexta = h1;
		} else {
			nextb = h1;
			++ it;
			++ it;
			if (abs(qd - h[i]) > abs((*it).data - h[i])) nexta = (*it).id;
			else nexta = q1;
		}
		st[i][0][0] = nexta;
		st[i][0][1] = nextb;
		sa[i][0][0] = abs(h[i] - h[nexta]);
		sa[i][1][0] = sa[i][0][0];
		sb[i][0][1] = abs(h[i] - h[nextb]);
		sb[i][1][1] = sb[i][0][1];
	}
	for (ri i = 1; i <= n; i ++ ) {
		st[i][1][0] = st[st[i][0][0]][0][1];
		sa[i][1][0] = sa[i][0][0];
		sb[i][1][0] = abs(h[st[i][1][0]] - h[st[i][0][0]]);
		st[i][1][1] = st[st[i][0][1]][0][0];
		sb[i][1][1] = sb[i][0][1];
		sa[i][1][1] = abs(h[st[i][1][1]] - h[st[i][0][1]]);
	}
	for (ri k = 2; k <= 18; k ++ )
		for (ri i = 1; i <= n; i ++ ) {
			st[i][k][1] = st[st[i][k - 1][1]][k - 1][1];
			sb[i][k][1] = sb[st[i][k - 1][1]][k - 1][1] + sb[i][k - 1][1];
			sa[i][k][1] = sa[i][k - 1][1] + sa[st[i][k - 1][1]][k - 1][1];
			st[i][k][0] = st[st[i][k - 1][0]][k - 1][0];
			sa[i][k][0] = sa[i][k - 1][0] + sa[st[i][k - 1][0]][k - 1][0];
			sb[i][k][0] = sb[i][k - 1][0] + sb[st[i][k - 1][0]][k - 1][0];
		}
//	cout << "Yes";
	for (ri i = 1; i <= n; i ++ ) {
		pair<int, int> pa = Get_ans(i, x0);
//		cout << i << "      %%%%%%" << endl;
		int Sa = pa.first, Sb = pa.second;
		if (!Sb) {
			if (Max - inf > eps) {
				Max = inf;
				ans = i;
			} else if (Max == inf && h[ans] < h[i]) ans = i;
		} else {
			ldb now = (ldb)Sa / (ldb)Sb;
			if (Max - now > eps) {
				Max = now;
				ans = i;
			} else if (now == inf && h[ans] < h[i]) ans = i;
		}
	}
	cout << ans << endl;
	while (m--) {
		int x;
		int y;
		cin >> x >> y;
		pair<int, int> pa = Get_ans(x, y);
		cout << pa.first << " " << pa.second << endl;
	}
}

signed main() {
	int T;
#ifdef IOS
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
#ifdef fre
	streopen("trip.in", "r", stdin);
	streopen("trip.out", "w", stdout);
#endif
#ifdef multitest
	cin >> T;
#else
	T = 1;
#endif
	while (T--) {
		Init();
		Solve();
	}
	return 0;
}
/*
5
-1000000000 0 -999999999 999999999 1000000000
1000000000
7
1 1000000000
2 1000000000
3 1000000000
4 1000000000
5 1000000000
1 2
2 3

2
1000000000 0
999999999 1
0 0
0 0
0 0
0 0
0 0

*/
2024/8/7 21:56
加载中...