题目来源: Codility
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题
 收藏
 关注
给出一个长度为N的整数数组A,对于每一个数组元素,如果他后面存在大于等于该元素的数,则这两个数可以组成一对。每个元素和自己也可以组成一对。例如:{5, 3, 6, 3, 4, 2},可以组成11对,如下(数字为下标):
(0,0), (0, 2), (1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (3, 3), (3, 4), (4, 4), (5, 5)。其中(1, 4)是距离最大的一对,距离为3。
Input
第1行:1个数N,表示数组的长度(2 <= N <= 50000)。
第2 - N + 1行:每行1个数,对应数组元素Ai(1 <= Ai <= 10^9)。
Output
输出最大距离。
Input示例
6
5
3
6
3
4
2
Output示例
3
 贪心 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node{
int hao,shu;
}dian[];
bool cmp(node xx,node yy)
{
if (xx.shu!=yy.shu)
return xx.shu<yy.shu;
return xx.hao<yy.hao;
}
int main()
{
int n;scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d",&dian[i].shu);
dian[i].hao=i;
}
sort(dian,dian+n,cmp);
int ans=,mi=dian[].hao;
for (int i=;i<n;i++)
{
if (dian[i].hao>mi) ans=max(ans,dian[i].hao-mi);
else mi=dian[i].hao;
}
printf("%d\n",ans);
return ;
}

最新文章

  1. 我有几个NUMA节点
  2. 创建 PDO 实例并在构造函数中设置错误模式
  3. Failed to execute request because the App-Domain could not be created.
  4. 【读书笔记】iOS-写代码注意事项
  5. 【Vijos】1431 守望者的逃离
  6. sql 流水号
  7. javascript权威指南笔记--javascript语言核心(四)
  8. Day01_JAVA语言基础第一天
  9. No.007 Reverse Integer
  10. ZOJ 3555 Ice Climber(dp)
  11. Jquery attr()方法 属性赋值和属性获取
  12. 转载Jquery中的三种$()
  13. 7个改变世界的Java项目
  14. bzoj2732: [HNOI2012]射箭 半平面交
  15. Hdu1384-Intervals(差分约束)
  16. UGUI batch 规则和性能优化
  17. ELK学习总结(3-1)elk的基本查询
  18. unity用PUN进行信息交互模块
  19. postman(三):添加断言
  20. 机器学习面试--一句话概括传统ML算法

热门文章

  1. skynet实践(8)-接入websocket
  2. UVA-11054(扫描法)
  3. codeforces 669E E. Little Artem and Time Machine(节点为map型的线段树)
  4. VM 下安装ghost版系统
  5. Video.js事件
  6. apache日志信息详解
  7. View Programming Guide for iOS ---- iOS 视图编程指南(一)
  8. CentOS 7 设置系统语言为英文并解决 cannot change locale 问题
  9. 自定义层or网络
  10. js和jquery给iframe src赋值的3种方法