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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值