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;
}
The nearest taller cow相关推荐
- TOJ2640 The nearest taller cow
描述 Farmer Zhao's N cows (1 ≤ N ≤ 1,000,000) are lined up in a row. So each cow can see the nearest c ...
- HZNUOJ 1472 The nearest taller cow
1472: The nearest taller cow Time Limit: 3 Sec Memory Limit: 64 MB Submit: 151 Solved: 6 Scores: 94. ...
- POJ 3268 D-Silver Cow Party
http://poj.org/problem?id=3268 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- usaco Cow Tours 牛的旅行
Cow Tours 牛的旅行 农民 John 的农场里有很多牧区.有的路径连接一些特定的牧区.一片所有连通的牧区称为一个牧场. 但是就目前而言,你能看到至少有两个牧区不连通.这样,农民 John 就有 ...
- 贪心 POJ - 3617 Best Cow Line
Best Cow Line POJ - 3617 FJ is about to take his N (1 ≤ N ≤ 2,000) cows to the annual"Farmer of ...
- 【POJ】3268 Silver Cow Party (将有向图的边反转)
问题链接:http://poj.org/problem?id=3268 [问题描述] One cow from each of N farms (1 ≤ N ≤ 1000) conveniently ...
- 解题报告:CF1307D Cow and Fields(最短路、最优解不等式化简)
CF1307D Cow and Fields 整张无向图的边权为1. 首先求出 1,n1,n1,n 两个点的单源最短路径.这 kkk 个特殊点中,我们令第 aaa 个特殊点到 111 的距离为 xax ...
- SP11469 SUBSET - Balanced Cow Subsets(折半搜索+状态压缩)难度⭐⭐⭐⭐★
题目链接 SP11469 SUBSET - Balanced Cow Subsets 题目翻译 给出N(1≤N≤20)N(1≤N≤20)N(1≤N≤20)个数M(i)(1<=M(i)<=1 ...
- P2888 [USACO07NOV]牛栏Cow Hurdles(Floyd算法)
P2888 [USACO07NOV]牛栏Cow Hurdles 行 1-T: 行 i 为一个整数,表示任务i路径上最高的栏的高度的最小值.如果无法到达,输出 -1. 5 6 3 1 2 12 3 2 ...
最新文章
- 更新!带你认识推荐系统全貌的论文清单
- SCImago Journal Rank:微生物领域期刊综合评价指标排名
- 【设计模式】两大策略和六大原则
- 别傻了,你还认为 count(1) 比 count(*) 效率高?
- Hybris commerce产品主数据的搜索API,批量返回若干主数据的值
- java 中的chartdata_获取Helm Charts中的文件夹列表
- OJ1080: a+b(多实例测试3)(C语言)
- FTP服务器构建与维护,ftp服务器的搭建与三种访问途径
- 【Elasticsearch】Elasticsearch中数据是如何存储的
- 利用python编写祝福_手把手|教你用Python换个姿势,送狗年祝福语
- 二分法查找的java简单实现
- j2ee三大框架个人理解
- 【40周年系列活动】中国干细胞第十届年会(2020·贵阳)第二轮通知
- Linux远程拷贝文件命令 - scp
- VBA读excel写xml
- App登陆页面必备(一)
- PLC可编程控制器、单片机开发应用及电气控制综合实训装置
- 宾夕法尼亚州立大学将领导 750 万美元的 GaN 辐射效应研究
- 艾孜尔江在腾讯网上的影视作品
- Linux ftp服务器虚拟用户的建立