题目来源: 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 ;
}

51nod 1272 最大距离的更多相关文章

  1. 51Nod 1272最大距离 (树状数组维护前缀最小值)

    题目链接 最大距离 其实主流解法应该是单调栈……我用了树状数组. #include <bits/stdc++.h> using namespace std; #define rep(i, ...

  2. 51Nod 1272 最大距离 (栈或贪心)

    #include <cstdio> #include <queue> #include <cstring> #include <iostream> #i ...

  3. 51nod 1272 最大距离 O(nlog(n)) , 快排 , 最大连续子串

    题目: 解法:排序,把值小的和索引小的放在前面,记录一下之前索引最小的就可以了. 没什么可以讲的,上代码吧: #include <bits\stdc++.h> using namespac ...

  4. 51nod 1272 思维/线段树

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 1272 最大距离 题目来源: Codility 基准时间限制:1 ...

  5. 1272 最大距离 只想到了dp

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1272 离散化后,用dp[i]表示向右,大于等于i这个数字的最大位置 dp ...

  6. 51nod 1272【二分+RMQ】

    思路: 这题不能说是长见识,倒是第一次写这么富有套路的题,倒着来,二分区间嘛,这个很简单啊,二分的条件查询一个当前区间的最小值是不是比那个特定的值小,一步步缩小,这就是二分嘛,然后查询用线段树的RMQ ...

  7. 51 Nod 1272 简单思维题

    1272 最大距离  题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 给出一个长度为N的整数数组A,对于每一个数组元素 ...

  8. 51nod水题记

    妈呀51nod已经刷不动了又开始跟bzoj一样总是得看题解了...那么发一下总结吧... 1051:最大子矩阵 #include<cstdio> #include<cstring&g ...

  9. 51nod 1378 夹克老爷的愤怒(树型dp+贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1378 题意: 思路:要想放得少,尽量放在叶子节点处,叶子节点处点比较多. ...

随机推荐

  1. luogu 4782【模板】 2-SAT 问题

    2-SAT就是给出$m$个限制表示$x==val_x || y==val_y$ 求出满足的解 每个点拆成两个点,如果$x$不满足则$y$一定满足,$y$不满足同理.这样我们连边,然后$tarjan$即 ...

  2. BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治

    BZOJ_3963_[WF2011]MachineWorks_斜率优化+CDQ分治 Description 你是任意性复杂机器公司(Arbitrarily Complex Machines, ACM) ...

  3. notepad++的NppFTP插件远程连接linux操作系统

    1.首先要有NppFTP插件,如果没有可以去下面链接或者其他网站下载:  https://sourceforge.net/projects/nppftp/files/latest/download   ...

  4. linux中使用netstat

    1 功能: 显示本机的网络连接.运行端口和路由表的信息. 2 常见选项 -a:显示本机所有连接和监听的端口 -n:网络IP地址的形式显示当前建立的有效连接和端口 -r:显示路由表信息 -s:显示按协议 ...

  5. E20180402-hm

    cascade  n. 串联; 倾泻; 小瀑布,瀑布状物; restrict  vt. 限制,限定; 约束,束缚; strict adj. 精确的; 绝对的; 严格的,严谨的; [植] 笔直的; re ...

  6. 51Nod 1095 Anigram单词

    熟练使用map即可,不然用vector会超时 #include <iostream> #include <cstring> #include <string> #i ...

  7. Hexo瞎折腾系列(6) - 将博客同时部署到Github和Coding

    前言 由于本人只是将Hexo博客同时部署到 Github 和 Coding.net ,所以这里只介绍怎么同时部署到这两个网站的pages. 之所以选择这两个网站,是因为国外用户可以访问 Github, ...

  8. [NOIP2018校模拟赛]T1 阶乘

    题目: 描述 有n个正整数a[i],设它们乘积为p,你可以给p乘上一个正整数q,使p*q刚好为正整数m的阶乘,求m的最小值. 输入 共两行. 第一行一个正整数n. 第二行n个正整数a[i]. 输出 共 ...

  9. Educational Codeforces Round 20 B

    Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...

  10. Codeforces Round #295 (Div. 2) B. Two Buttons

    B. Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...