95pts WA on#8
查看原帖
95pts WA on#8
999274
CNS_5t0_0r2楼主2024/11/1 13:48
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 9,LOGN = 20,INF = 2e9;
int n,q,logn,x;
multiset<pair<int,int> > s;
int h[N];
int dist(int i,int j){
	return abs(h[i] - h[j]);
}
int Next[N][LOGN][2];
int dis_a[N][LOGN][2],dis_b[N][LOGN][2];
void init(){
	for(int j = 1;j <= logn;j++)
		for(int i = 1;i <= n;i++)
			for(int k = 0;k <= 1;k++){
				if(j == 1){
					Next[i][j][k] = Next[Next[i][j - 1][k]][j - 1][1 - k];
					dis_a[i][j][k] = dis_a[i][j - 1][k] + dis_a[Next[i][0][k]][j - 1][1 - k];
					dis_b[i][j][k] = dis_b[i][j - 1][k] + dis_b[Next[i][0][k]][j - 1][1 - k];
				}
				else{
					Next[i][j][k] = Next[Next[i][j - 1][k]][j - 1][k];
					dis_a[i][j][k] = dis_a[i][j - 1][k] + dis_a[Next[i][j - 1][k]][j - 1][k];
					dis_b[i][j][k] = dis_b[i][j - 1][k] + dis_b[Next[i][j - 1][k]][j - 1][k];
				}
			}
}
pair<int,int> calc(int pos,int x){//返回从pos出发最多行驶x公里所走的最长路程。 
	int a = 0,b = 0;
	for(int i = logn;i >= 0;i--)
		if(Next[pos][i][0] && a + b + dis_a[pos][i][0] + dis_b[pos][i][0] <= x){
			a += dis_a[pos][i][0];
			b += dis_b[pos][i][0];
			pos = Next[pos][i][0];
		}
	return make_pair(a,b);
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin >> n;
	logn = log2(n) + 1;
	for(int i = 1;i <= n;i++)
		cin >> h[i];
	h[0] = INF;h[n + 1] = -INF;
	s.insert(make_pair(h[0],0));
	s.insert(make_pair(h[0],0));
	s.insert(make_pair(h[n + 1],n + 1));
	s.insert(make_pair(h[n + 1],n + 1));
	for(int i = n;i >= 1;i--){
        s.insert(make_pair(h[i],i));
        multiset<pair<int, int> >::iterator it = s.find(make_pair(h[i],i));
        pair<int, int> pre,nex;
        it--;
        pre = make_pair((*it).first,(*it).second);
        it++;it++;
        nex = make_pair((*it).first,(*it).second);
        it--;
        if(abs(h[i] - pre.first) <= abs(h[i] - nex.first)){
        	Next[i][0][1] = pre.second;
        	it--;it--;
	        if(abs(h[i] - nex.first) <= abs(h[i] - (*it).first))
        		Next[i][0][0] = nex.second;
        	else
        		Next[i][0][0] = (*it).second;
        	it++;it++;
		}
		else{
        	Next[i][0][1] = nex.second;
        	it++;it++;
	        if(abs(h[i] - pre.first) <= abs(h[i] - (*it).first))
        		Next[i][0][0] = pre.second;
        	else
        		Next[i][0][0] = (*it).second;
        	it--;it--;
		}
        dis_a[i][0][0] = dist(i,Next[i][0][0]);
        dis_b[i][0][1] = dist(i,Next[i][0][1]);
	}
	init();
	cin >> x;
	int ans1 = 0,ans2 = 0;
	long double Max = INF * 1.0;
	for(int i = 1;i <= n;i++){
		pair<int,int> tmp = calc(i,x);
		long double tmp2 = (long double)tmp.first / (long double)tmp.second;
		if(tmp2 < Max || (tmp2 == Max && h[i] > h[ans1])){
			ans1 = i;
			Max = tmp2;
		}
	}
	cout << ans1 << '\n';
	cin >> q;
	while(q--){
		int st;
		cin >> st >> x;
		pair<int,int> tmp = calc(st,x);
		cout << tmp.first << ' ' << tmp.second << '\n';
	}
	return 0;
}
2024/11/1 13:48
加载中...