A - Exposition CodeForces - 6E
题目链接: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的更多相关文章
- Codeforces - 6E - Exposition - 尺取
https://codeforc.es/problemset/problem/6/E 既然可以多个log,那就直接map伺候.尺取之后要查询区间里面的最大值和最小值的差.众所周知尺取的时候要是不是有序 ...
- codeforces 6E (非原创)
E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...
- 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/ ...
- [Codeforces Education Round 6E] New Year Tree
[题目链接] https://codeforces.com/contest/620/problem/E [算法] 显然 , 一棵子树的DFS序必然为连续的一段 用线段树维护颜色数即可 [代码] #in ...
- Codeforces Beta Round #6 (Div. 2 Only) 单调队列
题目链接: http://codeforces.com/contest/6/problem/E E. Exposition time limit per test 1.5 secondsmemory ...
- 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 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
随机推荐
- VC常用小知识
(1) 如何通过代码获得应用程序主窗口的 指针?主窗口的 指针保存在CWinThread::m_pMainWnd中,调用AfxGetMainWnd实现.AfxGetMainWnd() ->Sho ...
- Win10系统BitLocker解锁后再次快速锁定办法
原文地址:https://blog.csdn.net/ttljtw/article/details/54022241 谁都不愿意把自己电脑上资料完全公开,对资料选择性加密处理是唯一的办法. 微软Win ...
- mysql5.6基于主从复制的mmm高可用架构详解
MMM规划192.168.3.12 master192.168.3.13 slave1192.168.3.198 slave2 MMM部署步骤1.配置主主复制及主从同步集群2.安装主从节点所需要的支持 ...
- Windows服务没有及时响应启动或控制请求1053
参考链接: 解决“指定的服务已经标记为删除”问题 服务没有及时响应启动或控制请求 1053 关闭服务后,重新启动windows服务报错:"服务没有及时响应启动或控制请求 1053" ...
- ajax请求封装的公共方法
/** * post 方法 */ function ajaxPost(url, params, callBack) { ajax(url,params,"post",callBac ...
- 《JavaWeb从入门到精通》(明日科技,清华大学出版社)
<JavaWeb从入门到精通>(明日科技,清华大学出版社)
- FTP服务器配置和管理
一:ftp 简介 1:ftp服务: internet 是一个非常复杂额计算机环境,其中有pc/mac/小型机/大型机等.而在这些计算机上运行的操作系统也是五花八门,有 unix.Linux.微软的wi ...
- 基于 Confluence 6 数据中心的 SAML 单点登录设置 SSL/TLS
请确定 SAML 授权是安全和私有的,你需要在你的应用程序中设置SSL/TLS. 请参考in the application. See Running Confluence Over SSL or H ...
- ORM 对象关系映射
ORM (object relation mapping) 就是将对象数据转换为sql语句并执行 对象关系映射框架 orm 需要做的事情 1 生成创建表的语句 2 插入数据的语句 3 删除数据的语句 ...
- map函数和reduce函数的区别
①从参数方面来讲:map()函数: map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组).其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数.reduce()函数 ...