开车旅行
题解:
遍历起点就OK
#include<bits/stdc++.h>
#define ll long long
#define inf 10000000000
using namespace std;
ll n;
ll X;
ll H[100010];
ll wheA[100010][2];
ll wheB[100010][2];
ll s,m;
long double ans1=inf;
ll read(){
ll num=0,f=1;
char ch;
ch=getchar();
while(ch>'9'||ch<'0'){
if(ch=='-') f=-1;
ch=getchar();
}
while(ch<='9'&&ch>='0'){
num=(num<<3)+(num<<1)+ch-'0';
ch=getchar();
}
return f*num;
}
long double dfs(ll S,ll x,bool who,ll AS,ll BS){
// if(AS+BS>S) return inf;
if(x>=n){
if(BS==0){
return inf;
}
return (long double)AS/(long double)BS;
}
if(!who){
if(AS+wheA[x][1]+BS<=S){
return dfs(S,wheA[x][0],1,AS+wheA[x][1],BS);
}
}
else {
if(AS+wheB[x][1]+BS<=S){
return dfs(S,wheB[x][0],0,AS,BS+wheB[x][1]);
}
}
if(BS==0){
return inf;
}
return (long double)AS/(long double)BS;
}
void dfs_(ll S,ll x,bool who,ll AS,ll BS){
if(x>=n){
printf("%lld %lld\n",AS,BS);
return;
}
if(!who){
if(AS+wheA[x][1]+BS<=S){
dfs_(S,wheA[x][0],1,AS+wheA[x][1],BS);
return;
}
}
else {
if(AS+wheB[x][1]+BS<=S){
dfs_(S,wheB[x][0],0,AS,BS+wheB[x][1]);
return;
}
}
printf("%lld %lld\n",AS,BS);
}
int main(){
n=read();
for(ll i=1;i<=n;i++){
H[i]=read();
}
H[n+1]=inf;
for(ll i=1;i<=n-1;i++){
ll x=i+1;
ll y=i+2;
ll t;
if((abs(H[x]-H[i])>abs(H[y]-H[i]))||(((abs(H[x]-H[i])==abs(H[y]-H[i])))&&(H[y]-H[i]<0)))
swap(x,y);
for(ll j=i+3;j<=n;j++){
if((abs(H[x]-H[i])>abs(H[j]-H[i]))||(((abs(H[x]-H[i])==abs(H[j]-H[i])))&&(H[j]-H[i]<0))){
t=x;
x=j;
y=t;
continue;
}
if(((abs(H[x]-H[i])<abs(H[j]-H[i]))&&(abs(H[j]-H[i])<abs(H[y]-H[i])))||((abs(H[j]-H[i])==abs(H[y]-H[i]))&&(H[j]-H[i]<0))){
y=j;
continue;
}
}
wheA[i][0]=y;
wheA[i][1]=abs(H[y]-H[i]);
wheB[i][0]=x;
wheB[i][1]=abs(H[x]-H[i]);
}
X=read();
for(ll i=1;i<n;i++){
long double w=dfs(X,i,0,0,0);
if(ans1>w||(ans1==w&&H[i]>H[s])){
ans1=w;
s=i;
}
}
printf("%lld\n",s);
m=read();
while(m--){
int a,b;
a=read();
b=read();
dfs_(b,a,0,0,0);
}
}