天啊
查看原帖
天啊
305002
vegetable_ste楼主2021/11/11 17:41

为什么我的暴力代码连样例都过不掉还有75pts??!!

我输中量发现nxt求错了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
#define mp make_pair
const int N = 1e5 + 10;
int n, m, xx, h[N], nxt[N][3]; // 0: minimum 1: 2nd-minimum 
template<class T>
T read(T& x) {
	x = 0; T sign = 1, ch = getchar();
	while(!isdigit(ch) && ch != '-') ch = getchar();
	if(ch == '-') { sign = -1; ch = getchar(); }
	while(isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); }
	x *= sign;
	return x;
}
void init() {
	set<int> st;
	unordered_map<int, int> id;
	for(int i = 1; i <= n; i ++ ) id[h[i]] = i;
	for(int i = n; i >= 1; i -- ) {
		if(i == n) { st.insert(h[i]); continue; }
		auto it = st.lower_bound(h[i]);
		auto fp = st.begin(), bp = st.end(); bp -- ;
		if(it == st.end()) {
			nxt[i][0] = id[*bp];
			if(st.size() > 1) {
				auto it2 = bp; it2 -- ;
				nxt[i][1] = id[*it2];
			}
		} else if(it == fp) { 
			nxt[i][0] = id[*it];
			if(st.size() > 1) {
				auto it2 = it; it2 ++ ;
				nxt[i][1] = id[*it2];
			}
		} else if(it == bp) {
			nxt[i][0] = id[*it];
			if(st.size() > 1) {
				auto it2 = it; it2 -- ;
				nxt[i][1] = id[*it2];
				if(abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
				|| (abs(h[nxt[i][1]] - h[i]) == abs(h[nxt[i][0]] - h[i])
				 && h[nxt[i][1]] < h[nxt[i][0]]))
					swap(nxt[i][0], nxt[i][1]);
				if(it2 != bp) {
					auto it3 = it2; it3 -- ;
					if(abs(*it3 - h[i]) < abs(h[nxt[i][1]] - h[i])) nxt[i][1] = id[*it3];
					if(abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
					|| (abs(h[nxt[i][1]] - h[i]) == abs(h[nxt[i][0]] - h[i])
					 && h[nxt[i][1]] < h[nxt[i][0]]))
						swap(nxt[i][0], nxt[i][1]);
				}
			}
		} else {
			auto Lit = it, Rit = it;
			Lit -- ; 
			nxt[i][0] = id[*Lit];
			nxt[i][1] = id[*Rit];
			if(abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
			|| (abs(h[nxt[i][1]] - h[i]) == abs(h[nxt[i][0]] - h[i])
			 && h[nxt[i][1]] < h[nxt[i][0]]))
				swap(nxt[i][0], nxt[i][1]);
			if(Lit != fp) { 
				Lit -- ;
				if(abs(*Lit - h[i]) < abs(h[nxt[i][1]] - h[i])) nxt[i][1] = id[*Lit];
				if(abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
				|| (abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
				 && h[nxt[i][1]] < h[nxt[i][0]]))
					swap(nxt[i][0], nxt[i][1]);
			}
			if(Rit != bp) { 
				Rit ++ ;
				if(abs(*Rit - h[i]) < abs(h[nxt[i][1]] - h[i])) nxt[i][1] = id[*Rit];
				if(abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
				|| (abs(h[nxt[i][1]] - h[i]) < abs(h[nxt[i][0]] - h[i])
				 && h[nxt[i][1]] < h[nxt[i][0]]))
					swap(nxt[i][0], nxt[i][1]);
			}
		}
		st.insert(h[i]); 
	}
}
void solve(int pos, int len, int& xa, int& xb) {
	xa = xb = 0;
	bool st = 1;
	while(1) {
		st ^= 1;
		if(!st) { // A
			int ne = nxt[pos][1];
			if(!ne) break;
			if(xa + xb + abs(h[ne] - h[pos]) > len) break;
			xa += abs(h[ne] - h[pos]);
			pos = ne;
		} else { // B
			int ne = nxt[pos][0];
			if(!ne) break;
			if(xa + xb + abs(h[ne] - h[pos]) > len) break;
			xb += abs(h[ne] - h[pos]);
			pos = ne;
		}
	}
}
int main() {
	read(n);
	for(int i = 1; i <= n; i ++ ) read(h[i]);
	init();
	read(xx);
	double minn = 0x3f3f3f3f; int pos = 0;
	for(int i = 1; i <= n; i ++ ) {
		int xa, xb;
		solve(i, xx, xa, xb);
		if(!xb) {
			if(!pos) pos = i;
			continue;
		}
		double rating = (double)(xa) / xb;
		if(rating < minn || (rating == minn && h[i] > h[pos])) { minn = rating; pos = i; }
	}
	printf("%d\n", pos);
	read(m);
	for(int i = 1; i <= m; i ++ ) {
		int s, x, xa = 0, xb = 0; read(s); read(x);
		solve(s, x, xa, xb);
		printf("%d %d\n", xa, xb);
	}
	return 0;
}
2021/11/11 17:41
加载中...