51nod 1272 最大距离
来源:互联网 发布:淘宝空间图片协议 编辑:程序博客网 时间:2024/05/21 22:23
1272 最大距离
题目来源: 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示例
6536342
Output示例
3
ac 代码:
#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<stack>#include<set>using namespace std;#define ll long long#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define maxn 50000+10struct arr{ int v,in; friend bool operator <(arr x,arr y){ if(x.v==y.v) return x.in>y.in; return x.v>y.v; }};priority_queue<arr>q;int main(){ int n; scanf("%d",&n); arr a; for(int j=1;j<=n;j++){ a.in=j; cin>>a.v; q.push(a); } int i=n,ans=0; while(!q.empty()){ // cout<<q.top().v<<endl; if(q.top().in<i) i=q.top().in; ans=max(ans,q.top().in-i); q.pop(); } cout<<ans<<endl; return 0;}
tle 代码
#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<stdio.h>#include<string.h>#include<vector>#include<queue>#include<stack>#include<set>using namespace std;#define ll long long#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define maxn 50000+10int a[maxn];int main(){ int n; cin>>n; for(int j=1;j<=n;j++) scanf("%d",a+j); int i=0; for(int j=1;j<=n;j++){ for(int k=i+j;k<=n;k++){ if(a[k]>=a[j]){ i=max(i,k-j); if(i==n-1){ cout<<i<<endl; return 0; } } } } cout<<i<<endl; return 0;}
阅读全文
0 0
- 51NOD 1272 最大距离
- 51nod 1272 最大距离
- 51Nod 1272 最大距离
- 51nod 1272 最大距离
- 51nod 1272 最大距离
- 51nod 1272 最大距离 (单调栈)
- 51Nod-1272-最大距离
- 51nod oj 1272 最大距离【贪心】
- 51nod 1272 最大距离(贪心)
- 51nod 1272 最大距离 By Assassin
- 51nod 1272 最大距离【思维】
- 51nod 1272 最大距离(思维)
- 51nod 1272 最大距离(贪心)
- 51nod 1272 最大距离 思维题
- 51 nod 1272 最大距离(树状数组)
- 51nod 1272 最大距离 -暴力
- 51NOD 1272 最大距离(线段树)
- 51nod 1272 最大距离 (贪心或单调栈)
- 创建登录界面
- java读取tensorflow中图像的分类模型
- 10.25第九周java作业
- [Redis学习笔记]-Redis 发布订阅(充当消息组件)
- mxnet 基础学习笔记(李沐课)
- 51nod 1272 最大距离
- SVN_SERVER的搭建
- 基于Mathematica的机器人仿真环境(机械臂篇)
- 多线程知识点总结二
- NVIDIA CUDA Compiler Driver NVCC
- 简单实用的TAB切换标签
- SharedPreferences和handler实现的欢迎页面
- Elasticsearch在Centos 7上的安装与配置
- 学会利用java程序编写“随机输入三角形的三边,判断是否能构成三角形“