The nearest taller cow

 

 

The nearest taller cow

Time Limit:3000MS  Memory Limit:65536K
Description

Farmer Zhao's N cows (1 ≤ N ≤ 1,000,000) are lined up in a row. So each cow can see the nearest cow which is taller than it. You task is simple, given the height (0 < height ≤ 10^9) of each cow lined up in the row, to calculate the distance between each cow and its nearest taller cow, if it is the tallest cow in the row, such distance is regarded as N. You should output the average distance.

Input

For each test case:
Line 1: One integers, N
Lines 2: N integers. The ith integer is the height of the ith cow in the row.

Output

The average distance to their nearest taller cow, rounded up to 2 decimals.

Sample Input

7
7 6 5 8 6 4 10

Sample Output

2.43

 

 

#include<stdio.h>
#include<string.h>
int a[1000002];
//double b[1000002];//数组b存储每一头牛对应的离它最近的比它高的牛的距离
int main()
{
int i,j,k,n,max;
double sum;
while(scanf("%d",&n)!=EOF)
{
   //memset(b,0,sizeof(b));
   max=0;
   sum=0;//总距离 ,因为只要求出平均值 所以只要求出总距离
   for(i=0;i<n;i++)
   {
    scanf("%d",&a[i]);
    if(max<a[i])
     max=a[i];
   }
   for(i=0;i<n;i++)
   {
    if(a[i]==max){
     sum+=n;
    }
    else{
     for(j=i-1,k=i+1;j>=0||k<n;j--,k++)//2边一起走
     {
      if(j>=0)
       if(a[i]<a[j])
       {
        //b[i]=i-j;
        sum+=i-j;
        break;
       }
       if(k<n)
        if(a[i]<a[k])
        {
         //b[i]=k-i;
         sum+=k-i;
         break;
        }
        /* if(a[i]==max)
        {
        b[i]=n;
        break;  
     }*/
     } //找到符合条件的一个就跳出
    }
   }
   //for(i=0;i<n;i++)
   // sum+=b[i];
   printf("%.2lf\n",sum/n);
 
}
return 0;
}

 

你可能感兴趣的:(Integer,ini,input,each,output,distance)