题目链接: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. vc++基础班[26]---进程的相关操作

      ①.ShellExecute 进行进程的创建,属于 shell api: //int iRet = (int)ShellExecute(m_hWnd, _T("open"), ...

  2. Power Network POJ - 1459 [网络流模板]

    http://poj.org/problem?id=1459 嗯,网络流模板...多源点多汇点的图,超级汇点连发电厂,用户连接超级汇点 Status Accepted Time 391ms Memor ...

  3. 【翻译】Chemkin - Chapter 1

    [这段文字是2013年3月翻译的,整理老文档时发现就拿出来,提醒我们:任何试图翻译英文手册的尝试都是徒劳的] 第一章   介绍 Chemkin理论手册提供了一个对关系和公式的宽泛的概览,这些关系和公式 ...

  4. eclipse 迁移项目 乱码

    eclipse 迁移项目总是乱码问题,网上解决都无非是把workspace.项目.文件等改成utf-8,但总是不好使,因为原来有的文件类型还是要改成原来的编码格式,可以使用文本工具如notepad打开 ...

  5. in exists

    区别及应用场景 in 和 exists的区别: 如果子查询得出的结果集记录较少,主查询中的表较大且又有索引时应该用in, 反之如果外层的主查询记录较少,子查询中的表大,又有索引时使用exists.其实 ...

  6. selenium——获取元素的尺寸、文本信息、元素的属性、元素是否可见

    [is_disabled 可以用来检查元素是否存在]

  7. jquery中的attr与prop的区别,什么时候用attr,什么时候用prop

    只要有 Boolean() 属性的,简单说就是具有true 和 false 两个属性的属性,如 checked, selected 或者 disabled 使用prop(),(其实这些都是表单类的), ...

  8. npm dev run 报错

    解决办法: npm run dev --port 8088 Error: listen EACCES 0.0.0.0:8080at Object.exports._errnoException (ut ...

  9. PID控制器开发笔记之四:梯形积分PID控制器的实现

    从微积分的基本原理看,积分的实现是在无限细分的情况下进行的矩形加和计算.但是在离散状态下,时间间隔已经足够大,矩形积分在某些时候显得精度要低了一些,于是梯形积分被提出来以提升积分精度. 1.梯形积分基 ...

  10. web页面乱码,JSP页面编码设置

    解决Web页面访问出现乱码bug,JSP页面首行添加: <%@ page language="java" contentType="text/html; chars ...