比赛 2012Day1 评测结果 AAAAAAAAAAAAAATTTTTT
题目名称 开车旅行 最终得分 70
用户昵称 <蒟蒻>我要喝豆奶 运行时间 6.044 s
代码语言 C++ 内存使用 1.46 MiB
提交时间 2015-10-22 21:14:14
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 100100UL
#define inf 0x7fffffff
#define eps 1e-6
using namespace std;
int n,h[MAXN],f[MAXN][2],ans1,ans2;
inline int Fbs(int x){if(x<0)return (-x);return x;}
inline double Dbs(int x){if(x<0)return (-1*x);return x;}
inline void Get_Pos(void){
	for(int i=1;i<=n;i++){
		int minn=inf,ph=inf,pos=-1;
		for(int j=i+1;j<=n;j++)
			if(Fbs(h[j]-h[i])<minn||(Fbs(h[j]-h[i])==minn&&h[j]<ph))
				minn=Fbs(h[j]-h[i]),pos=j,ph=h[j];
		f[i][0]=pos;
		minn=inf,ph=inf,pos=-1;
		for(int j=i+1;j<=n;j++)
			if((f[i][0]!=j)&&(Fbs(h[j]-h[i])<minn||(Fbs(h[j]-h[i])==minn&&h[j]<ph)))
				minn=Fbs(h[j]-h[i]),pos=j,ph=h[j];
		f[i][1]=pos;
	}	
	return ;
}
inline double Drive(int s,int x0){
	int Day=0,pos=s;
	double dista=0.0,distb=0.0;
	while(true){
		Day++;
		if((Day&1)==0){
			if(f[pos][0]==-1){break;}//can`t find more ways
			if(dista+distb+(double)Fbs(h[f[pos][0]]-h[pos])>x0)//Distance Limited
				break;
			distb+=(double)Fbs(h[f[pos][0]]-h[pos]);
			pos=f[pos][0];	
		}else{
			if(f[pos][1]==-1){break;}
			if(dista+distb+(double)Fbs(h[f[pos][1]]-h[pos])>x0)
				break;
			dista+=(double)Fbs(h[f[pos][1]]-h[pos]);
			pos=f[pos][1];
		}
	}
	if(Dbs(distb)<eps){
		return 9999999.9;		
	}else
		return (double)(dista/distb);
}
inline int Solve1(void){int x0,pos=0;
	scanf("%d",&x0);
	double now=9999999.9;
	for(int i=1;i<=n;i++){
		double temp=Drive(i,x0);
		if((temp<now)||(temp==now&&h[pos]<h[i]))
			now=temp,pos=i;
	}
	return (pos==0?2:pos);
}
inline void Drive_Again(int s,int x0){
	int Day=0,pos=s,dista=0,distb=0;
	while(true){
		Day++;
		if((Day&1)==0){//odd
			if(f[pos][0]==-1){break;}//can`t find more ways
			if(dista+distb+Fbs(h[f[pos][0]]-h[pos])>x0)//Distance Limited
				break;
			distb+=Fbs(h[f[pos][0]]-h[pos]);
			pos=f[pos][0];	
		}else{//even
			if(f[pos][1]==-1){break;}
			if(dista+distb+Fbs(h[f[pos][1]]-h[pos])>x0)
				break;
			dista+=Fbs(h[f[pos][1]]-h[pos]);
			pos=f[pos][1];
		}
	}
	ans1=dista,ans2=distb;
	return ;
}
inline void Solve2(void){
	int T,s,x;
	scanf("%d",&T);
	while(T){
		T--;
		scanf("%d%d",&s,&x);
		Drive_Again(s,x);
		printf("%d %d\n",ans1,ans2);
	}return ;
}
inline void Debug(void){
	for(int i=1;i<=n;i++) 
		printf("%d ",f[i][0]);
	printf("\n");
	for(int i=1;i<=n;i++)
		 printf("%d ",f[i][1]);
	return ;
}
int main(){
	freopen("drive.in","r",stdin);
	freopen("drive.out","w",stdout);
	h[0]=inf;
	memset(f,-1,sizeof f);
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&h[i]);
	Get_Pos();//O(n^2)
//	Debug();
	printf("%d\n",Solve1());
	Solve2();
	return 0;
}/*
10
4 5 6 1 2 3 7 8 9 10
7
10
1 7
2 7
3 7
4 7
5 7
6 7
7 7
8 7
9 7
10 7 
*/