1 #include<cstdio> 2 #include<iostream> 3 #include<iomanip> 4 #include<cmath> 5 #include<vector> 6 #include<queue> 7 #include<stack> 8 #include<bitset> 9 #include<algorithm> 10 #include<string> 11 #include<cstring> 12 using namespace std; 13 const long long w=1000000000; 14 struct data{ 15 int no; 16 long long sa,sb; 17 }; 18 data f[100005][55]; 19 int n,m,s,p,q; 20 long long a,b,k; 21 double z=1e10; 22 long long h[100005],hf[100005],hs[100005]; 23 int fir[100005],sec[100005]; 24 bool comp(int a,int b) 25 { 26 if (a<=b) return true; 27 return false; 28 } 29 void dfs(int x,int t,int w) 30 { 31 if (w%2!=0&&a+b+hs[x]<=t) 32 { 33 a+=hs[x]; 34 dfs(sec[x],t,w+1); 35 } 36 if (w%2==0&&a+b+hf[x]<=t) 37 { 38 b+=hf[x]; 39 dfs(fir[x],t,w+1); 40 } 41 return ; 42 } 43 void zzz(int x,int kk) 44 { 45 q=x; 46 for (int j=50;j>=0;j--) 47 if (f[x][j].no!=0&&f[x][j].sa<=w&&f[x][j].sb<=w&&f[x][j].sa!=0&&f[x][j].sb!=0&&f[x][j].sa+f[x][j].sb<=kk) 48 { 49 a+=f[x][j].sa; 50 b+=f[x][j].sb; 51 zzz(f[x][j].no,kk-f[x][j].sa-f[x][j].sb); 52 break; 53 } 54 return ; 55 } 56 int main() 57 { 58 freopen ("drive.in","r",stdin); 59 //freopen ("drive.out","w",stdout); 60 scanf ("%d",&n); 61 for (int i=1;i<=n;i++) scanf ("%lld",&h[i]); 62 memset (hf,127,sizeof(hf)); 63 memset (hs,127,sizeof(hs)); 64 for (int i=1;i<=n-1;i++) 65 for (int j=i+1;j<=n;j++) 66 { 67 if (hf[i]>abs(h[i]-h[j])||(hf[i]==abs(h[i]-h[j])&&h[j]<h[fir[i]])) 68 { 69 sec[i]=fir[i]; 70 hs[i]=hf[i]; 71 fir[i]=j; 72 hf[i]=abs(h[i]-h[j]); 73 } 74 else if (hs[i]>abs(h[i]-h[j])||(hs[i]==abs(h[i]-h[j])&&h[j]<h[sec[i]])) 75 { 76 hs[i]=abs(h[i]-h[j]); 77 sec[i]=j; 78 } 79 } 80 for (int i=1;i<=n;i++) 81 { 82 f[i][0].no=fir[sec[i]]; 83 f[i][0].sa=hs[i]; 84 f[i][0].sb=hf[sec[i]]; 85 } 86 for (int j=1;j<=50;j++) 87 for (int i=1;i<=n;i++) 88 { 89 f[i][j].no=f[f[i][j-1].no][j-1].no; 90 f[i][j].sa=f[i][j-1].sa+f[f[i][j-1].no][j-1].sa; 91 f[i][j].sb=f[i][j-1].sb+f[f[i][j-1].no][j-1].sb; 92 } 93 scanf ("%lld%d",&k,&m); 94 for (int i=1;i<=n;i++) 95 { 96 a=0;b=0; 97 zzz(i,k); 98 if (f[q][0].sa+a+b<=k) a+=f[q][0].sa; 99 //dfs(i,k,1); 100 if (b!=0) 101 { 102 double zz=(double)a/b;//here 103 if (z>zz||(z==zz&&h[i]>h[p])) 104 { 105 z=zz; 106 p=i; 107 } 108 } 109 } 110 printf ("%d\n",p); 111 for (int i=1;i<=m;i++) 112 { 113 scanf ("%d%lld",&s,&k); 114 a=0;b=0; 115 //dfs(s,k,1); 116 //printf ("%lld %lld\n",a,b); 117 zzz(s,k); 118 if (f[q][0].sa+a+b<=k) a+=f[q][0].sa; 119 printf ("%lld %lld\n",a,b); 120 } 121 return 0; 122 }
时间: 2024-12-18 15:04:01