#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. MVC vs MVP vs MVVM

    一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面. 控制器(Controller):业务逻辑 模型(Model):数据保存 各部分之间的通信方式如下. View 传送 ...

  2. loj#2340. 「WC2018」州区划分

    FWT&&FMT板子 #include<cstdio> #include<iostream> #include<cstring> #include& ...

  3. Codeforces Round #340 (Div. 2) E. XOR and Favorite Number —— 莫队算法

    题目链接:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 ...

  4. java HttpClient GET请求

    HttpClient GET请求小实例,先简单记录下. package com.httpclientget; import java.io.IOException; import org.apache ...

  5. Transaction事务注解和DynamicDataSource动态数据源切换问题解决

    问题描述: 写主库开事务的情况下会导致时不时的将更新/插入操作写入到从库上, 导致mysqlException update command denied   问题原因: jetty的工作队列会重用处 ...

  6. valid No such filter: 'drawtext'"

    libfreetype is missing. You'll have to rebuild FFmpeg with this library or disable overlays. --enabl ...

  7. win7下使用source insight,没有Courier字体

    http://hi.baidu.com/raoxj/item/0e3a3a3b2461c5be134b14fa 1. “控制面板:--->“字体”--->找到Courier New(建议用 ...

  8. [转载]Dalvik指令集

    这篇文章是转载的,为了便于查找一些指令,贴在这里. 转自:http://blog.csdn.net/canfengxiliu/article/details/20144119 ------------ ...

  9. distutils 打包setup.py

    from distutils.core import setup setup(name='hello', version='1.0', description='test example', auth ...

  10. hdu 2899 Strange fuction —— 模拟退火

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2899 模拟退火: 怎么也过不了,竟然是忘了写 lst = tmp ... 还是挺容易A的. 代码如下: # ...