51nod 1272 最大距离
2024-09-30 11:05:55
给出一个长度为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 ;
}
最新文章
- 我有几个NUMA节点
- 创建 PDO 实例并在构造函数中设置错误模式
- Failed to execute request because the App-Domain could not be created.
- 【读书笔记】iOS-写代码注意事项
- 【Vijos】1431 守望者的逃离
- sql 流水号
- javascript权威指南笔记--javascript语言核心(四)
- Day01_JAVA语言基础第一天
- No.007 Reverse Integer
- ZOJ 3555	Ice Climber(dp)
- Jquery attr()方法 属性赋值和属性获取
- 转载Jquery中的三种$()
- 7个改变世界的Java项目
- bzoj2732: [HNOI2012]射箭 半平面交
- Hdu1384-Intervals(差分约束)
- UGUI batch 规则和性能优化
- ELK学习总结(3-1)elk的基本查询
- unity用PUN进行信息交互模块
- postman(三):添加断言
- 机器学习面试--一句话概括传统ML算法
热门文章
- skynet实践(8)-接入websocket
- UVA-11054(扫描法)
- codeforces 669E E. Little Artem and Time Machine(节点为map型的线段树)
- VM 下安装ghost版系统
- Video.js事件
- apache日志信息详解
- View Programming Guide for iOS ---- iOS 视图编程指南(一)
- CentOS 7 设置系统语言为英文并解决 cannot change locale 问题
- 自定义层or网络
- js和jquery给iframe src赋值的3种方法