#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <stack>
using namespace std; typedef long long ll;
struct node{int v, id;};
stack<node>q, sq; int main(){
int n;
scanf("%d", &n);
while(!sq.empty()) sq.pop();
int ans = ;
for(int i = ;i < n;i++){
int x;
scanf("%d", &x);
node a;
a.v = x; a.id = i;
if(sq.size() == || sq.top().v > x){
sq.push(a);
}
else{
while(sq.top().v <= x){
ans = max(ans, i - sq.top().id);
q.push(sq.top());
sq.pop();
if(sq.empty())
break;
}
while(!q.empty()){
sq.push(q.top());
q.pop();
}
}
}
printf("%d\n", ans);
return ;
}

后来提交一波的时候,最后一组数据超时,于是改用贪心,排序一遍过的:

 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = +;
struct node{
int id,v;
}a[maxn]; bool cmp(node xx,node yy)
{
if (xx.v!=yy.v)
return xx.v<yy.v;
return xx.id<yy.id;
} int main()
{
int n;
scanf("%d",&n);
for (int i=;i<n;i++)
{
scanf("%d", &a[i].v);
a[i].id=i;
}
sort(a,a+n,cmp);
int ans = ,Min=a[].id;
for (int i=;i<n;i++)
{
if (a[i].id > Min)
ans = max(ans, a[i].id-Min);
else Min = a[i].id;
}
printf("%d\n",ans);
return ;
}

51Nod 1272 最大距离 (栈或贪心)的更多相关文章

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

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

  2. 51nod 1272 最大距离

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

  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. vijos 1605 双栈排序 - 贪心 - 二分图

    题目传送门 传送门I 传送门II 题目大意 双栈排序,问最小字典序操作序列. 不能发现两个数$a_{j}, a_{k}\ \ (j < k)$不能放在同一个栈的充分必要条件时存在一个$i$使得$ ...

  6. 51nod 1163 最高的奖励(贪心+优先队列)

    题目链接:51nod 1163 最高的奖励 看着这题我立马就想到昨天也做了一道贪心加优先队列的题了奥. 按任务最晚结束时间从小到大排序,依次选择任务,如果该任务最晚结束时间比当前时间点晚,则将该任务的 ...

  7. 51nod 1117 聪明的木匠 (贪心)

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1117 跟挑战程序书上例题一样,将要切割的n断木板,分别对应二叉树树的叶子 ...

  8. 51Nod 1289 大鱼吃小鱼 栈模拟 思路

    1289 大鱼吃小鱼 栈模拟 思路 题目链接 https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289 思路: 用栈来模拟 ...

  9. 51Nod 1091 线段的重叠(贪心+区间相关,板子题)

    1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5         难度:1级算法题 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 2 ...

随机推荐

  1. ubuntu搭建ftp

    腾讯云服务器linux Ubuntu操作系统安装ftp服务器vsftpd Ubuntu Server 16.04.1 LTS 64位 登录服务器  xshell 登录服务器 用户名如果没自定义默认:u ...

  2. UVA1482 Playing With Stones —— SG博弈

    题目链接:https://vjudge.net/problem/UVA-1482 题意: 有n堆石子, 每堆石子有ai(ai<=1e18).两个人轮流取石子,要求每次只能从一堆石子中抽取不多于一 ...

  3. HDU4372 Count the Buildings —— 组合数 + 第一类斯特林数

    题目链接:https://vjudge.net/problem/HDU-4372 Count the Buildings Time Limit: 2000/1000 MS (Java/Others)  ...

  4. 整合kxmovie

    下载后发现kxmovie 直接运行是不通的,上面我们已经处理了ffmpeg部分的内容.当然了先处理ffmpeg部分.然后再复制kxmovie/kxmovie到项目中导入相应的文件. 编译报错:UIim ...

  5. U盘安装Ubuntu 14.04 LTS正式版

    Ubuntu 14.04 LTS正式版发布,而且提供五年的支持和维护服务.Ubuntu 14.04是Ubuntu开发团队历经五年的心血之作.许多新手都喜欢把Linux安装文件刻录成光盘再安装,而安装好 ...

  6. TestNG基本注解

    TestNG的注解: 注解 描述 @BeforeSuite 注解的方法将只运行一次,运行所有测试前此套件中. @AfterSuite 注解的方法将只运行一次此套件中的所有测试都运行之后. @Befor ...

  7. ubuntu nginx 安装 certbot(letsencrypt)

    https://certbot.eff.org 到上面网站按照步骤安装certbot, 安装完成后,certbot 生成证书有两种方式 第一种:standalone模式,certbot 会启动自带的n ...

  8. 【错误信息】springMVC No mapping found for HTTP request with URI

    出现这个问题的原因是在web.xml中配置错了:

  9. hdu-5738 Eureka(组合计数+极角排序)

    题目链接: Eureka Time Limit: 8000/4000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Pr ...

  10. 记录ubuntu16.04版本安装过程中遇到的问题

    记录ubuntu16.04版本安装和使用过程中遇到的些问题,方便以后查看,主要内容有: 1. ubuntu源替换 2. windows与vmware ubuntu文件夹共享 3. putty连接ubu ...