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 ...
随机推荐
- latex公式、编号、对齐
原文地址:http://blog.csdn.net/hjq376247328/article/details/49718931 LaTeX的数学公式有两种,即行中公式和独立公式.行中公式放在正文中间, ...
- [NOI2004]郁闷的出纳员(到底是谁郁闷啊?)
一道 FHQ treap 的裸水题,卡了这么久.(咦~一看就是修为不够) 题解什么的,不用看的(话说那我为什么要写这篇题解咧...),直接 FHQ 模板腾上去就能秒 A 了(打脸) 谈谈 de ...
- .NET基础之构造函数
1.构造函数: 分为实例构造函数.静态构造函数.私有构造函数. 使用new表达式创建某个类的对象时, 1.1实例构造函数: (1)构造函数的名字与类名相同: (2)使用new表达式创建类的对象或者结构 ...
- Light OJ 1102
题意: 给你一个数 N , 求分成 K 个数 (可以为 0 ) 的种数: 思路: 类似 在K个抽屉放入 N 个苹果, 不为0, 就是 在 n-1 个空隙中选 m-1个: 为 0, 就可以先在 K 个抽 ...
- 面向对象(metaclass继承高级用法)
方法一:# class MyType(type):# def __init__(self,*args,**kwargs):# print('132')# super(MyType,self).__in ...
- Navicat系列产品激活教程
准备 本教程可破解12.x版本,如果教程失效请联系我 # 19.1.11 破解暂时失效,请勿更新 (如已更新请卸载重新安装老版本,数据不会丢失 http://download.navicat.com/ ...
- Centos7 设置静态IP地址
一: 修改网卡配置文件(操作前先备份一下该文件),/etc/sysconfig/network-scripts/ 具体操作如下: 1:进入修改目录 [root@localhost ~]# clear ...
- React-Native到0.44版本后Navigator 不能用的问题
新升级 到0.46版本以后 Navigator 不能使用报错. 'Navigator is deprecated and has been removed from this package. It ...
- day12 函数的嵌套调用 闭包函数,函数对象
函数嵌套: 函数嵌套: 嵌套指的是,一个物体包含另一个物体,函数嵌套就是一个函数包含另一个函数 按照函数的两个阶段 嵌套调用 指的是在函数的执行过程中调用了另一个函数,其好处可以简化外层大函数的代码, ...
- day02 编程语言和变量
编程语言 编程语言分为三种 机器语言:用计算机能听得懂的二进位制语言来写程序 汇编语言:用英文字母来代替二进位制来写的程序 高级语言(两种) 编译型语言(C语言):相当于谷歌翻译整个程序写好一次性写好 ...