题目链接:https://vjudge.net/contest/202699#problem/A

题意

给一个n个元素的序列,从中挑出最长的子序列,要求子序列中元素差的最大值不超过k。问有几个最长子序列,子序列长度,以及这几个子序列的起始、终止位置。

  n<=10e5,k<=10e6 

题解

  很显然是水题。。

方法1.rmq+二分查找

很暴力很暴力的做法。若用st表维护时间复杂度为(nlogn),线段树(nlognlogn)

即维护出每一段区间的最小最大值

每次查找时确定头结点,二分尾节点

方法2.滑动窗口+单调队列

时间复杂度(n)

很容易发现这是个滑动窗口问题,即固定左端点,向右延伸右端点

若无法延伸,则移动左端点,而右端点维持原状

其中当前区域内的最小值和最大值显然用单调队列维护

代码

方法1:

 #include<iostream>
#include<cstdio>
typedef struct re {int x,h,t;};
int a[],f[];
re p1[],p2[];
using namespace std;
int n,k;
void build1(int x,int h,int t)
{
p1[x].h=h; p1[x].t=t;
if (h==t)
{
p1[x].x=a[h]; return;
}
int mid=(h+t)/;
build1(x*,h,mid); build1(x*+,mid+,t);
p1[x].x=max(p1[x*].x,p1[x*+].x);
}
void build2(int x,int h,int t)
{
p2[x].h=h; p2[x].t=t;
if (h==t)
{
p2[x].x=a[h]; return;
}
int mid=(h+t)/;
build2(x*,h,mid); build2(x*+,mid+,t);
p2[x].x=min(p2[x*].x,p2[x*+].x);
}
int query1(int x,int h,int t)
{
if (p1[x].h>t || p1[x].t<h) return();
if (h<=p1[x].h && p1[x].t<=t) return(p1[x].x);
return(max(query1(x*,h,t),query1(x*+,h,t)));
}
int query2(int x,int h,int t)
{
if (p2[x].h>t || p2[x].t<h) return();
if (h<=p2[x].h && p2[x].t<=t) return(p2[x].x);
return(min(query2(x*,h,t),query2(x*+,h,t)));
}
bool check(int x,int y)
{
if (query1(,x,y)-query2(,x,y)<=k) return(true);
else return(false);
}
main(){
scanf("%d%d",&n,&k);
for (int i=; i<=n; i++)
scanf("%d",&a[i]);
build1(,,n);
build2(,,n);
int maxn=;
for (int i=; i<=n; i++)
{
int h=,t=n-i;
while (h<t)
{
int mid=(h+t)/+;
if (check(i,i+mid)) h=mid; else t=mid-;
}
f[i]=h+; maxn=max(maxn,f[i]);
}
int cnt=;
for (int i=; i<=n; i++)
if (f[i]==maxn) cnt++;
printf("%d %d\n",maxn,cnt);
for (int i=; i<=n; i++)
if (f[i]==maxn) printf("%d %d\n",i,f[i]+i-);
return();
}

A - Exposition CodeForces - 6E的更多相关文章

  1. Codeforces - 6E - Exposition - 尺取

    https://codeforc.es/problemset/problem/6/E 既然可以多个log,那就直接map伺候.尺取之后要查询区间里面的最大值和最小值的差.众所周知尺取的时候要是不是有序 ...

  2. codeforces 6E (非原创)

    E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...

  3. Codeforces Beta Round #6 (Div. 2 Only) E. Exposition multiset

    E. Exposition Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/ ...

  4. [Codeforces Education Round 6E] New Year Tree

    [题目链接] https://codeforces.com/contest/620/problem/E [算法] 显然 , 一棵子树的DFS序必然为连续的一段 用线段树维护颜色数即可 [代码] #in ...

  5. Codeforces Beta Round #6 (Div. 2 Only) 单调队列

    题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...

  6. Codeforces Round #376 (Div. 2) A. Night at the Museum —— 循环轴

    题目链接: http://codeforces.com/contest/731/problem/A A. Night at the Museum time limit per test 1 secon ...

  7. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  8. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  9. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

随机推荐

  1. STL中vector、list、deque和map的区别

    1 vector     向量 相当于一个数组    在内存中分配一块连续的内存空间进行存储.支持不指定vector大小的存储.STL内部实现时,首先分配一个非常大的内存空间预备进行存储,即capac ...

  2. VIsual Studio编译OpenCV:无法打开python27_d.lib(python36_d.lib)的问题

    原文地址:http://blog.csdn.net/Chris_zhangrx/article/details/78947526 在用 VS2015 编译 Debug 版的 openCV 源码时,最后 ...

  3. 029_shell编写工作常用工具类总结

    一.检查命令的执行结果 function check_result() { result=$? flag=$1 if [[ "$result"x == "0"x ...

  4. 数据库入门-基本sql语句及数据类型

    一.基本sql语句 SQL语言主要用于存取数据.查询数据.更新数据和管理关系数据库系统,SQL语言由IBM开发.SQL语言分为3种类型: #.DDL语句 数据库定义语言: 数据库.表.视图.索引.存储 ...

  5. Swift 学习- 09 -- 枚举

    // 递归枚举 // 美家居为一组相关的值定义了一个共同的类型, 使你可以在代码中以类型安全的的方式使用这些值. // 如果你熟悉C语言, 你会知道在C语言中, 枚举会为一组整型值分配相关联的名称, ...

  6. JS跨域ajax访问

    方式1:jsonp解决跨域访问 需要服务和js配合 服务 [WebMethod] public void HelloWorld2(string name) { HttpContext.Current. ...

  7. LeetCode(117):填充同一层的兄弟节点 II

    Medium! 题目描述: 给定一个二叉树 struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *n ...

  8. python网络爬虫笔记(一)

    一.查询数据字典型数据 1.先说说dictionary查找和插入的速度极快,不会随着key的增加减慢速度,但是占用的内存大 2.list查找和插入的时间随着元素的增加而增加,但还是占用的空间小,内存浪 ...

  9. PHP编译安装时常见错误解决办法

    转载自:http://www.bkjia.com/PHPjc/1008013.html This article is post on https://coderwall.com/p/ggmpfa c ...

  10. jQuery绑定或删除绑定事件

    <!DOCTYPE html><html lang="en" class="loading"><head> <meta ...