#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. NSDate的具体使用(转载)

    NSDate的具体使用 时间与日期处理 主要有以下类: NSDate -- 表示一个绝对的时间点 NSTimeZone -- 时区信息 NSLocale -- 本地化信息 NSDateComponen ...

  2. HTTP1.1学习笔记 -- RFC2616

    本人跟web无缘,从来没有想去学http,现在看来,学学也是有益无害,总会要用着点滴. RFC见这里: https://www.ietf.org/rfc/rfc2616.txt 0. URI格式 ht ...

  3. jquery带按钮的图片切换效果

    <!doctype html> <html> <head> <meta charset="gb2312"> <title> ...

  4. jquery特效(4)—轮播图②(定时自动轮播)

    周末出去逛完街,就回公司好好地研究代码了,也算是把定时自动轮播程序写出来了,特意说明一下,这次的轮播图是在昨天随笔中jquery特效(3)—轮播图①(手动点击轮播)的基础上写出来的,也就是本次随笔展示 ...

  5. Nginx配置故障转移

    当上游服务器(真实访问服务器),一旦出现故障或者是没有及时相应的话,应该直接轮训到下一台服务器,保证服务器的高可用. 如果上游服务器的某一台宕机了,直接轮训到下一个~ 8080 8081 8082 关 ...

  6. python string写入二进制文件——直接wb形式open file,再write string即可

    4 down vote accepted You misunderstood what \xhh does in Python strings. Using \x notation in Python ...

  7. 点分治 [IOI2011]Race

    BZOJ2599. 放板子.利用agc009-D 代码简洁了很多 #include <bits/stdc++.h> #define N 200005 using namespace std ...

  8. bzoj 5072 [Lydsy1710月赛]小A的树——树形dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...

  9. 各个国家 不同字符集的unicode 编码范围

    原文地址:http://blog.csdn.NET/xzl04/article/details/6307416 0000-007F:C0控制符及基本拉丁文 (C0 Control and Basic  ...

  10. Ubuntu+win7 双系统修改开机启动项顺序

    Ubuntu和windows双系统安装完后默认Ubuntu系统是第一启动项,等待时间是10秒 如果你想改成windows为第一启动项 先进去Ubuntu系统 打开终端 (Ctrl+Alt+T) 修改启 ...