Topic background description Format input format output lattice model Example 1 limit hint problem code
Topic background
The little firs finally entered the heavens. He saw everyone carrying a pair of invisible wings, and he wanted to.
(How did the little cedar see it?) ...... ) describes
The angel tells the small fir, each wing has the length, the two wing's length ratio is closer to the Gold division ratio, the more perfect.
Now the angel gave the little Fir N wings, and the little firs wanted to pick out the perfect pair. format Input Format
For each set of test data
The first row has a number N (2<=n<=30000)
The second row has n a positive integer not exceeding 1e5, representing the length of the N wing.
20% data n<=100 output format
Output two integers for each set of test data, representing a pair of wings selected by a small fir.
Note that shorter in the front, if there is more than the perfect degree of wings, please output the smallest pair. Sample 1
Sample Input 1
4
2 3 4 6
Sample Output 1
2
3 Limit
1s tips per test point
You can think that the golden ratio is 0.6180339887498949.
Discrete (sort) + two-point
Note: in C + + The real number operation requires that at least one variable in the expression is real, and the result can be real code .
#include <algorithm> #include <stdio.h> #include <cmath> using namespace
Std
int a[30005];
Double const gold=0.6180339887498949;
int g,h;
void Best (int i,int j) {double c=a[i],d=g;
if (Fabs (c/a[j]-gold) <fabs (d/h-gold)) {g=a[i];h=a[j];
int main () {int n;
scanf ("%d", &n);
for (int i=1;i<=n;i++) scanf ("%d", &a[i]);
Sort (a+1,a+n+1);
G=A[1];H=A[2];
for (int i=1;i<=n;i++) {int l=1,r=i-1,mid;
Double C;
do {mid= (l+r)/2;
C=a[mid]; if (c/a[i]<gold) L=mid;
else R=mid;
while (L<R-1);
Best (L,i); Best (R,i);
L=i+1;r=n;
do {mid= (l+r)/2;
C=a[mid]; if (a[i]/c<gold) R=mid;
else L=mid;
while (L<R-1);
Best (i,l); Best (I,R);
printf ("%d\n%d", g,h);
return 0; }